Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

moses's avatar
Level 2

How can I solve "Connection could not be established with host smtp.gmail.com"?

If I do send email on the my localhost, it works. No error

But I try on the staging server, it display error like this :

Swift_TransportException in StreamBuffer.php line 268:
Connection could not be established with host smtp.gmail.com [Connection timed out #110]

in StreamBuffer.php line 268
at Swift_Transport_StreamBuffer->_establishSocketConnection() in StreamBuffer.php line 62
at Swift_Transport_StreamBuffer->initialize(array('protocol' => 'tcp', 'host' => 'smtp.gmail.com', 'port' => '587', 'timeout' => '30', 'blocking' => '1', 'tls' => true, 'type' => '1', 'stream_context_options' => array())) in AbstractSmtpTransport.php line 113
at Swift_Transport_AbstractSmtpTransport->start() in Mailer.php line 79
at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php line 395
at Mailer->sendSwiftMessage(object(Swift_Message)) in Mailer.php line 217
...

My env like this :

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=secret
MAIL_ENCRYPTION=tls

From some reference on the google (https://stackoverflow.com/questions/25572907/using-gmail-smtp-via-laravel-connection-could-not-be-established-with-host-smtp), I try some answer. I try change mail port and mail encryption to be like this :

MAIL_PORT=465
MAIL_ENCRYPTION=ssl

It does not work

I try again to change this :

MAIL_DRIVER=sendmail

It does not work too

I do on here : https://myaccount.google.com/lesssecureapps

It's the same

I try :

php artisan cache:clear
php artisan config:cache

It still does not work

My server using gitlab, forge laravel and digital ocean

Previous send mail on the staging server worked, but after I re-install the repository on the forge laravel, it now does not work

What do I need to set?

0 likes
38 replies
biishmar's avatar
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=secret
MAIL_ENCRYPTION=tls

this one is right config values.. are you using 2 step verification for that email?

if its you have use app password or normal password is fine...

2 likes
moses's avatar
Level 2

@biishmar Like description in my question above. I had try it all, but it's the same. It does not work. In my localhost it works. But in my staging server, it does not works

VijayKadiyam's avatar

Have you removed .env from the gitignore. If not, then make the necessary changes in the .env file to the server and run php artisan config:cache.

I think this may help you.

Snapey's avatar

Have you removed .env from the gitignore.

Don't do this unless you want all your confidential settings stored in your repository ... not a good idea.

5 likes
moses's avatar
Level 2

@Snapey I don't remove it. Then what is the right solution to solve my problem? I still have not found a solution

Snapey's avatar

The solution is to NOT use gmail for your outgoing mail server. Its not a good solution.

1 like
nats's avatar

@Snapey why? I am having the same problem but I am not using gmail, I am using the account I created in my cpanel and IDK what is wrong, I was thinking of changing my settings with gmail xD but I would like to know why it is not a good idea.

Snapey's avatar

@nats why would you want email coming from a gmail address instead of your domain?

sutherland's avatar

Use Mailgun, Amazon SES, or some other sending service. Your server's IP may have been associated with spam in the past and be blocked by Gmail.

1 like
moses's avatar
Level 2

@Snapey In my localhost, it works. This just does not work on the staging server.

3 likes
moses's avatar
Level 2

@sutherland I am still confused. Because when I try on my localhost, it works. This just does not work when run the send mail on the staging server

Defrag's avatar

Have you checked to make sure port 587 isn't blocked for outgoing traffic on your staging server?

sutherland's avatar

@moses that's because your local IP address is different than your server's IP. Your home IP is unlikely to get flagged by Gmail for spam, but you never know what your server's IP was previously used for.

D9705996's avatar

@moses - do you have access to the command line on your staging server? If so you can try

telnet smtp.gmail.com 587

If you don't get a connection refused error then the issue is not network related

djdiramio's avatar

Where is the staging server? I know for me, SMTP through Gmail doesn't work from my Homestead environment when on Verizon FiOS. I believe they are blocking that port.

One other thing to check is to make sure if you're setting any parameters in your .env file that are multiple words (such as a sender name) that you put them in quotes. i.e.

MAIL_FROM_NAME=First Last

will cause an error. Instead use:

MAIL_FROM_NAME="First Last"

It may not be your problem, but I've been burned on it before.

MuzammilRafay's avatar

when i change my mail_driver to sendmail it gives me this error proc_open() has been disabled for security reasons

lukegalea16's avatar

I am suffering from the same issue as @moses - Laravel Mailing works on my local host but not when the project is hosted on GoDaddy - any answers please?

DenMette's avatar

Hi Guys, did you change your .env files on your servers? Otherwise it will not work. Your .env file should not been pushed to your Github or other server so.

When deploying on laravel forge, you can change your env file on the admin page, when using ftp, you should change your configuration when uploading it to the server.

When you use gmail with 2 factor authentication, you need to create an app specific password.

Hopefully it gives an idea.

1 like
astro_369's avatar

Considering all the SMTP credentials are correct and properly configured, the reason why it works on localhost and not on staging server might be becasue of SELinux being enforced in the server.....

checkout if the SELinux is enforced. If it is,

$ setsebool -P httpd_can_network_connect on

$ setsebool -P httpd_can_sendmail on

1 like
webleyson's avatar

I think this maybe something to do with IPv6 I just upgraded my server from 32bit to 64 bit (and changed to ipv6) and am now getting the error.

digitlimit's avatar

If you are using Amazon EC2, do ensure to set outbound rules for SMTP (25) and SMTPS (465) to Anywhere.

MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD='yourgmailpassword'
MAIL_ENCRYPTION=ssl
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
2 likes
jeevamugunthan's avatar

@moses when you use gmail as your outgoing mail server you need to disable 2 step verification in your gmail account try this hope it will works fine... ;)

jeevamugunthan's avatar

@snapey haahaa...!! i didn't see the posted date , but the conversation is still going on i mention the wrong person

Snapey's avatar

@devnote Your SPAM link says NOTHING about resolving the issue.

Besides, the best advice is DON'T USE GMAIL FOR SENDING YOUR APPLICATION EMAIL

Next

Please or to participate in this conversation.