php – 未捕获异常’Swift_TransportException’,消息’无法与主机smtp一起建立连接[连接超时#110]’

我已经在谷歌搜索过它,并在*尝试了所有建议,但我仍然遇到致命错误:

我正在使用SwiftMailer发送和发送电子邮件给GMAIL.它在我的localhost上完美运行但是当我上传它并试试它给我一个致命的错误:

这是我的代码的一部分:

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发送电子邮件时收到的致命错误:

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协议.

尝试

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465);
上一篇:linux – 使用带有uuencode的’sendmail’发送邮件,并附带一个主题


下一篇:php – 如何在ubuntu 14.04中从localhost发送邮件