0
votes

I need to change the sending domain on some emails sent from Laravel. To do so, I have set up a queued job, and the in handle method configure the Swift_SmtpTransport object as described here: 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));
    });
}

For some reason Mailgun doesn't like my login details.

When I send an email via the Mail facade, it works, so my logins are correct.

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

In mail.php I have:

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

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

What gives?

1
Do you have the right password in your .env?CommandZ
Yes, because sending with the Mail facade works. I only have my mail credentials in 1 place, the .env fileRob
Ok, so I went ahead and reset the credentials and it's working now. Thanks @CommandZ for trying to help :DRob

1 Answers

0
votes

if you have the same key for 2 domains on MAILGUN

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

you can put this before sending your mail. it works also on queues