首页 > 解决方案 > Laravel:带有队列的 Mailgun 动态发送域

问题描述

我需要更改从 Laravel 发送的一些电子邮件的发送域。为此,我设置了一个排队作业,并且 in handle 方法配置Swift_SmtpTransport对象,如下所述:Laravel Mail Queue: change transport on fly

public function handle(Mailer $mailer)
{
    $transport = new \Swift_SmtpTransport(config('mail.host'), config('mail.port'), config('mail.encryption'));

    $transport->setUsername(config('mail.username'));
    $transport->setPassword(config('mail.password'));
    $transport->setLocalDomain(env('MAILGUN_EMAIL_DOMAIN'));

    $smtp = new \Swift_Mailer($transport);

    $mailer->setSwiftMailer($smtp);

    $mailer->send('emails.email', ['data'], function ($m) {
        $m->setTo($this->to)
          ->setBody($this->email->emailResponse)
          ->setSubject(sprintf('%s [Ref: #%s]', $this->email->emailThread->subject, $this->email->emailThread->slug));
    });
}

出于某种原因,Mailgun 不喜欢我的登录详细信息。

当我通过 Mail 门面发送电子邮件时,它可以工作,所以我的登录名是正确的。

Failed to authenticate on SMTP server with username "xxx" using 2 possible authenticators. Authenticator LOGIN returned Swift_TransportException: Expected response code 235 but got code "535", with message "535 5.7.0 Mailgun is not loving your login or password
" in /home/vagrant/code/myapp/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:457

mail.php我有:

'username' => env('MAIL_USERNAME'),

'password' => env('MAIL_PASSWORD'),

是什么赋予了?

标签: phplaravelswiftmailermailgun

解决方案


如果您在 MAILGUN 上的 2 个域具有相同的密钥

 $transport = (new TransportManager(app()))->driver('mailgun');
 $transport->setDomain('yourdomain.com');    
 FacadeMail::setSwiftMailer(new Swift_Mailer($transport));
 parent::send($mailer);

您可以在发送邮件之前将其放入。它也适用于队列


推荐阅读