【问题标题】:SMTP Error: Could not authenticate. Message could not be sent. Mailer Error: SMTP Error: Could not authenticateSMTP 错误:无法验证。无法发送消息。邮件程序错误:SMTP 错误:无法验证
【发布时间】:2016-09-28 04:50:10
【问题描述】:

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

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

邮件程序错误:SMTP 错误:无法验证。


以下是我使用过的代码。

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";

}

我该如何解决这个错误。

【问题讨论】:

标签: php email


【解决方案1】:

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

我想在您的问题中,用户名和密码字段看起来像

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

只是因为你显然不想分享它们,我建议在问题中这样呈现它们

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

所以如果你使用了正确的用户名和密码,还有另外两件事需要检查

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

  2. 如果开启,您的 gmail 帐户可能启用了两步验证。如果是这种情况,您需要创建一个应用专用密码。

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

【讨论】:

  • 我添加了正确的用户名和密码,但问题仍然存在
  • @Chathurika 我会尝试使用我自己的帐户凭据,看看会发生什么...很快就会通知您
  • 它适用于我在 php 5.4 上,但在 php 5.5 或更高版本中连接失败。你的php版本是多少?您是否在您的 gmail 设置中启用了 POP 连接(这也将启用 smtp)
  • 我也用过php 5.4版
  • 我没有使用启用POP连接,效果如何?
【解决方案2】:

请禁用您不太安全的应用 然后 尝试评论这一行

//$mail->IsSMTP();

我希望它会起作用!

【讨论】:

    【解决方案3】:

    正如评论中提到的,您需要输入有效的gmail id密码

    请参考下面的演示代码。

    <?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 = '*****001@gmail.com';  // 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('er.ravihirani@gmail.com', 'Ravi Hirani');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');
    //$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';
    }
    

    【讨论】:

      【解决方案4】:

      您可以尝试使用谷歌应用密码进行身份验证:

      【讨论】:

        【解决方案5】:

        我遇到了这个错误消息,想发布一个答案,以防它可以帮助其他人。我已经用 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;
                } 
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-10-27
            • 2020-12-08
            • 2015-07-30
            • 2012-09-29
            • 2016-01-16
            • 2018-07-17
            相关资源
            最近更新 更多