如何解决"Connection could not be established with host smtp.gmail.com"?



如果我确实在我的本地主机上发送电子邮件,那就起作用。没有错误

但是我尝试在登台服务器上显示这样的错误:

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
...

我这样的env:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=secret@gmail.com
MAIL_PASSWORD=secret
MAIL_ENCRYPTION=tls

从Google上的一些参考(使用laravel使用Gmail SMTP:无法使用主机smtp.gmail.com [连接时间#110](,我尝试一些答案。我尝试将邮件端口和邮件加密更改为这样:

MAIL_PORT=465
MAIL_ENCRYPTION=ssl

它不起作用

我再次尝试更改此信息:

MAIL_DRIVER=sendmail

它不起作用

我在这里做on:https://myaccount.google.com/lesssecureapps

是相同的

我尝试:

php artisan cache:clear
php artisan config:cache

它仍然不起作用

使用gitlab,forge laravel和数字海洋的服务器

上一个登台服务器上的发送邮件可行,但是在我重新安装了forge laravel上的存储库后,它现在不起作用

我需要设置什么?

几天前,我遇到了完全相同的问题,我在laracast stackoverflow和许多github问题等任何地方都搜索过,最后解决了这个问题。

您需要类似的配置,

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,因为这添加了额外的安全层,这就是无法建立连接的原因。

我认为这是Google最近添加的新功能。我以前从事邮件配置,但最近同一代码不起作用。

您可以转到您的Google帐户,您可以在其中看到登录和安全性选项卡。单击它,然后您将在其中看到 2步vitfication 。然后,您需要关闭它。那我认为它会起作用。

这东西对我有用,我希望它也对您有用。希望您能理解。

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=tls

您需要在env文件中用mail_mailer关键字替换mail_driver。

这是因为登台服务器中的发送邮件配置。有两种解决方案。1(一个是,创建一个带有ex的域名的电子邮件地址,example@domainname.com,然后使用提供商提供的设置更改电子邮件配置。2(对于另一个解决方案,需要了解您的Laravel版本。

由于您没有提及服务器设置,所以我认为这可能与Selinux Booleans有关。

如果您的SELINUX处于执行模式,则需要打开httpd_can_sendmailhttpd_can_network_connect布尔值。

您可以通过运行此命令来验证Selinux状态是否正在执行:

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

检查httpd sendmail和网络连接布尔值的状态:

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

启用sendmail和网络连接并在重新启动之间进行更改持续:

$ sudo setsebool -P httpd_can_sendmail 1
$ sudo setsebool -P httpd_can_network_connect 1

最新更新