cma*_*cre 21 laravel laravel-4
我用mandrill驱动程序配置了laravel的邮件服务.这里没问题!
现在,在我申请的某个时刻,我需要通过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);
Run Code Online (Sandbox Code Playgroud)
这不起作用,laravel总是使用mandrill配置.看起来他在脚本启动时启动了邮件服务,并忽略了执行过程中的任何操作.
如何在执行期间更改邮件服务配置/行为?
Bog*_*dan 43
您可以创建一个新Swift_Mailer
实例并使用它:
// 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);
Run Code Online (Sandbox Code Playgroud)
dev*_*ev7 13
派对有点晚了,但只是想扩大接受的答案并投入我的2美分,以防节省时间.在我的场景中,每个登录用户都有自己的SMTP设置但是我正在使用队列发送邮件,这导致设置在设置后恢复为默认值.它还创建了一些并发电子邮件问题.简而言之,问题是
$transport = Swift_SmtpTransport::newInstance($user->getMailHost(), $user->getMailPort(), $user->getMailEncryption());
$transport->setUsername($user->getMailUser());
$transport->setPassword($user->getMailPassword());
$mailer = new Swift_Mailer($transport);
Mail::setSwiftMailer($mailer);
//until this line all good, here is where it gets tricky
Mail::send(new CustomMailable());//this works
Mail::queue(new CustomMailable());//this DOES NOT WORK
Run Code Online (Sandbox Code Playgroud)
键盘敲击片刻之后,我意识到队列在一个单独的进程上运行,因此Mail :: setSwiftMailer根本不会影响它.它只是选择默认设置.因此,配置更改必须在发送电子邮件的实际时刻发生,而不是在排队时发生.
我的解决方案是将Mailable类扩展如下.
app\Mail\ConfigurableMailable.php
<?php
namespace App\Mail;
use Illuminate\Container\Container;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Mailable;
use Swift_Mailer;
use Swift_SmtpTransport;
class ConfigurableMailable extends Mailable
{
/**
* Override Mailable functionality to support per-user mail settings
*
* @param \Illuminate\Contracts\Mail\Mailer $mailer
* @return void
*/
public function send(Mailer $mailer)
{
$host = $this->user->getMailHost();//new method I added on User Model
$port = $this->user->getMailPort();//new method I added on User Model
$security = $this->user->getMailEncryption();//new method I added on User Model
$transport = Swift_SmtpTransport::newInstance( $host, $port, $security);
$transport->setUsername($this->user->getMailUser());//new method I added on User Model
$transport->setPassword($this->user->getMailPassword());//new method I added on User Model
$mailer->setSwiftMailer(new Swift_Mailer($transport));
Container::getInstance()->call([$this, 'build']);
$mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
$this->buildFrom($message)
->buildRecipients($message)
->buildSubject($message)
->buildAttachments($message)
->runCallbacks($message);
});
}
}
Run Code Online (Sandbox Code Playgroud)
然后改为CustomMail
扩展ConfigurableMailable
而不是Mailable
:
class CustomMail extends ConfigurableMailable {}
这样可以确保即使Mail::queue(new CustomMail())
在发送之前调用也会设置每用户邮件设置.当然,您必须在某些时候将当前用户注入CustomMail,即Mail::queue(new CustomMail(Auth::user()))
虽然这可能不是理想的解决方案(即,如果尝试发送批量电子邮件,最好配置一次邮件而不是每次发送的电子邮件),我喜欢它的简单性以及我们不需要更改全局Mail
或Config
设置的事实根本,只有$mailer
实例受到影响.
希望你觉得它有用!
对于Laravel 7.x及更高版本,您现在可以说明在发送电子邮件时要使用的邮件驱动程序。在config/mail.php
. 配置完成后,您可以通过mailer()
如下函数指定驱动程序的名称:
Mail::mailer('postmark')
->to($request->user())
->send(new OrderShipped($order));
Run Code Online (Sandbox Code Playgroud)
我希望它可以帮助某人。
小智 5
您可以即时设置邮件设置:
Config::set('mail.encryption','ssl');
Config::set('mail.host','smtps.example.com');
Config::set('mail.port','465');
Config::set('mail.username','youraddress@example.com');
Config::set('mail.password','password');
Config::set('mail.from', ['address' => 'youraddress@example.com' , 'name' => 'Your Name here']);
Run Code Online (Sandbox Code Playgroud)
也许您可以将设置值存储在 config/customMail.php 中并使用 Config::get('customMail') 检索它们
归档时间: |
|
查看次数: |
12254 次 |
最近记录: |