首页 文章

PHPMailer中的“SMTP错误:无法进行身份验证”

提问于
浏览
44

我在一个简单的脚本中使用PHPMailer通过Gmail发送电子邮件,我得到一个“未知错误”(至少对我而言!):

SMTP错误:无法进行身份验证 . 错误:SMTP错误:无法进行身份验证 . SMTP服务器错误:5.7.1不接受用户名和密码 . 要了解详情,请访问535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 p38sm2467302ybk.16

我已经阅读了有关配置OpenSSL For SSL / TLS Connections的信息,我做到了 . Apache和PHP配置正确(OpenSSL扩展在PHP中运行,mod_ssl在Apache 2.2.16中运行) .

这是PHP脚本:

<?php
  require_once ("PHPMailer\class.phpmailer.php");
  $Correo = new PHPMailer();
  $Correo->IsSMTP();
  $Correo->SMTPAuth = true;
  $Correo->SMTPSecure = "tls";
  $Correo->Host = "smtp.gmail.com";
  $Correo->Port = 587;
  $Correo->UserName = "foo@gmail.com";
  $Correo->Password = "gmailpassword";
  $Correo->SetFrom('foo@gmail.com','De Yo');
  $Correo->FromName = "From";
  $Correo->AddAddress("bar@hotmail.com");
  $Correo->Subject = "Prueba con PHPMailer";
  $Correo->Body = "<H3>Bienvenido! Esto Funciona!</H3>";
  $Correo->IsHTML (true);
  if (!$Correo->Send())
  {
    echo "Error: $Correo->ErrorInfo";
  }
  else
  {
    echo "Message Sent!";
  }
?>

用户名和密码都没问题,我试过Thunderbird,没有任何问题 . 我还使用了SSL身份验证和端口465,获得了相同的错误 .

14 回答

  • 21

    试试这个:

    $ Correo-> Username = "foo@gmail.com";

    我测试了它,它的工作完美,没有其他变化

  • 54

    我遇到了这个问题 . 为了使它工作,我不得不去myaccount.google.com - > "Sign-in & security" - > "Apps with account access",并将"Allow less secure apps"转到"ON"(靠近页面底部) .

    enter image description here

  • 7

    这是GMail问题

    阅读此Google帮助(https://support.google.com/mail/answer/14257?p=client_login&rd=1

    • 打开网络浏览器并在http://mail.google.com/mail登录Gmail . 如果您看到单词验证请求,请在扭曲的图片中键入字母并完成登录 .

    • 关闭浏览器并尝试再次访问电子邮件客户端中的邮件 .

    • 如果您仍然遇到问题,请访问http://www.google.com/accounts/DisplayUnlockCaptcha并使用您的Gmail用户名和密码登录 . 如有必要,请在扭曲的图片中输入字母 .

    • 单击继续 .

    • 重新启动邮件客户端,然后再次尝试访问电子邮件客户端中的邮件 .

    SMTP Error: Could not authenticate

    Gmail issue

  • 2

    我收到了同样的错误,在我的情况下,它是密码 . 我的密码有特殊字符,如果您提供密码而不转义特殊字符,则错误将继续显示 . 例如, $mail->Password = " por$ch3"; 有效但无法使用上述代码 . 解决方案应如下所示: $mail->Password = "por\$ch3"; 注意我在密码中放置美元字符之前的反斜杠 . 如果你有一个使用特殊字符的密码,这应该工作

  • 2

    我收到此错误是因为密码中有百分号 .

  • 0

    对我来说,我的密码字段中有一个特殊字符,我把它写成$ mail-> Password =“por $ ch3”,它适用于gmail smpt服务器但不适用于其他服务器;所以我只是将双引号更改为单引号,它对我有用 . $ mail-> Password ='por $ ch3';

  • 0

    如果您在发送电子邮件时仍然遇到错误,请使用相同的错误消息 . 试试这个:

    $mail->SMTPSecure = 'tls';
    $mail->Host = 'smtp.gmail.com';
    

    就行前:

    $send = $mail->Send();
    

    或者在其他意义上,在调用Send()函数之前 .

    经过测试和工作 .

  • 0

    我遇到了同样的问题并完成了所有提示,包括Gmail设置(例如安全性较低的应用访问),没有运气 . 但最后当我将密码更改为不同的东西时,出于某种原因它才起作用!仅供参考,初始密码没有任何特殊字符 .

  • 0

    [解决了] . 在Wordpress中配置 WP-Mail-SMTP 插件时遇到了同样的错误 .

    即使我已经“三次检查”设置和登录凭据,并且能够使用浏览器手动登录,问题仍将存在 .

    您可以采取一系列步骤来解决此问题 .

    • 为您要使用的Gmail帐户创建新密码

    • Google Security settings中启用安全性较低的应用

    • 使用Display Unlock Captcha page授予您的应用或网站登录Gmail的权限 . 单击 Continue 或按照说明操作 .

    • 使用应用或网站登录 . 适用于我的smtp设置是1)SMTP主机:smtp.gmail.com 2)SMTP端口:587 3)加密:TLS 4)身份验证:SMTP身份验证5)用户名:example@gmail.com 6)密码:examplesecret

  • 2

    我在身份验证时遇到了同样的问题 . 修复是设置两步验证并为设备创建应用程序专用密码(阻止设备的错误消息将显示在您的帐户设置 - >“通知和警报”,如果您的问题与我的相同)

  • 20

    这是selinux的问题 . 我刚刚更新了/ etc / selinux / config文件 SELINUX=permissive 中的以下给定部分(之前是SELINUX =强制执行) . 然后通过重新启动重新启动系统现在邮件没有任何麻烦 . 组态

    From Email Address : [noreply@yourdomain.com]
    From Name : [your domain name]
    SMTP Host   : smtp.gmail.com
    Type of Encryption : SSL
    SMTP Port : 465
    SMTP Authentication  : YES
    Username : [your mail id]
    Password : [your password]
    
  • 0

    SMTP Error: could not authenticate 我遇到了同样的问题 . 下列故障排除步骤帮助我 .

    • 我在我的Gmail帐户中关闭了双因素身份验证 .

    • 我允许安全性较低的应用访问我的Gmail帐户 . 为了使它工作,我不得不去 myaccount.google.com - > Sign-in & security - > Apps with account access ,然后将 Allow less secure apps 转到 ON (靠近页面底部) .

    • 在这一步,当我尝试注册用户时,我会得到同样的错误 . Google会向我发送一条警告消息,告知有人有我的密码并且登录被阻止 .

    • Gmail将为您提供选项 . 您可以单击该活动是否属于您自己 . 单击该活动属于您的选项 .

    • 再次尝试注册 . 它现在应该工作 .

  • 3

    您的代码没有问题 .

    按照以下两个简单步骤从phpmailer发送电子邮件 .

    如果已启用,则必须禁用Google帐户的两步验证设置 . 开启允许访问不太安全的应用程序 .

  • 3

    另一篇文章解决问题是正确的,但如果启用了两步验证,则无法解决该问题 . 然后,不提供允许安全性较低的应用程序的选项 . 以下是如何做到的答案:

    a. Go to the URL of `https://myaccount.google.com/` and click `Sing-in and security`
    
    b. Click on the app password.
    

    enter image description here

    You will reach a page like this,
    

    enter image description here

    c. Create name of your app and generate a password for the respective app.  
    
    d. Use that password acquired here inside the app.
    

    这应该解决问题 .

相关问题