我需要能够使用 Mailgun 从一个域发送电子邮件,切换设置,然后从另一个域发送另一个。但是,在发送第二封电子邮件之前更新设置不起作用,第二封电子邮件仍然使用第一封电子邮件设置发送。
初始设置在 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 上的域发送。
有人对这个有经验么?
最佳答案
谢谢楼上的回答。不幸的是在 Laravel 5.4 share()
被删除,这将不再起作用。使用单例代替 share()
在 5.4 中工作的上述代码的更新版本.
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));
关于php - Laravel 即时更新电子邮件配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35267198/