无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误:在端口 587 上提交邮件需要 SMTP AUTH.如何解决? [英] Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?

查看:17
本文介绍了无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误:在端口 587 上提交邮件需要 SMTP AUTH.如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to send an email using Gmail SMTP server through PHP Mailer.

这是我的代码

<?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";
 }

上面的代码已经过测试并且对我有用.

This code above has been tested and worked for me.

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

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

Also make sure you don't have two step verification switched on for that account as that can cause problems also.

更新

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

You could try changing $mail->SMTP to:

$mail->SMTPSecure = 'tls';

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

It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

这篇关于无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误:在端口 587 上提交邮件需要 SMTP AUTH.如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆