【问题标题】: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?无法通过 PHPMailer 使用 Gmail SMTP 服务器发送电子邮件,出现错误:在端口 587 上提交邮件需要 SMTP AUTH。如何解决?
【发布时间】:2013-04-09 12:36:18
【问题描述】:

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

【问题讨论】:

标签: php smtp gmail phpmailer


【解决方案1】:
$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-&gt;SMTPSecure = 'ssl';

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

更新

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

$mail->SMTPSecure = 'tls';

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

【讨论】:

  • 我在使用您的代码时收到此错误邮件错误:以下发件人地址失败:phoenixd110@gmail.com:在未连接的情况下调用 Mail()
  • 更新了答案并提出了进一步的建议。无论如何,代码肯定没问题。您可以检查这两个来源phpmailer.worxware.com/index.php?pg=exampleagmailgithub.com/Synchro/PHPMailer/blob/master/examples/…
  • 我收到以下错误:客户端 -> SMTP:EHLO localhost SMTP -> 错误:EHLO 未从服务器接受:客户端 -> SMTP:HELO localhost 注意:fwrite():发送 16 个字节failed with errno=10054 现有连接被远程主机强行关闭。在 C:\xampp\htdocs\program\mailsending1\mailsending_v1\PHPMailer-master\class.smtp.php 中的第 1023 行 SMTP -> 错误:服务器不接受 HELO:SMTP -> 注意:检查是否已连接 SMTP Connect( ) 失败的。邮件程序错误:SMTP Connect() 失败。
  • 请注意,如果您指定“ssl”(强烈推荐),您的 PHP 安装需要加载(或编译)“openssl”扩展,使用您最喜欢的 PHP 扩展机制(我碰巧使用 OS X Macports,它是 sudo port install php5-openssl)
  • 最好同时检查返回值和错误 if(!$mail->Send() || $mail->ErrorInfo) {
【解决方案2】:

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

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

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

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

$mail->isSMTP();

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

【讨论】:

  • 此解决方案有效,因为它完全关闭了 SMTP。我不推荐这种方法。我从来没有解决过这个问题,我现在改用 Mandrill API。
  • 就用 Mandrill,它好多了,而且实际上有一些分析。
  • 解决了我的问题。非常感谢。
  • 它也对我有用。奇怪了,这个库已经很成熟了
  • 糟糕的解决方案,您没有使用这样的 smtp,而是使用 php mail() 函数,它可能会将您的电子邮件作为垃圾邮件发送给某些收件人
【解决方案3】:

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

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

然后将$mail-&gt;Password 设置为您的应用程序专用密码。

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      打开这个Link 并选择按照说明进行操作,谷歌服务器会阻止来自未知服务器的任何尝试,所以一旦你点击验证码,一切都会好起来的

      【讨论】:

      • 我使用了与接受的答案相同的代码,但仍然会出现 SMTP_connect() 失败。我尝试了该链接中的所有内容,最终成功了,所以 +1。
      • 我也遇到了 SMTP_connect() 失败的问题。我按照上述步骤操作,但仍然出现错误。我在 2 个不同的 gmail 地址上按照这些步骤操作了两次,而且这两次似乎我都需要等待大约 1 小时才能进行更改。之后一切都按预期工作。
      • 访问google.com/accounts/DisplayUnlockCaptcha 然后运行我的代码是它为我工作的原因。 +1
      • 解决了我的问题。
      【解决方案6】:

      Google 会根据可用的用户信息对 Gmail 帐户进行不同的处理,这可能是为了遏制垃圾邮件发送者。

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

      【讨论】:

        【解决方案7】:

        这段代码对我来说很好用

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

        【讨论】:

        • $mail->SMTPSecure = 'ssl'; $邮件->端口= 465;我刚刚将 TSL 更改为 ssl 并将 587 更改为 465,然后它对我有用,谢谢.. GMAIL 允许不太安全的应用程序:myaccount.google.com/lesssecureapps
        【解决方案8】:
         $mail->SMTPOptions = array(
                        'ssl' => array(
                            'verify_peer' => false,
                            'verify_peer_name' => false,
                            'allow_self_signed' => true
                        )
                    );
        

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        • 这很危险。不要这样做。
        【解决方案9】:

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

        登录到 CPanel > Tweak Settings > All> "将传出 SMTP 限制为 root、exim 和 mailman (FKA SMTP Tweak)"

        在这里回答:

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

        【讨论】:

          【解决方案10】:

          Anderscc 正确。谢谢。它对我有用,但不是 100%。

          我必须设置

          $mail->SMTPDebug = 0;
          

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

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

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

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

          注意: 更详细的参考可以到下面的stackoverflow回答链接。

          https://stackoverflow.com/a/25175234

          【讨论】:

            【解决方案11】:

            如果有人没有得到任何有效的答案,可以试试这个。因为我花了一天时间来解决这个问题。
            注意:我使用的是 PHPMailer 5.2.23。

            <?php
            
                date_default_timezone_set('Asia/Kolkata');
            
                require './Libraries/PHPMailer5/PHPMailerAutoload.php';
                $mail = new PHPMailer;
            
                $mail->isSMTP();
            
                // Enable SMTP debugging
                // 0 = off (for production use)
                // 1 = client messages
                // 2 = client and server messages
                $mail->SMTPDebug = 2;
            
                $mail->Debugoutput = 'html';
            
                $mail->Host = 'localhost';
                $mail->Port = 587;
            
                $mail->SMTPSecure = 'tls';
            
                $mail->SMTPAuth = true;
                $mail->Username = "yourWebmailUsermail";
                $mail->Password = "yourWebmailPassword";
            
                $mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );
            
                $mail->setFrom('formEmailAddress', 'First Last');
            
                $mail->addAddress('toEmailAddress', 'John Doe');
            
                $mail->Subject = 'PHPMailer GMail SMTP test';
            
                $mail->msgHTML("<h1>Hi Test Mail</h1>");
                $mail->AltBody = 'This is a plain-text message body';
            
                if (!$mail->send()) {
                    echo "Mailer Error: " . $mail->ErrorInfo;
                } else {
                    echo "Message sent!";
                }
            ?>
            

            【讨论】:

              【解决方案12】:

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

              基本设置:

              $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/

              【讨论】:

                【解决方案13】:

                我只是想分享我使用 phpMailer 的经验,它在本地 (XAMPP) 上运行,但不适用于我的托管服务提供商。

                我开启了phpMailer报错

                 $mail->SMTPDebug=2
                

                我收到“连接被拒绝错误”

                我就这个问题给我的主机提供商发了电子邮件,他们说他们会打开 SMTP 端口 25、465、587。

                然后我收到以下错误响应“SMTP 错误:密码命令失败:”....“请通过您的网络浏览器登录,然后重试”....“SMTP 错误:无法验证。”

                所以 google 会检查您是否登录到您的帐户(我是通过浏览器在本地运行脚本时),然后允许您通过 phpMailer 脚本发送邮件。

                解决这个问题:

                1:转到您的 Google 帐户 -> 安全性 2:滚动到钥匙图标并选择“2路验证”并按照程序操作 3:完成后从谷歌帐户返回钥匙图标 - >安全并选择第二个选项“创建应用程序密码”并按照程序获取密码。

                现在转到您的 phpMailer 对象并使用上述过程中提供的密码更改您的 Google 密码。

                你已经完成了。

                代码:

                require_once('class.phpmailer.php');
                
                $phpMailerObj= new PHPMailer();
                
                                $phpMailerObj->isSMTP();                    
                                $phpMailerObj->SMTPDebug = 0;
                                $phpMailerObj->Debugoutput = 'html';                    
                                $phpMailerObj->Host = 'smtp.gmail.com';                     
                                $phpMailerObj->Port = 587;
                                $phpMailerObj->SMTPSecure = 'tls';
                                $phpMailerObj->SMTPAuth = true;                 
                                $phpMailerObj->Username = "YOUR EMAIL";                 
                                $phpMailerObj->Password = "THE NEW PASSWORD FROM GOOGLE ";
                                $phpMailerObj->setFrom('YOUR EMAIL ADDRESS', 'THE NAME OF THE SENDER',0);
                                $phpMailerObj->addAddress('RECEIVER EMAIL ADDRESS', 'RECEIVER NAME');
                                
                                $phpMailerObj->Subject = 'SUBJECT';
                                $phpMailerObj->Body ='MESSAGE';
                                
                                if (!phpMailerObj->send()) {
                                    echo "phpMailerObjer Error: " . $phpMailerObj->ErrorInfo;
                                    return 0;
                                } else {
                                    echo "Message sent!";
                                    return 1;
                                } 
                

                【讨论】:

                  【解决方案14】:

                  我认为是连接问题,您可以在这里获取代码http://skillrow.com/sending-mail-using-smtp-and-php/

                  include(“smtpfile.php“);
                  include(“saslfile.php“); // for SASL authentication $from=”my@website.com“; //from mail id
                  
                  $smtp=new smtp_class;
                  
                  $smtp->host_name=”www.abc.com“; // name of host
                  $smtp->host_port=25;//port of host
                  
                  $smtp->timeout=10;
                  $smtp->data_timeout=0;
                  $smtp->debug=1;
                  $smtp->html_debug=1;
                  $smtp->pop3_auth_host=””;
                  $smtp->ssl=0;
                  $smtp->start_tls=0;
                  $smtp->localhost=”localhost“;
                  $smtp->direct_delivery=0;
                  
                  $smtp->user=”smtp username”;
                  $smtp->realm=””;
                  $smtp->password=”smtp password“;
                  
                  $smtp->workstation=””;
                  $smtp->authentication_mechanism=””;
                  
                  $mail=$smtp->SendMessage($from,array($to),array(“From:$from”,”To: $to”,”Subject: $subject”,”Date: ”.strftime(“%a, %d %b %Y %H:%M:%S %Z”)),”$message”);
                  
                  if($mail){
                     echo “Mail sent“;
                  }else{
                     echo $smtp->error;
                  }
                  

                  【讨论】:

                  • 通常不鼓励仅链接的答案。你能把它扩展成独立的东西吗?
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-10-19
                  • 2020-02-21
                  • 1970-01-01
                  • 2022-01-09
                  • 2014-10-15
                  相关资源
                  最近更新 更多