我正在尝试通过phpmailer类发送电子邮件。但我发现此问题:SMTP Error: Could not authenticate.
在我的Gmail帐户中,我看到了一封邮件:sign in attempt prevented
function send_mail($email,$message,$subject)
{
require_once('mailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->AddAddress($email);
$mail->Username="mymail@gmail.com";
$mail->Password="password";
$mail->SetFrom('mymail@gmail.com','Coding Cage');
$mail->AddReplyTo("mymail@gmail.com","Coding Cage");
$mail->Subject = $subject;
$mail->MsgHTML($message);
$mail->Send();
}
由于我的收件箱中有消息,因此可能是错误或我如何提供权限:Sign-in attempt prevented
答案 0 :(得分:1)
与@Synchro所说的一样,您使用的是旧版本的PHPMailer。使用最新的here。
您还提到了一封电子邮件Sign in attempt prevented
登陆您的收件箱。尝试点击Review your devices now
,应该有一个显示Allow this sign in attempt
或类似内容的按钮或链接。然后,尝试再次运行代码以查看它是否有效。
如果没有,那么,如果您的Google帐户中已启用2FA,请尝试将其关闭。
希望有所帮助:)