多种邮件配置 [英] 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屋!
查看全文