Swiftmailer Gmail 连接超时 #110 [英] Swiftmailer Gmail Connection timed out #110
问题描述
我想使用 gmail 的 smtp 和下面使用 Swiftmailer 发布的 PHP 脚本发送电子邮件.现在这在我自己的网络服务器上运行良好.但是当我在创建它的人的网络服务器上使用它时,我得到了一个例外:
I want to send emails using gmail's smtp with the PHP script posted below using Swiftmailer. Now this works fine on my own webserver. But when I used it on the webserver of the people I'm creating this for, I get an exception:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' in ...
可能是什么问题?我假设它与服务器设置的差异有关,因为代码在我自己的网络服务器上工作.我已经用 phpinfo() 检查了以下内容:
What could be the problem? I'm assuming its got to do with the difference in server settings, because the code works on my own webserver. I've checked with phpinfo() the following:
- Registered Stream Socket Transports tcp, udp, unix, udg, ssl, sslv3, sslv2, tls
- OpenSSL support enabled
- OpenSSL Library Version OpenSSL 1.0.1e-fips 11 Feb 2013
这是我的 PHP 代码:
This is my PHP code:
$emailname = MY_GMAIL_ACCOUNT_USERNAME;
$emailpass = MY_GMAIL_ACCOUNT_PASSWORD;
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername($emailname)
->setPassword($emailpass);
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($emailtitle)
->setFrom(array($emailname.'@gmail.com' => $emailsender))
->setTo(array($emailrecp))
->setBody($emailbody,'text/html');
$result = $mailer->send($message);
推荐答案
我在 Digital Ocean 服务器上遇到了同样的问题.原来他们在 IPv6 上默认阻止了 SMTP.修复方法如下:
I had the same issue on a Digital Ocean server. Turns out they're blocking SMTP by default on IPv6. Here's the fix:
纳米/etc/gai.conf优先级::ffff:0:0/96 100
nano /etc/gai.conf precedence ::ffff:0:0/96 100
这篇关于Swiftmailer Gmail 连接超时 #110的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!