Gmail SMTP Connection Works Locally But Fails on Shared Hosting

My email sending setup works fine on my local machine but breaks when I upload it to my shared hosting account.

I’m using Laravel 11 with PHP 8.2 on a shared hosting service. When I try to send emails through Gmail’s SMTP, I keep getting this timeout error:

stream_socket_client(): Unable to connect to smtp.gmail.com:587 (Connection timed out)
Connection could not be established with host "smtp.gmail.com:587"

Here’s my email configuration:

MAIL_DRIVER=smtp
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASS=MyAppPassword
MAIL_SECURE=tls
[email protected]
MAIL_SENDER_NAME="My App"

What I’ve tried so far:

  • Double checked all my Gmail credentials
  • Tested ports 587, 465, and 25 (all timeout)
  • Verified 2FA is enabled and using app password
  • Ran openssl s_client -connect smtp.gmail.com:465 which also fails

My questions:

  • How do I check if my hosting provider blocks SMTP ports?
  • What alternatives exist for testing SMTP on shared hosting?
  • Should I contact support about opening blocked ports?
  • Are there cPanel settings I’m missing?

I only have cPanel access, no root privileges. The exact same code and settings work perfectly on my localhost setup.

yeah, most shared hosts block outgoing smtp ports for security. quick test - try telnet smtp.gmail.com 587 from ur host’s terminal if u can access it. if it hangs, the ports are definitely blocked. i’d use ur hosting provider’s mail server instead or switch to an api service like the other person suggested.

Check if your hosting provider has their own SMTP server you can use. Most shared hosts have internal mail servers that skip port restrictions entirely. Look in cPanel for mail settings or ask support for the server details. I had this exact problem with shared hosting - switching to their internal SMTP fixed it instantly. If that doesn’t work, you’ll probably need to upgrade to a VPS or dedicated server for more network control. The timeout on all ports (including your openssl test) confirms this is a hosting restriction, not a config issue.

It is common for shared hosting providers to block SMTP ports by default to prevent spam activities. You should verify with your hosting provider’s documentation or contact their support directly to confirm if these ports are indeed blocked. In many cases, it’s worth considering switching to a dedicated transactional email service such as SendGrid, Mailgun, or Amazon SES. These services typically use HTTP APIs which can circumvent port restrictions and offer better reliability. Laravel has built-in support for these services, and they often provide ample free tiers suitable for development and small applications.