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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37524275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:16:25  来源:igfitidea点击:

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

phpemail

提问by Chathurika

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

当我尝试使用 PHP SMTP 电子邮件服务器发送电子邮件时,发生了以下错误。

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

Mailer Error: SMTP Error: Could not authenticate.

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



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.

我该如何修复这个错误。

回答by Igor S Om

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.

错误消息非常清楚“无法验证”。我会说您正确使用了 PHPMailer 类,但只是没有填写正确的 gmail 帐户详细信息。

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 enabledin your gmail account. If this is the case, you need to create an App specific password.

  1. 也许您的设置“访问不太安全的应用程序”已关闭。从网络登录后,您可以从此页面打开它 https://www.google.com/settings/u/0/security/lesssecureapps

  2. 如果启用,则您的 Gmail 帐户可能启用两步验证。如果是这种情况,您需要创建应用程序专用密码。

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

按照此链接获取有关如何操作的分步指南 http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-或-IMAP.htm

回答by Ravi Hirani

As mentioned in comment, you need to enter valid gmail idand password.

如评论中所述,您需要输入有效的gmail idpassword

Please refer below demo code.

请参考下面的演示代码。

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 2;    // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;   // Enable SMTP authentication
$mail->Username = '*****[email protected]';  // SMTP username
$mail->Password = 'ma****02';    // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('ravi******@gmail.com', 'Mailer'); // Add set from id
$mail->addAddress('[email protected]', 'Ravi Hirani');     // Add a recipient
//$mail->addAddress('[email protected]');               // Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
//$mail->isHTML(true);   // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

回答by Mike Volmar

I ran into this error message and wanted to post an answer in case it can help someone else. I have phpmailer installed with composer and am running php 7. The implementation of the phpmailer script that threw the error is in a php object method. The password variable, set in a config file elsewhere, needed to be declared as a global in the object context otherwise it did not contain the actual password and thus caused the authentication error..

我遇到了这个错误消息,想发布一个答案,以防它可以帮助其他人。我用composer安装了phpmailer并且正在运行php 7。抛出错误的phpmailer脚本的实现是在一个php对象方法中。在其他地方的配置文件中设置的密码变量需要在对象上下文中声明为全局变量,否则它不包含实际密码,从而导致身份验证错误。

        global $password; // <- this added to make script work

        $mail = new PHPMailer(true);   
        try {
            $mail->isSMTP();            

            // Set mailer to use SMTP               
            $mail->Host = '*******.com'; 
            $mail->SMTPAuth = true;   
            $mail->Username = 'no-reply@*******.com'; 
            $mail->Password = $password;     
            $mail->SMTPSecure = 'tls';      
            $mail->Port = 587;        
            $mail->setFrom('no-reply@*******.com', '******* - DO NOT REPLY'); 

            //$mail->isHTML(true);  
            $mail->AddAddress($user->email);

            $mail->Subject = "*******";
            $mail->Body    = "*******";

            $mail->send();

        } catch (Exception $e) {
            echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
        } 

回答by Victor Rusu

In my case it was because I didn't turn on the Less secure app accessfrom here: https://myaccount.google.com/lesssecureapps

就我而言,这是因为我没有从这里打开不太安全的应用程序访问https: //myaccount.google.com/lesssecureapps

回答by user10347452

Please unable your less secure app and then Try to comment this line

请无法使用您不太安全的应用程序,然后尝试评论此行

//$mail->IsSMTP();

//$mail->IsSMTP();

I hope it will work!

我希望它会起作用!