未捕获的异常“Swift_TransportException"和消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]"

Uncaught exception #39;Swift_TransportException#39; with message #39;Connection could not be established with host smtp.gmail.com [Connection timed out #110]#39;(未捕获的异常“Swift_TransportException和消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]) - IT屋-程序员软件开发技术分享
本文介绍了未捕获的异常“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]"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)