无法发送消息。邮件程序错误:SMTP 错误:无法进行身份验证

发布时间:2021-03-07 11:11

伙计们,我也面临同样的问题,比如无法发送邮件错误,我在谷歌上搜索并尝试了所有方法,但仍然遇到同样的问题 请帮我解决我的错误

方法一:

<块引用>

我允许使用安全性较低的应用

方法二:

<块引用>

我输入了正确的电子邮件和密码

<?php 

use PHPMailer\PHPMailer\PHPMailer; 
use PHPMailer\PHPMailer\Exception; 

require 'vendor/autoload.php'; 

$mail = new PHPMailer(true); 

try { 
    $mail->SMTPDebug = 2;                                    
    $mail->isSMTP();                                             
    $mail->Host  = 'smtp.gmail.com;';                    
    $mail->SMTPAuth = true;                          
    $mail->Username = '****@gmail.com';              
    $mail->Password = '*******';                         
    $mail->SMTPSecure = 'tls';                           
    $mail->Port  = 587; 

    $mail->setFrom('*************', 'Name');         
    $mail->addAddress('*****************'); 
    $mail->addAddress('***********', 'Name'); 
    
    $mail->isHTML(true);                                 
    $mail->Subject = 'Subject'; 
    $mail->Body = 'HTML message body in <b>bold</b> '; 
    $mail->AltBody = 'Body in plain text for non-HTML mail clients'; 
    $mail->send(); 
    echo "Mail has been sent successfully!"; 
} catch (Exception $e) { 
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; 
} 

?> 
回答1

您的 Gmail 可能存在身份验证问题。在连接到 Gmail 之前,请确保您已完成以下步骤。

# Visit https://accounts.google.com
#
# 1. Click on [Security] menu
# 2. Scroll to section [Less secure app access] and set it to ON
# 3. Scroll to section [Signing in to Google] and set [Use your phone to sign in] to OFF 
# 4. Scroll to section [Signing in to Google] and set [2-step Verification] to OFF 
#