php Swift_TransportException 无法与主机 smtp.gmail.com 建立连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14176965/
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
Swift_TransportException Connection could not be established with host smtp.gmail.com
提问by Andres
I can't figure for the life of me why exactly is it failing.. this is the exact error I am getting:
我一生都无法弄清楚为什么它会失败..这是我得到的确切错误:
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection
could not be established with host smtp.gmail.com [Connection refused #111]' in
/home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php:259
Stack trace: #0
/home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php(64):
Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /home/content/38/6896038/html/test/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 /home/content/38/6896038/html/test/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /home/content/38/6896038/html/test/contact.php(55): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /home/content/38/6896038/html/test/lib/classes/Swift/Transport/StreamBuffer.php on line 259
And the code Im using is what I've gotten from the tutorial and reading on here examples, here it is:
我使用的代码是我从教程和阅读示例中得到的,这里是:
require_once 'lib/swift_required.php';
$transport = Swift_MailTransport::newInstance();
// Create the Transport the call setUsername() and setPassword()
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('[email protected]')
->setPassword('xxx')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$nombre = $_POST['name'];
$apellido = $_POST['apellido'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$title = $_POST['jobtitle'];
// Create the message
$message = Swift_Message::newInstance()
// Give the message a subject
->setSubject('Nuevo applicante para: ' . $title)
// Set the From address with an associative array
->setFrom(array('[email protected]' => 'no-reply'))
// Set the To addresses with an associative array
->setTo('[email protected]')
// Give it a body
->setBody('<html>
<head></head>
<body>
<table>
<tr>
<td>Nombre:</td><td>' . $nombre . ' ' . $apellido .'</td>
</tr>
<tr>
<td>Email:</td><td>' . $email . '</td>
</tr>
<tr>
<td>Telefono:</td><td>'. $telefono .'</td>
</tr>
</table>
</body>
</html>', 'text/html')
// Optionally add any attachments
->attach(Swift_Attachment::fromPath($_FILES["file"]["tmp_name"])->setFilename($_FILES['file']['name']));
// Send the message
$result = $mailer->send($message);
any help is greatly appreciated, I've done my reading and searching and cannot find a solution to this :(
非常感谢任何帮助,我已经完成了阅读和搜索,但找不到解决方案:(
EDIT: Apparently it's an issue with gmail, I changed to a different smtp and it worked.. :S
编辑:显然这是 gmail 的问题,我改用了不同的 smtp 并且它起作用了.. :S
回答by Charles
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection refused #111]
致命错误:未捕获的异常“Swift_TransportException”,消息为“无法与主机 smtp.gmail.com 建立连接 [连接被拒绝 #111]
Connection refused is a very explicit and clear error message. It means that the socket connection could not be established because the remote end actively refused to connect.
连接被拒绝是一个非常明确和明确的错误信息。这意味着无法建立套接字连接,因为远程端主动拒绝连接。
It's very unlikely that Google is blocking the connection.
Google 阻止连接的可能性很小。
It's very likely that your web hosting provider has firewall settings that block outgoing connections on port 465, or that they are blocking SMTP to Gmail. 465 is the "wrong" port for secure SMTP, though it is often used, and Gmail does listen there. Try port 587 instead. If the connection is still refused, call your host and ask them what's up.
很可能您的网络托管服务提供商的防火墙设置阻止了端口 465 上的传出连接,或者他们阻止了 SMTP 到 Gmail。465 是安全 SMTP 的“错误”端口,尽管它经常被使用,而且 Gmail 确实在那里侦听。请尝试使用端口 587。如果连接仍然被拒绝,请致电您的主机并询问他们发生了什么事。
回答by Ivan Proskuryakov
remove from your config.ymlspool: { type: memory }
从你的 config.yml 中删除spool: { type: memory }
Then you will be to add try catch like this
然后你将像这样添加 try catch
try{
$mailer = $this->getMailer();
$response = $this->getMailer()->send($message);
}catch(\Swift_TransportException $e){
$response = $e->getMessage() ;
}
回答by Kuya A
tcp:465 was blocked. Try to add a new firewall rules and add a rule port 465. or check 587 and change the encryption to tls.
tcp:465 被阻止。尝试添加新的防火墙规则并添加规则端口 465。或检查 587 并将加密更改为 tls。
回答by Amarja
I had the same issue when I was using Godaddy web hosting and solved this by editing my .envfile as,
我在使用 Godaddy 网络托管时遇到了同样的问题,并通过编辑我的.env文件解决了这个问题,
MAIL_DRIVER=smtp
MAIL_HOST=XXXX.XXXX.in
MAIL_PORT=587
MAIL_USERNAME=dexxxxx
MAIL_PASSWORD=XXXXXXXX
MAIL_ENCRYPTION=tls
Where MAIL_HOST is your domain name from Godaddy, MAIL_USERNAME is your user name from Godaddy and MAIL_PASSWORD is your password from Godaddy.
其中 MAIL_HOST 是您在 Godaddy 的域名,MAIL_USERNAME 是您在 Godaddy 的用户名,MAIL_PASSWORD 是您在 Godaddy 的密码。
I hope this may help you.
我希望这可以帮助你。
回答by Samir Daraf
My solution was to change:
我的解决方案是改变:
'driver' => 'smtp',
to
到
'driver' => 'mail',
In app/config/mail
在应用程序/配置/邮件中
Hope that helps
希望有帮助
回答by krishna Tyagi
firstly,check for gmail SMTP server . you should have to allow access for less secured apps without allowing swift mailer is not possible.for this login in your email account first then go into privacy settings and then click on sign and security then click on apps without access and then make on to less secure apps option then try mailer again, it will work then.
首先,检查 gmail SMTP 服务器。您应该必须允许访问安全性较低的应用程序而不允许 swift mailer 是不可能的。首先在您的电子邮件帐户中登录,然后进入隐私设置,然后单击签名和安全,然后单击没有访问权限的应用程序,然后进行更少的访问安全应用程序选项,然后再次尝试邮件程序,它将起作用。
回答by Muhammad Athar
In my case, I had trouble with GoDaddy and SSL encryption.
就我而言,我在使用 GoDaddy 和 SSL 加密时遇到了麻烦。
Setting the encryption to Null and Port to 80 (Or any supportive port) did the job.
将加密设置为 Null 并将端口设置为 80(或任何支持的端口)即可完成这项工作。
回答by Ezekiel Muigai
If you have hosted your website already it advisable to use the email or create an offical mail to send the email Let say your url is www.example.com. Then go to the cpanel create an email which will be [email protected]. Then request for the setting of the email. Change the email address to [email protected]. Then change your smtp which will be mail.example.com. The ssl will remain 465
如果您已经托管了您的网站,建议您使用电子邮件或创建官方邮件来发送电子邮件假设您的网址是 www.example.com。然后转到 cpanel 创建一个电子邮件,它将是 [email protected]。然后请求设置电子邮件。将电子邮件地址更改为 [email protected]。然后更改您的 smtp,即 mail.example.com。ssl 将保持为 465
回答by Mohammad Anini
I had the same problem for a while, replacing: smtp.gmail.comwith 173.194.65.108actually worked for me!
我有同样的问题了一会儿,替换:smtp.gmail.com与173.194.65.108实际工作对我!
回答by Jesus Rodriguez
In my case, I was using Laravel 5 and I had forgotten to change the mail globals in the .env file that is located in your directory root folder (these variables override your mail configuration)
就我而言,我使用的是 Laravel 5,但忘记更改位于目录根文件夹中的 .env 文件中的邮件全局变量(这些变量会覆盖您的邮件配置)
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=yourpassword
by default, the mailhost is:
默认情况下,邮件主机是:
MAIL_HOST=mailtraper.io
I was getting the same error but that worked for me.
我遇到了同样的错误,但这对我有用。

