如何解决无法通过PHPMailer使用Gmail SMTP服务器发送电子邮件,出现错误:在端口587上提交邮件需要SMTP AUTH如何解决?
$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 protected]”; $mail->Password = “password”; $mail->SetFrom("[email protected]”); $mail->Subject = “Test”; $mail->Body = “hello”; $mail->AddAddress("[email protected]”);
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 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 = '[email protected]';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = '[email protected]';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('[email protected]');
$mail->AddReplyTo('[email protected]','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: [email protected]
SMTP server error: SMTP AUTH is required for message submission on port 587
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。