无法连接到SMTP主机。邮件程序错误:SMTP错误:无法连接到SMTP主机

时间:2014-01-09 11:25:05

标签: php smtp phpmailer

我正在使用PHPMailer发送带身份验证的邮件,但是当我尝试运行 sendmessage.php时,我收到以下消息。

SMTP -> ERROR: Failed to connect to server: Connection timed out (110) 
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

任何人都可以告诉我为什么我收到这条消息......以及一些解决方案

Mycode如下所示

sendmessage.php

<html>
<head>
<title>PHPMailer - SMTP (Gmail) basic test</title>
</head>
<body>

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded


$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "myemailid@gmail.com";
$mail->Password = "myemailpassword";
$mail->SetFrom('myemailid@gmail.com','vgvb');
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress('myfriendemailid');
 if(!$mail->Send())
    {
    echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else
    {
    echo "Message has been sent";
    }
?>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

从WAMP打开 php.ini 文件,确认 openssl.dll 已取消注释。

答案 1 :(得分:0)

试试这个

$mail->Host = "ssl://smtp.gmail.com";

答案 2 :(得分:0)

问题可能是您的防火墙阻止smtp.gmail.com,而是提供它应该工作的IP地址

74.125.129.108