4

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
MAIL_USERNAME=secret@gmail.com
MAIL_PASSWORD=secret
MAIL_ENCRYPTION=tls

From some reference on the google (Using gmail smtp via Laravel: Connection could not be established with host smtp.gmail.com [Connection timed out #110]), 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?

moses toh
  • 12,344
  • 71
  • 243
  • 443

4 Answers4

4

Few days ago I've faced exactly the same problem and I've searched everywhere like laracast stackoverflow and many github issues and finally solve the problem.

You need configuration something like this,

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=ssl

After this setup you should make sure that your google account 2 step verification is turned off because this adds an extra layer of security that's why connection can't be established.

I think this is new feature added by google recently. I've working mail configuration before but recently same code doesn't work.

You can go to your google account where you will see Sign-in and Security tab. Click on it then you will see 2 step verfication there. Then you need to turn off that. Then I think it'll work.

This thing worked for me I hope it'll work for you as well. I hope you understand.

Sagar Gautam
  • 9,049
  • 6
  • 53
  • 84
  • Like description on the my question, I had try change mail port and mail encryption like that. But it's the same. I just saw `2 step verfication` on the `Sign-in and Security ` in my account. It's `off` – moses toh Feb 07 '18 at 03:29
  • Still not work ? I've the same issue and worked with above code. But I'm able to figure out how that work actually. – Sagar Gautam Feb 07 '18 at 03:40
  • I update my question with full error. Maybe you can solve the problem – moses toh Feb 07 '18 at 09:02
  • @SuccessMan I'll try – Sagar Gautam Feb 07 '18 at 09:08
  • From this link https://stackoverflow.com/questions/25572907/using-gmail-smtp-via-laravel-connection-could-not-be-established-with-host-smtp, I have tried all. Only 2 answers I have not tried. Answers from 李偉成 and John Foley. You try see it. I do not know where to set it. If you know that, you can give me another answer – moses toh Feb 07 '18 at 09:32
  • What are you using your gmail password or application password ? – Sagar Gautam Feb 07 '18 at 09:35
  • Yes, I using my gmail and my gmail password in the env – moses toh Feb 07 '18 at 09:36
  • I've just have same problem when I change my current gmail at env file to my second gmail. But with application password it works perfectly. Have a try :D – Sagar Gautam Feb 07 '18 at 09:37
  • I want to try answers from 李偉成 and John Foley on the link. But I don't know where to set it – moses toh Feb 07 '18 at 09:37
  • @SuccessMan I also don't understand those answers – Sagar Gautam Feb 07 '18 at 09:40
  • Okay. No problem. I just confused. Previously my send mail works. But when I re-install repository in my forge laravel. It becomes not working – moses toh Feb 07 '18 at 09:45
  • I've exactly the similar issue but solved I'm not able to provide you the way to get out of the problem. Since I still don't know what the problem was in my case but solved. – Sagar Gautam Feb 07 '18 at 09:49
  • In my localhost, it works. This just does not work on the staging server – moses toh Feb 14 '18 at 03:36
  • @SuccessMan Any Errors ? – Sagar Gautam Feb 14 '18 at 03:37
  • Same error. `Connection could not be established with host smtp.gmail.com [Connection timed out #110]` – moses toh Feb 14 '18 at 03:38
2
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls

You need to replace MAIL_DRIVER with MAIL_MAILER keyword in your env file.

0

This is because of send mail configuration in your staging server. There are two solutions are available. 1) One is, create a email address with your domain name for ex, example@domainname.com then change the email config using the settings provided by your provider. 2) for another solution need to know your version of laravel.

  • okay my version 7.1.11 and i tried the whole solutions and facing the same problem , so tell me please if you've more ! – Hesham Salama Aug 06 '18 at 07:01
0

Since you didn't mention your server settings, I assumed this might be related to SELinux booleans.

If your SELinux is in enforcing mode, you need to turn on httpd_can_sendmail and httpd_can_network_connect booleans.

You can verify if SELinux status is enforcing by running this command:

$ sestatus
...
Current mode: enforcing
...

Check status of httpd sendmail and network connect booleans:

$ getsebool httpd_can_sendmail httpd_can_network_connect
httpd_can_sendmail --> off
httpd_can_network_connect --> off

To enable sendmail and network connect and make changes persistant across reboots:

$ sudo setsebool -P httpd_can_sendmail 1
$ sudo setsebool -P httpd_can_network_connect 1
badrul
  • 71
  • 6