How can I solve “Connection could not be establish

2019-03-01 02:15发布

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?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-01 02:51

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.

查看更多
SAY GOODBYE
3楼-- · 2019-03-01 03:04

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.

查看更多
等我变得足够好
4楼-- · 2019-03-01 03:05

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
查看更多
登录 后发表回答