无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误:在端口 587 上提交邮件需要 SMTP AUTH。如何解决?

新手上路,请多包涵

我想通过 PHP Mailer 使用 Gmail SMTP 服务器发送电子邮件。

这是我的代码

<?php
require_once('class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername@gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;

$mail->From = 'MyUsername@gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail@gmail.com');
$mail->AddReplyTo('phoenixd110@gmail.com', 'Information');

$mail->IsHTML(true);
$mail->Subject    = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$mail->Body    = "Hello";

if(!$mail->Send())
{
  echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
  echo "Message sent!";
}
?>

但我收到以下错误

Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail@gmail.com

SMTP server error: SMTP AUTH is required for message submission on port 587

我的域名是 vatandesign.ir

原文由 Mohammad Masoudian 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$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->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

 if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
 } else {
    echo "Message has been sent";
 }

上面的代码已经过测试并为我工作。

可能是您需要 $mail->SMTPSecure = 'ssl';

还要确保您没有为该帐户打开两步验证,因为这也会导致问题。

更新

您可以尝试将 $mail->SMTP 更改为:

 $mail->SMTPSecure = 'tls';

值得注意的是,一些 SMTP 服务器会阻止连接。某些 SMTP 服务器不支持 SSL (或 TLS )连接。

原文由 andrew-caulfield 发布,翻译遵循 CC BY-SA 3.0 许可协议

我找到了一个解决方案并且它正在工作。

基本设置:

 $mail->IsHTML(true);
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'user@gmail.com';
//Password to use for SMTP authentication
$mail->Password = 'PASSWORD_HERE';
//Set who the message is to be sent from
$mail->setFrom('email@email.ext', 'From where it is sent');
//Set an alternative reply-to address
$mail->addReplyTo('email@email.ext', 'Reply To');
//Set who the message is to be sent to
$mail->addAddress('email@email.ext');
//Set the subject line
$mail->Subject = 'Email subject';
// Body message
$mail->Body = '
  <h1>Message details</h1>
';
// Send message
if( $mail->send()  ) {
    echo "Sent"
} else {
    echo "Not Sent";
}

如果您已经尝试了所有方法,请尝试这样做:

  1. 禁用阻止可疑应用程序/技术的功能 https://www.google.com/settings/u/1/security/lesssecureapps
  2. 清除验证码 https://accounts.google.com/b/0/DisplayUnlockCaptcha

做测试。

以下是一些帮助我解决此问题的感兴趣的文章:

https://support.google.com/a/thread/108782439/smtp-error-password-command-failed-535-5-7-8-username-and-password-not-accepted?hl=en&msgid=108963583

https://bobcares.com/blog/phpmailer-smtp-error-password-command-failed/

原文由 Learning and sharing 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题