多邮件配置 [英] multiple mail configurations

查看:16
本文介绍了多邮件配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆