Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel动态更新电子邮件配置_Php_Email_Laravel 4_Mailgun - Fatal编程技术网

Php Laravel动态更新电子邮件配置

Php Laravel动态更新电子邮件配置,php,email,laravel-4,mailgun,Php,Email,Laravel 4,Mailgun,我需要能够发送电子邮件使用邮枪从一个域,切换设置,然后发送另一个从另一个域。但是,在发送第二封电子邮件之前更新设置不起作用,第二封电子邮件仍然使用第一封电子邮件设置发送 初始设置是在Config.mail和Config.services中设置的,这些都可以正常工作 // send first email try { Mail::send(--stuff here--) } catch (Exception $e) { ... } // update config using the

我需要能够发送电子邮件使用邮枪从一个域,切换设置,然后发送另一个从另一个域。但是,在发送第二封电子邮件之前更新设置不起作用,第二封电子邮件仍然使用第一封电子邮件设置发送

初始设置是在
Config.mail
Config.services
中设置的,这些都可以正常工作

// send first email
try { 
    Mail::send(--stuff here--) 
} catch (Exception $e) { ... }

// update config using the sandbox account details
Config::set('mail.username', 'postmaster@secret.mailgun.org');
Config::set('mail.password', 'password');
Config::set('services.mailgun.domain', 'domain.mailgun.org'); 
Config::set('services.mailgun.secret', 'key-secret');

// send second email
try { 
    Mail::send(--stuff here--) 
} catch (Exception $e) { ... }
// Second email has now been sent using the first emails config settings 
如果我注释掉第一封电子邮件发送,然后按上述方式更改设置,则第二封电子邮件将从沙箱帐户正确发送。如果我留下第一封电子邮件,它将从我在MailGun上的一个域发送


有人对此有经验吗?

谢谢你的回答。不幸的是,在Laravel5.4中,
share()
被删除,这将不再有效。上述代码的更新版本在5.4中使用单例而不是
share()

    config([
        'mail.host' => 'smtp.yandex.ru',
        'mail.port' => 465,
        'mail.encryption' =>'ssl',
        'mail.username' => 'username',
        'mail.password' => 'password'
    ]);

    $app = App::getInstance();

    $app['swift.transport'] = $app->share(function ($app) {
        return new TransportManager($app);
    });

    $mailer = new \Swift_Mailer($app['swift.transport']->driver());
    Mail::setSwiftMailer($mailer);

    $msg = Mail::send('mail.view', ['key' => 'value'], function(Message $message) {
        $message
            ->to('user@mail.com', 'Name')
            ->subject('Subject');
    });

我的情况稍微简单一点。我只需要选择一个不同的司机。这在Laravel 6.10中起了作用。也许你可以调整它以适应你的需要

app/Traits/MailDriver.php

namespace-App\Traits;
特征邮件驱动程序
{
公共功能驱动程序($driver)
{
config()->set('mail.driver',$driver);//根据需要进行配置。
app()->singleton('swift.transport',函数($app){
返回新的\light\Mail\TransportManager($app);
});
\邮件::setSwiftMailer(新的\Swift_邮件程序(app()['Swift.transport']->driver());
退还$this;
}
}
app/Mail/TestMail.php

namespace-App\Mail;
...
类TestMail扩展了Mailable
{
使用\App\Traits\MailDriver;//使用上面的trait
公共功能构建()
{
退还$this
->driver('ses')//在此处更改驱动程序
->从('info@site.com')
->查看('emails.ses');
}
}

您知道我们如何创建phpunit测试以确保其正常工作吗@琼丽
config(['mail.driver' => 'mailgun']);
config(['services.mailgun.domain' => mail_domain]);
config(['services.mailgun.secret' => mail_secret]);

$app = \App::getInstance();

$app->singleton('swift.transport', function ($app) {
    return new \Illuminate\Mail\TransportManager($app);
});

$mailer = new \Swift_Mailer($app['swift.transport']->driver());
\Mail::setSwiftMailer($mailer);

\Mail::to(me@example.com)->send(new TrialCreated($params));