一尘不染

SMTP connect()失败的PHPmailer-PHP

php

我是PHP新手。我试图通过PHPmailer向自己发送示例电子邮件。我正在使用gmail的smtp服务器。我正在尝试将示例邮件从我的gmail帐户发送到我的yahoo帐户。但是我得到了错误:Mailer Error: SMTP connect() failed.
这是代码:

<?php

require "class.phpmailer.php";
$mail = new PHPMailer(); 
$mail->IsSMTP();                              // send via SMTP
$mail->Host = "ssl://smtp.gmail.com";
$mail->SMTPAuth = true;                       // turn on SMTP authentication
$mail->Username = "myemail@gmail.com";        // SMTP username
$mail->Password = "mypassword";               // SMTP password
$webmaster_email = "myemail@gmail.com";       //Reply to this email ID
$email="myyahoomail@yahoo.in";                // Recipients email ID
$name="My Name";                              // Recipient's name
$mail->From = $webmaster_email;
$mail->Port = 465;
$mail->FromName = "My Name";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"My Name");
$mail->WordWrap = 50;                         // set word wrap
$mail->IsHTML(true);                          // send as HTML
$mail->Subject = "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";
}
?>

我在Windows 7 64位计算机上使用WAMP服务器。可能是什么概率?
请帮我解决这个问题。谢谢!


阅读 227

收藏
2020-05-26

共1个答案

一尘不染

您需要添加Host参数

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

另外,请检查是否已open_ssl启用。

<?php
echo !extension_loaded('openssl')?"Not Available":"Available";
2020-05-26