如何解决“无法通过主机smtp.gmail.com建立连接"? [英] 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
...
我的环境如下:
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
它不起作用
我再次尝试更改此内容:
I try again to change this :
MAIL_DRIVER=sendmail
它也不起作用
我在这里on
: https://myaccount.google.com/lesssecureapps
是一样的
我尝试:
php artisan cache:clear
php artisan config:cache
它仍然不起作用
我的服务器使用gitlab,forge laravel和数字海洋
My server using gitlab, forge laravel and digital ocean
以前在登台服务器上发送邮件有效,但是在我重新安装伪造的laravel上的存储库后,它现在不起作用
Previous send mail on the staging server worked, but after I re-install the repository on the forge laravel, it now does not work
我需要设置什么?
推荐答案
几天前,我遇到了完全相同的问题,并且在各处搜索了类似laracast stackoverflow和许多github问题,并最终解决了该问题.
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.
您需要这样的配置
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=ssl
完成此设置后,您应该确保您的Google帐户2 step verification
已关闭,因为这增加了一层额外的安全保护,这就是无法建立连接的原因.
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.
我认为这是Google最近添加的新功能.我之前已经在进行邮件配置,但是最近相同的代码不起作用.
I think this is new feature added by google recently. I've working mail configuration before but recently same code doesn't work.
您可以转到自己的Google帐户,在该帐户中会看到登录和安全性标签.点击它,您将在此处看到两步验证.然后,您需要关闭该功能.然后我认为它会起作用.
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.
这篇关于如何解决“无法通过主机smtp.gmail.com建立连接"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!