PHPMailer - 当我从我的服务器发送电子邮件时,出现SMTP错误:密码命令失败

77

我使用phpmailer()概念通过php脚本从我的共享服务器向用户发送邮件,但尽管根据phpmailer代码一切都正确,但我仍然无法发送邮件。

我的代码如下:

  $message = " This is testing message from my server";

  $mail = new PHPMailer(); // create a new object
  $mail->IsSMTP(); // enable SMTP
  $mail->Host = "smtp.gmail.com";
  $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
  $mail->SMTPAuth = true; // authentication enabled
  $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
  $mail->Port = 465; // or 587
  $mail->IsHTML(true);
  $mail->Username = "moorthi.mrk10@gmail.com"; // My gmail username
  $mail->Password = "************"; // My Gmail Password
  $mail->SetFrom("moorthi.mrk10@gmail.com");
  $mail->Subject = "Test Mail from my Server";
  $mail->Body = $message;
  $mail->AddAddress($email);
   if($mail->Send())
      {
    print json_encode("SUCCESS");
}
else
{
    echo "Mailer Error: " . $mail->ErrorInfo;
    
}

注:我使用“GMail”作为我的SMTP服务器,SMTPSecure是“ssl”,端口是“465”,用户名和密码是我的GMail用户名和密码。

我使用的是VPS共享服务器,并将我的php脚本放在该服务器上。

我认为我的php脚本没有问题,但我不知道为什么它不起作用。

我收到了如下错误信息。

  2014-02-21 12:30:11   CLIENT -> SERVER: EHLO jkcorporates.com
  2014-02-21 12:30:11   CLIENT -> SERVER: AUTH LOGIN
  2014-02-21 12:30:11   CLIENT -> SERVER: bW9vcnRoaS5tcmsxMEBnbWFpbC5jb20=
  2014-02-21 12:30:11   CLIENT -> SERVER: OTk0MTI0MTE0MA==
  2014-02-21 12:30:11   SMTP ERROR: Password command failed: 534-5.7.14 
  534-5.7.14 i-_eumA> Please log in via your web browser and then try again.
  534 5.7.14 54 k76sm17979938yho.18 - gsmtp
  2014-02-21 12:30:11   CLIENT -> SERVER: QUIT
  " The ERROR is  "  SMTP connect() failed.

请给出解决方法。

注意:我使用共享服务器名称“VPS.mydomain.com”,并且想要使用Gmail作为我的SMTP服务器向用户发送邮件。


1
请查看此答案:http://stackoverflow.com/a/41793970/5275436,并尝试给出的步骤。 - Govinda P
13个回答

232

可能有些晚了,但或许还有人会觉得有用。

解决问题的链接(您必须登录Google帐户):

https://security.google.com/settings/security/activity?hl=en&pli=1

https://www.google.com/settings/u/1/security/lesssecureapps

https://accounts.google.com/b/0/DisplayUnlockCaptcha

问题产生的一些解释:

此问题可能由“不太安全”的应用程序尝试使用电子邮件帐户引起(这是根据Google的帮助信息,不确定他们如何判断什么是安全的和什么是不安全的),或者您尝试连续登录几次,或者更改国家(例如使用VPN,将代码移动到不同的服务器或从世界其他地方尝试登录)。

要解决问题,我必须执行以下操作:(第一次)

  • 通过Web登录我的帐户
  • 查看最近尝试使用该帐户并接受可疑访问:此链接
  • 禁用阻止可疑应用程序/技术的功能:此链接

这在第一次解决了问题,但几小时后,可能是因为我进行了大量测试,问题重新出现,并且无法使用上述方法修复。此外,我还必须清除验证码(有趣的图片,在现在登录任何帐户时过多次要求您重新编写单词或句子):

  • 登录我的账户后,我点击此处
  • 然后点击继续

1
在搜索了一段时间后,我找到了这个页面并且对我有用。请问您是如何找到这个链接的:https://accounts.google.com/b/0/DisplayUnlockCaptcha? - Yoones Mehdian
1
@YunessMehdian 那是很久以前的事了,所以我不记得我是怎么到那里的。我当时在做很多研究来解决这个问题,可能是无意中发现了验证码页面。 - callmebob
1
没问题,谢谢! - D.Pacheco
1
在000webhost上,不安全的应用程序和显示解锁验证码对我有效。 - Yash Karanke
1
它工作得如此惊人!我无法表达感激之情。愿真主指引并保佑您走在正确的道路上。 - Fechang Fuchang
显示剩余9条评论

30

使用这个:

https://www.google.com/settings/u/1/security/lesssecureapps
https://accounts.google.com/b/0/DisplayUnlockCaptcha
https://security.google.com/settings/security/activity?hl=en&pli=1

这个链接允许访问谷歌账户。

更新于19-05-2017:

您必须从将发送电子邮件的IP地址访问这些URL。


我正在使用亚马逊EC2,你能告诉我如何完成这个过程吗? - Jay Acharya

7

正如其他人所建议的那样,您可以启用“不安全”的应用程序,或者您可以从ssl切换到tls

$mailer->Host = 'tls://smtp.gmail.com';
$mailer->SMTPAuth = true;
$mailer->Username = "xxx@gmail.com";
$mailer->Password = "***";
$mailer->SMTPSecure = 'tls';
$mailer->Port = 587;

使用tls时,无需授权不安全应用程序的访问权限,只需确保启用了IMAP。


7
解决了问题 - PHPMailer - 在我的服务器上发送邮件时出现SMTP ERROR: Password command failed
    require_once('class.phpmailer.php');
    include("class.smtp.php"); 
    $nameField = $_POST['name'];
    $emailField = $_POST['email'];
    $messageField = $_POST['message'];
    $phoneField = $_POST['contactno'];
    $cityField = $_POST['city'];

$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch

$mail->IsSMTP(); // telling the class to use SMTP

$body .= $nameField;

try {
     //$mail->Host       = "mail.gmail.com"; // SMTP server
      $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
      $mail->SMTPAuth   = true;                  // enable SMTP authentication
      $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
      $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
      $mail->Port       = 465;   // set the SMTP port for the GMAIL server
      $mail->SMTPKeepAlive = true;
      $mail->Mailer = "smtp";
      $mail->Username   = "xxxxx@gmail.com";  // GMAIL username
      $mail->Password   = "********";            // GMAIL password
      $mail->AddAddress('sendto@gmail.com', 'abc');
      $mail->SetFrom('xxxxxx@gmail.com', 'def');
      $mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
      $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
      $mail->MsgHTML($body);
      $mail->Send();
      echo "Message Sent OK</p>\n";
      header("location: ../test.html");
} catch (phpmailerException $e) {
      echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
      echo $e->getMessage(); //Boring error messages from anything else!
}

重要提示:

前往 Google 设置,启用“较不安全的应用程序”。这样就可以正常工作了。这对我有用。


4
  1. 使用网页浏览器登录您的 Gmail 帐户。

  2. 单击此链接以启用应用程序访问您的帐户:https://accounts.google.com/b/0/DisplayUnlockCaptcha

  3. 单击“继续”按钮完成该步骤。

  4. 现在再尝试从您的 PHP 脚本发送电子邮件,应该可以了。


2
我遇到了同样的问题,我认为我知道为什么会发生这种情况。
我平常使用的gmail帐户是在印度使用的,而我使用的Web服务器位于荷兰。
谷歌通知我有一个登录尝试来自不寻常的地点,并需要通过Web浏览器从该位置登录。
此外,我必须接受通过https://security.google.com/settings/security/activity访问我的Gmail帐户的可疑行为。
但最终我的问题还没有得到解决,因为我必须从荷兰的某个位置登录Gmail。
我希望这能对你有所帮助!(抱歉,我不阅读此电子邮件地址上的邮件回复)

2

您需要使用应用程序密码。

访问此链接以查看如何创建应用程序密码。


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。仅有链接的答案如果链接页面发生更改可能会变得无效。 - kadamb

2

万一有人像我一样找到这里。

尽管我已经启用了对我的Google帐户的不安全访问,但在我的情况下,它仍然拒绝发送电子邮件,并抛出了SMTP错误:密码命令失败:534-5.7.14。

(解决方案在https://know.mailsbestfriend.com/smtp_error_password_command_failed_5345714-1194946499.shtml中找到)

步骤:

  1. log into your google account

  2. Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha and click continue to enable.

  3. Setup your phpmailer as smtp with ssl:

    $mail = new PHPMailer(true);
    
    $mail->CharSet  ="utf-8";
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; // or 0 for no debuggin at all        
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; 
    $mail->SMTPAuth   = true;
    $mail->Username = 'yourgmailaccount';
    $mail->Password = 'yourpassword';
    

还有其他需要的$mail对象属性。

希望能对某些人有所帮助!!


1

对于那些仍然无法使其正常工作的人,请尝试除了@CallMeBob提供的方法外,还要尝试以下方法。

PHP.ini

  1. 进入 C:\xampp\php 目录,使用记事本编辑 php.ini 文件。
  2. 按下键盘上的 CTRL+F 键,在搜索栏中输入 sendmail_path 并点击两次 查找下一个
  3. 现在,您应该在[mail munction]部分。

取消此行的分号: sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
为此行添加分号: sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

SendMail.ini

  • 转到C:\xampp\sendmail,使用记事本编辑sendmail.ini文件
  • 更改以下内容:
  • smtp_server=smtp.gmail.com
  • smtp_port=465
  • auth_username=your-gmail-username@gmail.com
  • auth_password=your-gmail-password
  • force_sender=your-gmail-username@gmail.com


注意:
** smtp_port 必须与您在 PHP 代码中编写的端口号相符。
** 记得将 your-gmail-usernameyour-gmail-password 更改为您正在使用的帐户。
**


希望这可以帮助! :)


1
如果您是G Suit用户,管理员可以解决此问题。
1. 进入您的管理面板。 2. 在顶部搜索栏中键入“安全”(选择带有盾牌图标的“Security”)。 3. 打开“基本设置”。 4. 转到“较不安全的应用程序”部分。 5. 点击“前往较不安全的应用程序设置 ››”。
现在选择一个单选按钮: a) 禁用所有用户对较不安全的应用程序的访问(推荐) b) 允许用户管理其对较不安全的应用程序的访问 c) 强制所有用户访问较不安全的应用程序(不推荐)
通常情况下,由于选项a),它无法正常工作。使用选项c)将立即开始工作。对于选项b),需要为GSuit中的每个用户进行更多配置。
希望这可以帮助您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接