laravel 如何解决“无法与主机 smtp.gmail.com 建立连接”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48654849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:21:05  来源:igfitidea点击:

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

phplaravelsmtpsendmaillaravel-forge

提问by Success Man

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 (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 :

从谷歌上的一些参考资料(通过 Laravel 使用 gmail smtp:无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]),我尝试了一些答案。我尝试将邮件端口和邮件加密更改为如下所示:

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 onhere : https://myaccount.google.com/lesssecureapps

我在on这里做: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

我的服务器使用 gitlab、forge 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

以前在登台服务器上发送邮件有效,但在我在伪造 laravel 上重新安装存储库后,它现在不起作用

What do I need to set?

我需要设置什么?

回答by Sagar Gautam

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.

几天前,我遇到了完全相同的问题,并且到处搜索,例如 laracast stackoverflow 和许多 github 问题,最终解决了问题。

You need configuration something like this,

你需要这样的配置,

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=yourpassword
MAIL_ENCRYPTION=ssl

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

完成此设置后,您应该确保您的 google 帐户2 step verification已关闭,因为这会增加额外的安全层,这就是无法建立连接的原因。

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 Securitytab. Click on it then you will see 2 step verficationthere. Then you need to turn off that. Then I think it'll work.

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

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

这件事对我有用,我希望它也对你有用。我希望你明白。

回答by Prabakaran T

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, [email protected] then change the email config using the settings provided by your provider. 2) for another solution need to know your version of laravel.

这是因为您的登台服务器中的发送邮件配置。有两种解决方案可用。1) 一种是,使用您的域名创建一个电子邮件地址,例如 [email protected],然后使用您的提供商提供的设置更改电子邮件配置。2)对于另一个解决方案需要知道你的laravel版本。

回答by badrul

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

由于您没有提到您的服务器设置,我认为这可能与 SELinux 布尔值有关。

If your SELinux is in enforcing mode, you need to turn on httpd_can_sendmailand httpd_can_network_connectbooleans.

如果您的 SELinux 处于强制模式,则需要打开httpd_can_sendmailhttpd_can_network_connect布尔值。

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

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

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

Check status of httpd sendmail and network connect booleans:

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

$ 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:

要启用 sendmail 和网络连接并使更改在重新启动后保持不变:

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