多个邮件配置 [英] multiple mail configurations
本文介绍了多个邮件配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用 mandrill 驱动程序配置了 laravel 的邮件服务.这里没有问题!
I configured laravel's mail service with mandrill driver. No problems here!
现在,在我的应用程序的某个时刻,我需要通过 gmail 发送邮件.
Now, at certain point of my application, I need to send a mail via gmail.
我做了类似的事情:
// backup current mail configs
$backup = Config::get('mail');
// rewrite mail configs to gmail stmp
$new_configs = array(
'driver' => 'smtp',
// ... other configs here
);
Config::set('mail', $new_configs);
// send the email
Mail::send(...
// restore configs
Config::set('mail', $backup);
这不起作用,laravel 总是使用 mandrill 配置.看起来他在脚本启动时启动邮件服务并忽略您在执行期间所做的任何事情.
This doens't work, laravel always uses the mandrill configurations. Looks like he initiates mail service at script startup and ignores whatever you do during execution.
您如何在执行期间更改邮件服务配置/行为?
How do you change mail service configs/behaviour during execution?
推荐答案
您可以创建一个新的 Swift_Mailer
实例并使用它:
You can create a new Swift_Mailer
instance and use that:
// Backup your default mailer
$backup = Mail::getSwiftMailer();
// Setup your gmail mailer
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
$transport->setUsername('your_gmail_username');
$transport->setPassword('your_gmail_password');
// Any other mailer configuration stuff needed...
$gmail = new Swift_Mailer($transport);
// Set the mailer as gmail
Mail::setSwiftMailer($gmail);
// Send your message
Mail::send();
// Restore your original mailer
Mail::setSwiftMailer($backup);
这篇关于多个邮件配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文