草庐IT

php - 无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误 : SMTP AUTH is required for message submission on port 587. 如何解决?

coder 2023-04-28 原文

我想通过 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

最佳答案

$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)连接。

关于php - 无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误 : SMTP AUTH is required for message submission on port 587. 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16048347/

有关php - 无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误 : SMTP AUTH is required for message submission on port 587. 如何解决?的更多相关文章

随机推荐