PHPMailer使用Gmail作为SMTP服务器.无法连接到SMTP主机.Mailer错误:SMTP错误:无法连接到S


PHPMailer to use Gmail as SMTP server.Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host

我正在尝试使用phpMailer通过电子邮件向用户发送确认消息。我的代码是:

<?php
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "mypasswrord"; // your SMTP password or your gmail password
$from = "webmaster@example.com"; // Reply to this email
$to="receiver@yahoo.com"; // Recipients email ID
$name="Jersey Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

我已经在php.ini中启用了ssl。

PS>sender@gmail.com是一封保护隐私的屏蔽电子邮件。但我确实在那部分放了一个真实的电子邮件地址

php.ini中的

请确保已使用取消注释该行

extension=php_openssl.dll

这个页面有作者声称"完美工作"的代码。

我看到他和你的唯一真正区别是他的

$mail->Mailer = "smtp";

我想我会从他的代码开始,看看它是否有效,然后从那里调试。

错误:

$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server

良好:

$mail->Host = "smtp.gmail.com"; // specify main and backup server

从这里开始

2) 注释掉class.phpmailer.php中的以下代码行

/*
if(strstr($hosts[$index], ":"))
list($host, $port) = explode(":",
$hosts[$index]); else 
*/

如果你还没有,试试这个。