At some point recently the Mail::send function stopped working on our Laravel server. It still works when it’s run locally though.
Mail::send(
'emails.hello',
$data,
function ($message) use ($data) {
$message->from('[email protected]', 'Admin');
$message->to('[email protected]', 'Admin')
->replyTo($data['email'], $data['name'])
->subject('Contact Request');
}
);
The .env configuration is exactly the same between the server and local instance, and I’ve run ‘php artisan config:cache’ to make sure the current .env file is loaded.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=xxx
MAIL_PASSWORD=xxx
MAIL_ENCRYPTION=tls
They are both running Laravel 5.5.50. The server is running PHP 7.2.24, and locally the PHP version is PHP 7.4.33. The server is responding with the following error:
Failed to authenticate on SMTP server with username "xxx" using 3 possible authenticators.
Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted.
I have 2-factor auth set up with my mail account and I am authenticating using an app password. Could this be a configuration issue somewhere in Laravel, or could it be a Google mail configuration issue?
2
Answers
If you’ve ruled out the same code and configuration (username and app password) working from one location (i.e. on your local machine) and not working in another location (the server where it’s been working for a while and now isn’t working), this generally points to something external.
Remember that Google are using a lot of smarts to protect you and others against abuse and spam from folks using their email systems.
It seems quite likely that your server may have been blocked as part of Google’s attempts to protect your account: https://support.google.com/accounts/troubleshooter/2402620?sjid=10023023285085076167-EU#ts=9279979
I had a similar problem on my test hosting when setting up websites for clients. However, when I moved the website to a new hosting platform to make it live, the email sending worked perfectly. This problem seemed to be caused by either a mistake in how the email system was set up on the original hosting or some limitations imposed by that hosting service.