php Swiftmailer:无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25517281/
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-08-25 17:52:06  来源:igfitidea点击:

Swiftmailer: Connection could not be established with host smtp.gmail.com [Connection timed out #110]

phpemailsymfonyswiftmailer

提问by Jonathan

I'm posting because I've already gone through the solutions posted in other questions on here but they haven't helped.

我发帖是因为我已经浏览了此处其他问题中发布的解决方案,但它们没有帮助。

What I am trying to do is send email using Swiftmailer through Google Apps for Business Gmail but I keep getting this error:

我想要做的是通过 Google Apps for Business Gmail 使用 Swiftmailer 发送电子邮件,但我不断收到此错误:

Connection could not be established with host smtp.gmail.com [Connection timed out #110]

无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]

I know the code is fine because it works on my local machine but not on the production server.

我知道代码很好,因为它可以在我的本地机器上运行,但不能在生产服务器上运行。

What I have tried so far:

到目前为止我尝试过的:

  • Enabled OpenSSL.
  • Unblocked account from Google captcha.
  • Used an Application Specific Password.
  • Whitelisted Gmail SMTP IP addresses
  • 启用 OpenSSL。
  • 从 Google 验证码解锁帐户。
  • 使用了应用程序专用密码。
  • 列入白名单的 Gmail SMTP IP 地址

Configuration:

配置:

# Swiftmailer Configuration
swiftmailer:
    transport:  smtp
    encryption: ssl
    auth_mode:  login
    host:       smtp.gmail.com
    username:   [email protected]
    password:   applicationspecificpassword
    port:       465

What else can I try? Could this be a DNS issue since I am using Gmails SMTP MX records instead of the servers.

我还能尝试什么?这可能是 DNS 问题,因为我使用的是 Gmails SMTP MX 记录而不是服务器。

回答by ikuchris

The following steps worked for me :

以下步骤对我有用:

  1. Check if httpd_can_sendmailis on , run this getsebool httpd_can_sendmail

    • When you get: httpd_can_sendmail --> off, run setsebool -P httpd_can_sendmail 1
    • When you get: httpd_can_sendmail --> onthat's fine move on step 2.
  2. Check also if httpd_can_network_connectis on, run getsebool httpd_can_network_connect

    • When you get httpd_can_network_connect --> offrun setsebool -P httpd_can_network_connect 1
    • When you get: httpd_can_network_connect --> onthat's fine move on step 3.
  3. Use the following settings for smtp: 'host' => '64.233.166.108''port' => '465'
  1. 检查httpd_can_sendmail是否打开,运行这个getsebool httpd_can_sendmail

    • 当你得到:httpd_can_sendmail --> off,运行setsebool -P httpd_can_sendmail 1
    • 当您收到:httpd_can_sendmail --> 时,在第 2 步中就可以了。
  2. 还要检查httpd_can_network_connect是否打开,运行 getsebool httpd_can_network_connect

    • 当你得到httpd_can_network_connect --> offrunsetsebool -P httpd_can_network_connect 1
    • 当您获得:httpd_can_network_connect --> 时,在第 3 步中就可以了。
  3. smtp使用以下设置: 'host' => '64.233.166.108''port' => '465'

I'm using Centos

我正在使用 Centos

回答by Mohammad Anini

Replacing: smtp.gmail.comwith 173.194.65.108actually worked for me!

更换:smtp.gmail.com173.194.65.108实际工作对我!

回答by gregsanderson

If you ever want to return to using Gmail, I've just faced the same problem - the code worked on my local machine but not on a real server - and think I've figured out what is causing it (at least in my case).

如果您想重新使用 Gmail,我刚刚遇到了同样的问题 - 代码在我的本地机器上运行,但在真实服务器上不起作用 - 我想我已经弄清楚是什么导致了它(至少在我的情况下) )。

It appears that if a server supports IPv6 and IPv4, the server's domain resolves as its IPv6 version so PHP tries to connect to that. But I was finding that smtp.gmail.com did not respond, so after 30 seconds the script just gave up and timed out. You would think that PHP would try IPv4 after IPv6 failed, but no.

看起来,如果服务器支持 IPv6 和 IPv4,则服务器的域将解析为其 IPv6 版本,因此 PHP 会尝试连接到该版本。但是我发现 smtp.gmail.com 没有响应,所以 30 秒后脚本就放弃并超时了。您会认为 PHP 会在 IPv6 失败后尝试 IPv4,但事实并非如此。

So when I swapped in the IPv4 address of smtp.gmail.com (got by ping-ing it), everything worked fine and the email sent. It's not ideal using an IP in place of a domain since it could change, but at least it saves digging up the rest of the server's floorboards :)

因此,当我交换 smtp.gmail.com 的 IPv4 地址(通过 ping 得到)时,一切正常并且电子邮件已发送。使用 IP 代替域并不理想,因为它可能会改变,但至少它可以节省挖掘服务器的其余部分:)

回答by dwandw

just add 74.125.130.108 smtp.gmail.comto server's hosts file

只需添加 74.125.130.108 smtp.gmail.com到服务器的主机文件

回答by Mihai

For me the issue was i that I used tls encryption,here are the ports for TLS and SSL. Don`t forget to reboot after changing the env file.

对我来说,问题是我使用了 tls 加密,这里是 TLS 和 SSL 的端口。更改 env 文件后不要忘记重新启动。

smtp.gmail.com    ---    SSL       ---      465
smtp.gmail.com    ---   StartTLS ---   587

Also,you need to change the securityof your google account

此外,您需要更改谷歌帐户的安全性

回答by Arseniy

Config working for me:

配置为我工作:

mailer_transport: gmail
mailer_host: smtp.gmail.com
mailer_user: [email protected]
mailer_password: my_pass
mailer_encryption: ssl
mailer_auth_mode: login
mailer_port: 465

回答by Turbok

I've just had this issue and after immense search for the solution, it was all a matter of a security setting with my host (In this case Scaleway). I had to open security settings and enable outgoing SMTP.

我刚刚遇到了这个问题,经过大量搜索解决方案后,这完全是我的主机(在本例中为 Scaleway)的安全设置问题。我必须打开安全设置并启用传出 SMTP。

回答by okki

I have posted a workaround solution in Swiftmailer Gmail Connection timed out #110where you can either use IPv4 or IPv6.

我在Swiftmailer Gmail Connection timed out #110中发布了一个解决方案,您可以在其中使用 IPv4 或 IPv6。