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 = "******@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "******"; // your SMTP password or your gmail password
$from = "***********@example.com"; // Reply to this email
$to="******@gmail.com"; // Recipients email ID
$name=" 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";
}
我收到此错误
SMTP 错误: 无法进行身份验证。邮件程序错误: SMTP 错误: 无法进行身份验证。
因为你使用的是SSL。添加此行代码
$mail->SMTPSecure = 'ssl';
同时更改您的
$mail->Host = "ssl://smtp.gmail.com";
自
$mail->Host = "smtp.gmail.com";
另外,请确保您已启用扩展php_openssl.dll
如果您确定您的用户名和密码正确然后执行以下步骤
1 $mail -> SMTPSecure = 'ssl';
2 $mail->主机 = "smtp.gmail.com";
3 https://support.google.com/mail/answer/78754
4 转到谷歌ACOCUNTS,然后连接应用程序并更改为安全性较低的应用程序按照第 3 条中的所有步骤操作