使用PHPMailer和GMAIL SMTP发送电子邮件

时间:2012-07-03 20:20:36

标签: php phpmailer

我已经阅读了网络上的每个示例,我似乎仍然无法连接到GMAIL SMTP。这是我正在运行的代码:

include("phpMailer/class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer(); 
$mail->IsSMTP(); // send via SMTP

$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myUsername"; // SMTP username
$mail->Password = "myPassword"; // SMTP password
$mail->SMTPDebug = 1;
$webmaster_email = "webMasterEmail@gmail.com"; //Reply to this email ID
$email="someone@gmail.com"; // Recipients email ID
$name="SomeonesName"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Me";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
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";
}

我尝试在这里设置端口,我也在class.smtp.php文件中设置了以下内容:

$host = "ssl://smtp.gmail.com";
$port = 465;

我一直得到同样的错误,我确保启用了ssl。我得到的错误是:

SMTP -> ERROR: Failed to connect to server: No connection could be made because the target machine actively refused it.(10061)

1 个答案:

答案 0 :(得分:1)

正如我上面提到的,我已经通过摆脱它来启用了ssl;在线上使用php_openssl.dll并重启Apache。我做了一些阅读,我发现有些人在启用命令之前也有“[PHP_OPENSSL]”。我添加它并重新启动Apache,一切正常!感谢所有评论

在php.ini中:

[PHP_OPENSSL]
extension=php_openssl.dll