首页 文章

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

提问于
浏览
44

我已经使用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 = "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;

}

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

我使用VPS共享服务器,我将PHP脚本保存在该服务器上 .

我认为我的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.

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

Remember: 我使用共享服务器名称'VPS.mydomain.com',我想使用GMail作为我的SMTP服务器向用户发送邮件 .

8 回答

  • 1

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

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

    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

    Some explanation of what happens:

    这个问题可能是由于“不太安全”的应用程序尝试使用电子邮件帐户造成的(这是根据谷歌的帮助,不确定他们如何判断什么是安全的,什么不是)或者如果你试图在一个时间点登录行或者如果你改变国家(例如使用VPN,将代码移动到不同的服务器或实际上尝试从世界的不同地方登录) .

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

    • 通过网络登录我的帐户

    • 查看最近尝试使用该帐户并接受可疑访问:THIS LINK

    • 禁用阻止可疑应用/技术的功能:THIS LINK

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

    • 登录我的帐户后我去了HERE

    • 点击继续

    希望这可以帮助 .

  • 7

    用这个:

    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

  • 21

    正如其他人已经建议的那样,您可以启用"less secure"应用程序,或者只需从 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 .

  • 6

    解决了问题 - PHPMailer - SMTP ERROR:从我的服务器发送邮件时密码命令失败

    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!
    }
    

    重要:

    转到谷歌设置,并启用“不太安全”的应用程序 . 它会工作 . 它为我工作 .

  • 2

    我面临同样的问题,并认为我确实知道为什么会这样 .

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

    Google会通知您从异常位置进行登录尝试,并且需要通过网络浏览器从该位置登录 .

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

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

    我希望这会对你有所帮助! (对不起,我没有看过这个电子邮件地址的电子邮件回复)

  • 142

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

    • 转到“管理”面板

    • 键入顶部搜索栏« Security »(选择带盾牌的安全图标)

    • 打开 Basic settings

    • 转到 Less secure apps 部分

    • 按:转到安全性较低的应用>>的设置

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

    通常它因a)而无法正常工作!并将立即开始使用c)选项 . b) - 选项将需要为GSuit中的每个用户进行更多配置

    希望能帮助到你

  • 0

    此错误是由于gmail的更多安全功能 .

    生成此错误后...请登录您的Gmail帐户..您可以从GOOGLE找到安全提醒 . 关注邮件...点击查看安全性较低的选项..然后再试一次phpmailer ..

  • 0

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

    PHP.ini

    • 转到 C:\xampp\php ,用记事本编辑 php.ini 文件 .

    • 按键盘上的 CTRL+F ,在搜索栏上输入 sendmail_path ,然后单击 Find Next 两次 .

    • 现在,你应该在 [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 =你的gmail密码

    • force_sender=your-gmail-username@gmail.com

    Note:
    **smtp_port必须与您的PHP代码中编写的那些一致 .
    **请记住将您的-gmail-username和your-gmail-password更改为您使用的任何帐户 .
    **

    希望这可以帮助! :)

相关问题