phpmailer 用户名和密码不被接受

phpmailer username & password not accepted

我在使用 PHPMailer 时从未收到过以下错误:

2021-11-23 03:10:20 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted.
2021-11-23 03:10:20 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted.

我确实输入了正确的用户名和密码。这是代码:

<?php

use phpmailer\PHPMailer\PHPMailer;
require_once '../phpmailer/PHPMailer.php';
require_once '../phpmailer/SMTP.php';
require_once '../phpmailer/Exception.php';

$mail = new PHPMailer;    
$mail->SMTPDebug = 4;  
                    
$mail->isSMTP();                                        
$mail->Host = 'smtp.gmail.com';                             
$mail->SMTPAuth = true;       
$mail->Username = 'someemail@gmail.com';
$mail->Password = 'somepassword';
$mail->SMTPSecure = 'tls';                                                       
$mail->Port = 25;    
                    
$mail->setFrom('someemail@gmail.com', 'Mailer');
$mail->addAddress('testemail@yahoo.com', 'Receiver');      
$mail->addReplyTo('someemail@gmail.com', 'No Reply');
$mail->Body = $body;

$mail->isHTML(true);

if($mail->send()){
  echo "Email sent";
} else {
  echo "Error: There was an error sending email " . $mail->ErrorInfo;;
}

?>

我已尝试使用此处找到的答案:Username and Password not accepted

这里:Username and Password not accepted

运气不好。

我使用的是正确的用户名和密码。

我将端口更改为 465,但出现以下错误:

2021-11-23 03:20:20 Connection: opening to smtp.gmail.com:465, timeout=300, options=array()<br>
2021-11-23 03:20:20 Connection: opened<br>
2021-11-23 03:20:30 SMTP INBOUND: &quot;&quot;<br>
2021-11-23 03:20:30 SERVER -&gt; CLIENT: <br>
2021-11-23 03:20:30 Connection: closing due to error<br>
2021-11-23 03:20:30 Connection: closed<br>
SMTP connect() failed. 
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting<br>
Error: There was an error sending email SMTP connect() failed. 
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我做错了什么?

我可以通过这里解决我的问题:

https://myaccount.google.com/u/0/lesssecureapps

并将允许安全应用设置为开。

完成此更新后,我就可以发送电子邮件了。