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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16048347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:26:53  来源:igfitidea点击:

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?

phpsmtpgmailphpmailer

提问by Mohammad Masoudian

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

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

this is my code

这是我的代码

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

but I receive this following error

但我收到以下错误

Mailer Error: SMTP Error: The following recipients failed: [email protected]

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

my domain is vatandesign.ir

我的域名是vatandesign.ir

回答by andrew-caulfield

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

This code above has been tested and worked for me.

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

It could be that you needed $mail->SMTPSecure = 'ssl';

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

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

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

UPDATED

更新

You could try changing $mail->SMTP to:

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

$mail->SMTPSecure = 'tls';

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

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

回答by Evan Butler

So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.

所以我刚刚解决了我自己的“SMTP 连接失败”错误,我想发布解决方案,以防万一它可以帮助其他人。

I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.

我使用了 PHPMailer 示例 gmail.phps 文件中给出的 EXACT 代码。它在我使用 MAMP 时很简单,然后一旦我将它移到我的个人服务器上,我就收到了 SMTP 连接错误。

All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.

我读过的所有 Stack Overflow 答案以及来自 PHPMailer 的所有故障排除文档都说这不是 PHPMailer 的问题。这是服务器端的设置问题。我尝试了不同的端口(587、465、25),我尝试了“SSL”和“TLS”加密。我检查了我的 php.ini 文件中是否启用了 openssl。我检查了没有防火墙问题。一切都检查了,仍然没有。

The solution was that I had to remove this line:

解决方案是我必须删除这一行:

$mail->isSMTP();

Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.

现在一切正常。我不知道为什么,但它有效。我的其余代码是从 PHPMailer 示例文件中复制和粘贴的。

回答by Tim Carr

Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.

还值得注意的是,如果您启用了两因素身份验证,则需要设置一个应用程序特定密码来代替您的电子邮件帐户的密码。

You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

您可以按照以下说明生成应用程序专用密码:https: //support.google.com/accounts/answer/185833

Then set $mail->Passwordto your application specific password.

然后设置$mail->Password为您的应用程序特定密码。

回答by Pr Shadoko

It seems that your server fails to establish a connection to Gmail SMTP server. Here are some hints to troubleshoot this: 1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini). 2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config. I hope you'll sort this out quickly!

您的服务器似乎无法与 Gmail SMTP 服务器建立连接。以下是解决此问题的一些提示:1) 检查您的 PHP 上是否正确配置了 SSL(处理它的模块默认情况下未安装在 PHP 上。您必须在 phph.ini 中检查您的配置)。2) 检查您的防火墙是否允许呼出到所需端口(此处为 465 或 587)。使用 telnet 执行此操作。如果端口未打开,则您将需要 sysdmin 的一些支持来设置配置。我希望你能尽快解决这个问题!

回答by Yasin Hassanien

Open this Linkand select follow the instructions google servers blocks any attempts from unknown servers so once you click on captcha check every thing will be fine

打开此链接并选择按照说明谷歌服务器阻止来自未知服务器的任何尝试,因此一旦您点击验证码检查一切都会好起来的

回答by Forgot My Login 12

Google treat Gmail accounts differently depending on the available user information, probably to curb spammers.

Google 根据可用的用户信息以不同的方式对待 Gmail 帐户,这可能是为了遏制垃圾邮件发送者。

I couldn't use SMTP until I did the phone verification. Made another account to double check and I was able to confirm it.

在进行电话验证之前,我无法使用 SMTP。制作了另一个帐户进行仔细检查,我能够确认它。

回答by Zohaib Hussain

this code working fine for me

这段代码对我来说很好用

    $mail = new PHPMailer;
    //Enable SMTP debugging. 
    $mail->SMTPDebug = 0;
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();
    //Set SMTP host name                          
    $mail->Host = $hostname;
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth = true;
    //Provide username and password     
    $mail->Username = $sender;
    $mail->Password = $mail_password;
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure = "ssl";
    //Set TCP port to connect to 
    $mail->Port = 465;
    $mail->From = $sender;  
    $mail->FromName = $sender_name;
    $mail->addAddress($to);
    $mail->isHTML(true);
    $mail->Subject = $Subject;
    $mail->Body = $Body;
    $mail->AltBody = "This is the plain text version of the email content";
    if (!$mail->send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    }
    else {
           echo 'Mail Sent Successfully';
    }

回答by Akshay P. Raul

Anderscc has got it correct. Thanks. It worked for me but not 100%.

Anderscc 说得对。谢谢。它对我有用,但不是 100%。

I had to set

我必须设置

$mail->SMTPDebug = 0;

Setting it to 1, can cause errors especially if you are passing some data as json to next page. Example - Performing verification if mail is sent, using json to pass data through ajax.

将其设置为 1,可能会导致错误,尤其是当您将一些数据作为 json 传递到下一页时。示例 - 如果发送邮件,则执行验证,使用 json 通过 ajax 传递数据。

I had to lower my gmail account security settings to get rid of errors: " SMTP connect() failed " and " SMTP ERROR: Password command failed "

我不得不降低我的 Gmail 帐户安全设置以消除错误:“ SMTP connect() failed ”和“ SMTP ERROR: Password command failed ”

Solution:This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).

解决方案:此问题可能是由于尝试使用电子邮件帐户的“安全性较低”的应用程序(这是根据谷歌帮助,不确定他们如何判断什么是安全的,什么是不安全的)或者如果您尝试多次登录引起连续或如果您更改国家/地区(例如使用 VPN,将代码移动到不同的服务器或实际尝试从世界的不同部分登录)。

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

解决问题的链接(您必须登录谷歌帐户):

Note:You can go to the following stackoverflow answer link for more detailed reference.

注意:您可以转到以下 stackoverflow 答案链接以获取更详细的参考。

https://stackoverflow.com/a/25175234

https://stackoverflow.com/a/25175234

回答by Tristanisginger

If you are using cPanel you should just click the wee box that allows you to send to external servers by SMTP.

如果您使用的是 cPanel,您只需单击允许您通过 SMTP 发送到外部服务器的 wee 框。

Login to CPanel > Tweak Settings > All> "Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak)"

登录 CPanel > 调整设置 > 全部 > “限制传出 SMTP 为 root、exim 和邮递员(FKA SMTP Tweak)”

As answered here:

正如这里的回答:

"Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer

使用 GMail 和 phpMailer 发送时“服务器不接受密码:535 验证数据不正确”

回答by brandenM

 $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );