Ale*_*abe 2 php email mailgun laravel-4
我需要能够使用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
Run Code Online (Sandbox Code Playgroud)
如果我注释掉第一封电子邮件发送,然后更改上面的设置,则第二封电子邮件将从沙盒帐户中正确发送.如果我留下第一封电子邮件,它将从我在MailGun上的域发送.
有人对这个有经验么?
小智 6
谢谢你的回答.不幸的是,Laravel 5.4 share()
被删除了,这将不再适用.上面代码的更新版本在5.4中使用单例而不是share()
.
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));
Run Code Online (Sandbox Code Playgroud)