【问题标题】:SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticateSMTP 错误:无法验证。邮件程序错误:SMTP 错误:无法验证
【发布时间】:2026-02-19 12:30:01
【问题描述】:
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 错误:无法验证。

【问题讨论】:

  • 使用$mail->SMTPDebug = 1; 找出显示错误的原因。

标签: php


【解决方案1】:

由于您使用的是 SSL。添加这行代码

$mail->SMTPSecure = 'ssl';

也改变你的

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

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

另外,请确保您已启用扩展程序 php_openssl.dll

【讨论】:

  • 仍然收到 SMTP 错误:无法进行身份验证。邮件程序错误:SMTP 错误:无法验证
  • 您使用的是哪个网络服务器?
  • 你的 php_openssl.dll 启用了吗?启用扩展后您是否重新启动了 wampserver?试试看,让我们知道。
【解决方案2】:

如果您确定您的用户名和密码正确无误 然后执行这些步骤

1 $mail -> SMTPSecure = 'ssl';

2 $mail->Host = "smtp.gmail.com";

3 https://support.google.com/mail/answer/78754

4 转到 google acocunts 然后连接应用程序并更改为不太安全的应用程序 按照编号3中的所有步骤

【讨论】: