【问题标题】:How to configure gmail smtp and phpmailer?如何配置 gmail smtp 和 phpmailer?
【发布时间】:2026-02-01 12:20:03
【问题描述】:

我想知道问题出在哪里,因为我遇到了身份验证错误。

  1. 我已经有几个 gmail 帐户了,
  2. 我为自己的网站新建了一个,
  3. 我将“帐户和导入”中的新电子邮件添加到我的旧 gmail 帐户。
  4. 我将新的电子邮件用户名和密码复制到 phpmailer 代码中
include_once('phpmailer/class.phpmailer.php');
include_once('phpmailer/class.smtp.php');
//6nb5Drv;
function sendmail(){
$mail = new PHPMailer();  

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->Port = 587; // Set the SMTP port i tried and 457
$mail->Username = 'newmail@gmail.com';                // SMTP username
$mail->Password = 'newmailpass';                  // SMTP password

$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only

$mail->From = 'from@yahoo.com';
$mail->FromName = 'From';
$mail->AddAddress('to@gmail.com', 'To');  // Add a recipient

$mail->IsHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <strong>in bold!</strong>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}

echo 'Message has been sent';
}
sendmail();

但我收到了身份验证错误。 怎么了? 可能与凭据有关,如何配置 gmail smtp?

调试报告:

2015-12-04 17:56:15 客户端 -> 服务器:EHLO www.site.co 2015-12-04 17:56:15 客户端-> 服务器:STARTTLS 2015-12-04 17:56:15 客户端 -> 服务器:EHLO www.site.co 2015-12-04 17:56:15 客户端-> 服务器:授权登录 2015-12-04 17:56:15 客户端-> 服务器:UHJlZGljdG9sb2d5 2015-12-04 17:56:15 客户端-> 服务器: U2dHZlB0VHZUbTZ1SW9ZMi1qTlNCQQ== 2015-12-04 17:56:17 SMTP 错误: 密码命令失败:435 4.7.8 错误:身份验证失败: UGFzc3dvcmQ6 2015-12-04 17:56:17 SMTP 错误:无法验证。 2015-12-04 17:56:17 客户端-> 服务器:退出 2015-12-04 17:56:17 SMTP 连接失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting留言 无法发送。Mailer 错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

【问题讨论】:

标签: php smtp gmail phpmailer


【解决方案1】:

根据这篇文章: PHPMailer - SMTP ERROR: Password command failed when send mail from my server,在某些情况下,您必须向 google 指明这不是可疑活动,并在您的帐户中激活一些不太安全的选项。
假设这是正确的密码...

【讨论】:

  • 您不必这样做,尽管这是最简单的方法。另一种方法是 read the docs 并正确实施。
  • 嘿,有趣的事情发生了。一旦他们向我发送邮件,但当我再次尝试时,我再次收到身份验证错误。
  • @Synchro 文档重定向到谷歌允许不太安全的应用程序和谷歌XOAUTH2机制无论如何......
  • 这是新的 请使用您的网络浏览器登录,然后重试。了解更多 at534 5.7.9 support.google.com/mail/answer/78754 m67sm4963214wmf.16 - gsmtp SMTP 错误:密码命令失败:534-5.7.9 请使用您的网络浏览器登录,然后重试。了解更多 at534 5.7.9 support.google.com/mail/answer/78754 m67sm4963214wmf.16 - gsmtp
  • 这不是新的。它只是不可见,因为您之前没有显示足够的调试输出。虽然很痛苦,但正确的解决方法是实现 xoauth2。如果您启用安全性较低的应用程序,您仍然需要按照说明操作并通过浏览器登录。