2013-12-11 232 views
0

我需要帮助,请 这是我的代码:的PHPMailer和Gmail SMTP错误:无法连接到服务器:网络不可达(101)SMTP连接()失败

require 'PHPMailerAutoload.php'; 
$mail = new PHPMailer; 
$mail->isSMTP(); 
$mail->Host = "smtp.gmail.com"; 
$mail->SMTPDebug = 2; 
$mail->SMTPAuth = true; 

$mail->SMTPSecure = "tls"; 
$mail->Port = 587; 

$mail->Username = '[email protected]'; 
$mail->Password = 'somepass'; 
$mail->addAddress('[email protected]', 'Josh Adams'); 
$mail->Subject = 'PHPMailer GMail SMTP test'; 
$body = 'This is the HTML message body in bold!'; 
$mail->MsgHTML($body); 
if (!$mail->send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

,我得到这个错误: 2013-12-11 15:15:02 SMTP错误:无法连接到服务器:网络无法访问(101)SMTP连接()失败。邮件程序错误:SMTP连接()失败。

请帮忙吗?

回答

5

您可能想从隔离这个问题开始,以确定它是否真的是网络问题;或者它是否特定于PHP邮件程序或您的代码。在服务器上,从命令提示符下,尝试使用telnet连接到smtp.gmail.com端口587,就像这样:

telnet smtp.gmail.com 587 

您应该看到smtp.gmail.com的响应,像这样:

Trying 173.194.74.108... 
Connected to gmail-smtp-msa.l.google.com. 
Escape character is '^]'. 
220 mx.google.com ESMTP f19sm71757226qaq.12 - gsmtp 

您是否看到过这种情况,或者连接尝试是否挂起并最终超时?如果连接失败,则可能意味着您的托管公司阻止端口传出SMTP连接587

-2

变化

$mail->SMTPSecure = "tls";

$mail->SMTPSecure = 'ssl';

相关问题