Laravel Gmail SMTP: Works locally but fails on hosted site

Hey guys, I’m pulling my hair out over here! My Laravel app’s email setup is giving me grief. Everything’s fine on my local XAMPP, but as soon as I upload it to my cPanel hosting, boom! Authentication errors left and right.

Here’s the weird part: this just started happening about two weeks ago. Before that, it was smooth sailing on the live site too.

I’ve double-checked everything:

  • Using the correct Gmail address
  • Got an app password (2FA is on)
  • Tried different encryption settings

My .env file looks like this:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=my_app_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME=${APP_NAME}

I’ve scoured the web for similar issues, but everyone just says ‘Use 2FA and app passwords!’ which I’m already doing. My host swears they haven’t blocked SMTP either.

Any ideas what could be causing this? I’m seriously stumped!

hey mate, have u tried using a different email service? sometimes gmail can be finicky. maybe give mailgun or amazon ses a shot? they’re pretty reliable and might solve ur issue. also, double-check ur server’s php version - sometimes that can cause weird auth problems. good luck!

Have you considered that your hosting provider might be throttling SMTP connections? This can happen if they’ve recently implemented stricter anti-spam measures. It would explain why it worked before but suddenly stopped.

A workaround I’ve used is to implement queue workers for sending emails. This way, if there’s a temporary block or throttle, Laravel will keep retrying until the email goes through.

You could also try setting up a local mail server on your hosting account, like Postfix, and route your emails through that instead of connecting directly to Gmail’s SMTP.

If all else fails, you might want to look into using a transactional email service like Mailgun or SendGrid. They often have better deliverability rates and are less likely to be blocked by hosting providers.

I feel your frustration! I had a similar issue a while back, and it turned out to be something unexpected. Have you checked if your hosting provider has made any recent changes to their firewall or security settings? Sometimes they silently update these, which can interfere with SMTP connections.

One thing that worked for me was switching to port 465 with SSL encryption instead of 587 with TLS. It’s worth a shot if you haven’t tried it already.

Also, consider temporarily setting up a free SendGrid account just to rule out any Gmail-specific issues. It helped me isolate the problem when I was troubleshooting.

Lastly, don’t forget to clear your config cache after making changes:

php artisan config:clear

Sometimes Laravel holds onto old settings stubbornly. If all else fails, reach out to your hosting support again. They might have logs that could shed light on what’s blocking the connection.