php - Laravel 即时更新电子邮件配置

标签 php email laravel-4 mailgun

我需要能够使用 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/

相关文章:

php - 使用 SimpleXML 解析 PHP 中嵌套的 XML/RDF 命名空间元素

php - 列出数据库中所有以 "pages_backup"开头的表名

iphone - iOS 邮件客户端测试

gdb 回溯中的 PHP-FPM 未知调用者

PHP获取本地日期和时间

php - 在 ubuntu 中通过 php 脚本发送邮件

php - 回复电子邮件以发表评论

OS X 上的 GitHub 客户端提交失败(“无法添加文件 ...)

php - 在 laravel 中播种时出现质量分配错误

php - 如何在 Laravel 的种子文件中禁用 'create_at' 和 'update_at'?