php PHPMailer - SMTP 错误:从我的服务器发送邮件时密码命令失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21937586/
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-25 04:33:47  来源:igfitidea点击:

PHPMailer - SMTP ERROR: Password command failed when send mail from my server

phpemailsmtpphpmailer

提问by Punniyam Moorthi

I have used phpmailer() concept to send mail to users from my shared server using php script, but I'm not able to send even though everything is right in my script according to phpmailer code.

我已经使用 phpmailer() 概念使用 php 脚本从我的共享服务器向用户发送邮件,但是即使根据 phpmailer 代码我的脚本中的所有内容都正确,我也无法发送。

My code like this:

我的代码是这样的:

  $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 = "[email protected]"; // My gmail username
  $mail->Password = "************"; // My Gmail Password
  $mail->SetFrom("[email protected]");
  $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;

}

Note:I have used " GMail " as my SMTP server and SMTPSecure is " ssl " and port is "465" and username & passwords are my GMail username & password

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

I used VPS shared server and I kept my php script on that server.

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

I think there is no problem in my php script and I don't know why it doesn't work.

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

I got the ERROR like this.

我得到了这样的错误。

  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.

Please give some solution for that.
Thanks in Advance.

请给出一些解决方案。
提前致谢。

Remember:I use Shared Server Name 'VPS.mydomain.com' and I want to use GMail as my SMTP server to send mail to users.

请记住:我使用共享服务器名称“VPS.mydomain.com”并且我想使用 GMail 作为我的 SMTP 服务器来向用户发送邮件。

回答by callmebob

A bit late, but perhaps someone will find it useful.

有点晚了,但也许有人会发现它很有用。

Links that fix the problem (you must be logged into google account):

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

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

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

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

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

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

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

Some explanation of what happens:

对发生的事情的一些解释:

This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).

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

To resolve I had to: (first time)

要解决我不得不:(第一次)

  • login to my account via web
  • view recent attempts to use the account and accept suspicious access: THIS LINK
  • disable the feature of blocking suspicious apps/technologies: THIS LINK
  • 通过网络登录到我的帐户
  • 查看最近尝试使用该帐户并接受可疑访问:此链接
  • 禁用阻止可疑应用程序/技术的功能:此链接

This worked the first time, but few hours later, probably because I was doing a lot of testing the problem reappeared and was not fixable using the above method. In addition I had to clear the captcha (the funny picture, which asks you to rewrite a word or a sentence when logging into any account nowadays too many times) :

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

  • after login to my account I went HERE
  • Clicked continue
  • 登录我的帐户后,我去了这里
  • 点击继续

Hope this helps.

希望这可以帮助。

回答by Sanya Snex

Use this:

用这个:

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

this link allow acces to google account

此链接允许访问谷歌帐户

UPDATE 19-05-2017:

2017 年 5 月 19 日更新:

These url you must to visit from the IP address that will be send email

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

回答by Sebastian Henzl

As others already suggested, you can enable the "less secure" applications or you can simply switch from sslto tls:

正如其他人已经建议的那样,您可以启用“不太安全”的应用程序,或者您可以简单地从 切换ssltls

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

When using tlsthere's no need to grant access for less secure applications, just make sure, IMAP is enabled.

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

回答by Ajitabh Ranjan

Solved the problem - PHPMailer - SMTP ERROR: Password command failed when send mail from my server

解决了问题 - PHPMailer - SMTP ERROR: Password command failed when send mail from my server

    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   = "[email protected]";  // GMAIL username
      $mail->Password   = "********";            // GMAIL password
      $mail->AddAddress('[email protected]', 'abc');
      $mail->SetFrom('[email protected]', '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!
}

Important:

重要的:

Go to google Setting and do 'less secure' applications enables. It will work. It Worked for Me.

转到谷歌设置并启用“不太安全”的应用程序。它会起作用。它对我有用。

回答by privacy

I face the same problem, and think that I do know why this happens.

我面临同样的问题,并认为我知道为什么会发生这种情况。

The gmail account that I use is normally used from India, and the webserver that I use is located in The Netherlands.

我使用的 gmail 帐户通常在印度使用,而我使用的网络服务器位于荷兰。

Google notifies that there was a login attempt from am unusualy location and requires to login from that location via a web browser.

Google 通知有人尝试从异常位置登录,并要求通过网络浏览器从该位置登录。

Furthermore I had to accept suspicious access to the gmail account via https://security.google.com/settings/security/activity

此外,我必须通过https://security.google.com/settings/security/activity接受对 gmail 帐户的可疑访问

But in the end my problem is not yet solved, because I have to login to gmail from a location in The Netherlands.

但最终我的问题还没有解决,因为我必须从荷兰的某个位置登录 gmail。

I hope this will help you a little! (sorry, I do not read email replies on this email address)

我希望这会对你有所帮助!(抱歉,我不阅读此电子邮件地址的电子邮件回复)

回答by Rafilson

For those who are still unable to get it working, try the following in addition to the method provided by @CallMeBob.

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

PHP.ini

配置文件

  1. Go to C:\xampp\php, edit php.inifile with notepad.
  2. Press CTRL+Fon your keyboard, input sendmail_pathon the search bar and click Find Nexttwice.
  3. Right now, you should be at the [mail munction]section.
  1. 转到C:\xampp\php,用记事本编辑php.ini文件。
  2. 按键盘上的CTRL+F,在搜索栏上输入sendmail_path并单击Find Next两次。
  3. 现在,您应该在[mail munction]部分。

Remove the semicolon for this line: sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Add a semicolon for this line: sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"

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

SendMail.ini

发送邮件文件

  • Go to C:\xampp\sendmail, edit sendmail.inifile with notepad
  • Change the following:
  • 转到C:\xampp\sendmail,用记事本编辑sendmail.ini文件
  • 更改以下内容:



Note:
** smtp_portmust tally with your those written in your php code.
** Remember to change your-gmail-usernameand your-gmail-passwordto whichever account you are using.
**

注意:
** smtp_port必须与你在你的 php 代码中写的一致。
** 请记住将your-gmail-usernameyour-gmail-password 更改为您使用的任何帐户。
**


Hope this helps! :)


希望这可以帮助!:)

回答by Vladimir

If you are G Suit user it can be solved by Administrator

如果您是 G Suite 用户,管理员可以解决

  1. Go to your Admin panel
  2. Type in top search bar ?Security? (Select Security with Shield icon)
  3. Open Basic settings
  4. Goto Less secure appssection
  5. Press: Go to settings for less secure apps ??
  1. 转到您的管理面板
  2. 输入顶部搜索栏?安全?(选择带盾牌的安全图标)
  3. 打开基本设置
  4. 转到不太安全的应用程序部分
  5. 按:转到安全性较低的应用程序的设置??

And now select one of Radio Button a) Disable access to less secure apps for all users (Recommended) b) Allow users to manage their access to less secure apps c) Enforce access to less secure apps for all users (Not Recommended)

现在选择单选按钮之一 a) 禁止所有用户访问不太安全的应用程序(推荐) b)允许用户管理他们对不太安全的应用程序的访问 c)强制所有用户访问不太安全的应用程序(不推荐)

Usually It does not working because of a)! And will start working immediately with c) option. b) – option will need more configuration for each user in GSuit

通常它不起作用是因为 a)!并将立即开始使用 c) 选项。b) – GSuit 中每个用户的选项需要更多配置

Hope it helps

希望能帮助到你

回答by Bhanu

This error is due to more security features of gmail..

此错误是由于 gmail 的更多安全功能所致。

Once this error is generated...Please login to your gmail account..there you can find security alert from GOOGLE..follow the mail...check on click for less secure option..Then try again phpmailer..

一旦生成此错误...请登录您的 Gmail 帐户...在那里您可以找到来自 GOOGLE 的安全警报...关注邮件...点击点击以获取不太安全的选项...然后再试一次 phpmailer...

回答by Bhanu

Your mail won't be sent online unless you complete the two-step verification for your g-mail account and use that password.

除非您完成 g-mail 帐户的两步验证并使用该密码,否则您的邮件不会在线发送。