PHPmailer 无法使用正确的信息登录

PHPmailer unable to login with correct info

我的问题

我尝试通过 PHPMailer 发送邮件,但在发送邮件时出现以下错误:

 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials b4sm17820744wrv.42 - gsmtp
SMTP Error: Could not authenticate.

代码

        $mail->IsSMTP();
        $mail->Host = "tls://smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "tls";
        $mail->Username = "******@gmail.com";
        $mail->Password = "*********";
        $mail->Port = "587";
        $mail->SMTPDebug = 1;

我试过了

根据您的情况简单调整此代码:)

LINK

这里你主要做错的是:

$mail->SMTPDebug = 1;

这只会向您显示 SMTP 客户端(即 PHPMailer)在说什么,而不是 Gmail 在说什么;您需要将其设置为 2 才能看到服务器响应。最有可能的是 gmail 返回了这样的消息:

5.7.14 Please log in via your web browser and then try again

Gmail 对使用新机制登录非常挑剔,尤其是当您启用 "less secure" 应用程序时。所以当你说:

manually login to check if username and password are correct(they are)

这样做的行为将清除块并允许您的代码工作,因此这与您的代码无关。 PHPMailer 提供的 gmail 示例实际上与您已有的没有任何不同;基本设置与您已有的相同。

另请注意 this exact problem is covered in the PHPMailer troubleshooting guide,这应该始终是您寻找 PHPMailer 答案的第一个地方。