dpwuvfpu52502 2016-05-30 03:14
浏览 769
已采纳

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

When I trying to send a email using PHP SMTP email server, following error has occurred.

SMTP Error: Could not authenticate. Message could not be sent.

Mailer Error: SMTP Error: Could not authenticate.


Following is my code that I have used.

function supervisorMail(){

global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "***@gmail.com";
$mail->Password = "****";
$mail->SetFrom("***@gmail.com", "Employee Leave Management System");

$userID=$_SESSION['userID'];

$select_query = mysql_query("SELECT * FROM employee WHERE emp_id = '$userID'");
$select_sql = mysql_fetch_array($select_query);
$name=$select_sql['manager_name'];
var_dump($name);

$select_query1 = mysql_query("SELECT email FROM employee WHERE emp_id='$name'");
$select_sql1 = mysql_fetch_array($select_query1);
$email=$select_sql1['email'];
    var_dump($email);

$mail->Subject = "subject ";
$mail->Body = "something.";
$mail_to = $email;
$mail->AddAddress($mail_to);

if(!$mail->Send())
{
    echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}

echo "Message has been sent";

}

How can I fixed this error.

展开全部

  • 写回答

3条回答 默认 最新

  • douying2243 2016-05-30 04:06
    关注

    The error message is very clear "Could not authenticate". I would say you are correctly using the PHPMailer class, but just not filling in the correct gmail account details.

    I suppose that in your question the fields username and password look like

    $mail->Username = "@gmail.com";
    $mail->Password = "";
    

    just because you, obviously, don't want to share them, I would suggest to present them like this in the question

    $mail->Username = "********@gmail.com";
    $mail->Password = "********";
    

    So if you are using the correct username and password there are other two things to check

    1. Maybe your setting "Access for less secure apps" is turned off. After you login from the web you can turn it on from this page https://www.google.com/settings/u/0/security/lesssecureapps

    2. If that is On, it might be possible you have 2-Step Verification enabled in your gmail account. If this is the case, you need to create an App specific password.

    Follow this link for a step by step guide on how to do it http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置