【发布时间】:2026-02-01 12:20:03
【问题描述】:
我想知道问题出在哪里,因为我遇到了身份验证错误。
- 我已经有几个 gmail 帐户了,
- 我为自己的网站新建了一个,
- 我将“帐户和导入”中的新电子邮件添加到我的旧 gmail 帐户。
- 我将新的电子邮件用户名和密码复制到 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
【问题讨论】:
-
将您的代码基于the gmail example provided with PHPMailer,而不是一些旧的和过时的副本。使用自动装载机。 Read the docs.
-
如果您设置
SMTPDebug = 2(并忽略代码中对其的错误描述),您将看到gmail拒绝您登录的原因。