未捕获的异常“Swift_TransportException"和消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]" [英] Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]'

查看:65
本文介绍了未捕获的异常“Swift_TransportException"和消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 google 中搜索过它并在 StackOverflow 中尝试了所有建议,但我仍然收到 致命错误:

I had already searched it in google and tried all the suggestions here in StackOverflow but I still get a Fatal Error:

我使用 SwiftMailer 发送和 emailGMAIL.它在我的本地主机上完美运行,但是当我上传它并尝试时它给了我一个 致命错误:

I am using SwiftMailer for sending and email to GMAIL. It works perfectly on my localhost but when I uploaded it and try It gives me a Fatal Error:

这是我的代码的一部分:

Here is a part of my code:

require_once 'Swift-5.1.0/lib/swift_required.php';


$email = "fromemail@gmail.com";
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465);
$transport->setUsername("username@gmail.com");
$transport->setPassword("usernamepassword");

// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
   "to@gmail.com" => "First Name"
));
$message->setCc(array("cc_email1@gmail.com" => "Name NAme" , "cc_email2@gmail.com" => "His Name"));

$message->setSubject("Subject");

$message->setBody("The Body");

$message->setFrom($email, "Full Name");

$message->setReplyTo(array($email => "Full Name"));

// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);

这是我向 gmail 发送电子邮件时遇到的致命错误:

致命错误:在/homepages/41/d513702051/htdocs/php/Swift-5.1 中未捕获异常Swift_TransportException",并带有消息无法与主机 smtp.gmail.com [连接超时 #110] 建立连接".0/lib/classes/Swift/Transport/StreamBuffer.php:266 堆栈跟踪:#0/homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1/homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2/homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3/homepages/41/d513702051/htdocs/php/contactus.php(33): Swift_Mailer->send(Object(Swift_Message)) #4 {main} 抛出/homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php 在第 266 行

Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' in /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php:266 Stack trace: #0 /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array) #2 /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start() #3 /homepages/41/d513702051/htdocs/php/contactus.php(33): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in /homepages/41/d513702051/htdocs/php/Swift-5.1.0/lib/classes/Swift/Transport/StreamBuffer.php on line 266

推荐答案

我不确定,但我认为当你添加端口 465 时,Swift 类会自动添加 ssl 协议.

I'm not sure but I think when you adding port 465, Swift class automatically will add ssl protocol.

试试

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465);

这篇关于未捕获的异常“Swift_TransportException"和消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆