php PHPMailer SMTP 错误:无法使用 gmail 作为 smtp 服务器进行身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14233702/
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 06:54:37  来源:igfitidea点击:

PHPMailer SMTP Error: Could not authenticate using gmail as smtp server

phpsmtpgmailphpmailerappfog

提问by Eliethesaiyan

I am failing to send email in my application hosted on appfogi am using the following code which works fine on localhost but fail on appfog. JPhpMailer extend class.PhpMailer.php

我无法在appfog 上托管的应用程序中发送电子邮件 我正在使用以下代码,该代码在 localhost 上运行良好,但在 appfog 上失败。JPhpMailer 扩展 class.PhpMailer.php

                    $mailer = new JPhpMailer(true);
                    $mailer->IsSMTP();
                    $mailer->Mailer = "smtp";
                    //$mailer->SMTPSecure == 'tls'; 
                    $mailer->Host = 'ssl://smtp.gmail.com';
                    $mailer->Port = '465';
                    $mailer->SMTPAuth = true;
                    //$mailer->SMTPSecure = true; 
                    $mailer->Username = '[email protected]';
                    $mailer->Password = 'zzzzzzz';
                    $mailer->SetFrom($to['from'], $to['from_name']); 
                    $mailer->AddAddress($to['to'],$to['to_name'] ); 
                    $mailer->Subject = $to['subject'];
                    $mailer->Body = $to['body'];
                    $mailer->Send();

here is the line that in phpMailer that fails to execute`if ($tls) { if (!$this->smtp->StartTLS()) { throw new phpmailerException($this->Lang('tls')); }

这是在 phpMailer 中无法执行的行`if ($tls) { if (!$this->smtp->StartTLS()) { throw new phpmailerException($this->Lang('tls')); }

         //We must resend HELO after tls negotiation
        $this->smtp->Hello($hello);
      }

       $connection = true;
      if ($this->SMTPAuth) {
         if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
         **strong text throw new phpmailerException($this->Lang('authenticate')); **            }
      }
     }
   $index++;
    if (!$connection) {
       throw new phpmailerException($this->Lang('connect_host'));
     }

回答by

The code below is working for me :

下面的代码对我有用:

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->Port = 587;
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "mypass"; // SMTP password

$mail->From = "[email protected]";
$mail->FromName = "myname";
$mail->AddAddress("[email protected]", "myname");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "Here is the subject";
$mail->Body    = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

回答by Developer

I encountered the same problem. To get it working, I had to go to myaccount.google.com-> "connected apps & sites", and turn "Allow less secure apps" to "ON" (near the bottom of the page).

我遇到了同样的问题。为了让它工作,我必须去myaccount.google.com->“连接的应用程序和网站”,然后将“允许安全性较低的应用程序”设置为“开启”(靠近页面底部)。

Hope it helps some one

希望它可以帮助某人

enter image description here

在此处输入图片说明

回答by Gavin

After signing up to appfog, I was able to get PHPMailer working with the following.

注册 appfog 后,我能够让 PHPMailer 使用以下内容。

I was unable to find JPHPMailer, although I suspect that isn't the cause of your issue but the fact that you were putting ssl://smtp.gmail.com as the host.

我找不到 JPHPMailer,虽然我怀疑这不是问题的原因,而是您将 ssl://smtp.gmail.com 作为主机的事实。

ini_set('display_errors', 1);
error_reporting(E_ALL);

include('class.phpmailer.php');
$mailer = new PHPMailer(true);
$mailer->IsSMTP();
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 465;
$mailer->SMTPAuth = true;
$mailer->Username = '[email protected]';
$mailer->Password = 'password';
$mailer->SetFrom('[email protected]', 'Name'); 
$mailer->AddAddress('[email protected]'); 
$mailer->Subject = 'This is a test';
$mailer->Body = 'Test body';
$mailer->Send();

Hope this helps?

希望这可以帮助?

回答by UWU_SANDUN

Print your PHPMailerobject and check PORTon object and you given PORT

打印您的PHPMailer对象并检查PORT对象并给出PORT

echo "<pre>", print_r($mailer, true);
exit;

回答by Arshid KV

Step 1:- Go to https://myaccount.google.com/security#signinthen App passwordsgenerate app password.

第 1 步:- 转到https://myaccount.google.com/security#signin,然后应用密码生成应用密码

Step 2:- Paste that 16 digit password $mailer->Password

第 2 步:- 粘贴 16 位密码$mailer->Password

回答by Mark

In most cases you need to create a 2-Step Verification and sign in with an App password.

在大多数情况下,您需要创建两步验证并使用应用密码登录。

Small tip: you should carefully read phpmailer's debug message. There could be a proper link to an answer of the problem.

小提示:你应该仔细阅读 phpmailer 的调试信息。可能存在指向问题答案的正确链接。

I had this one: https://support.google.com/mail/answer/7126229?visit_id=1-636190350518295662-3485238940&rd=2#cantsignin

我有这个:https: //support.google.com/mail/answer/7126229?visit_id=1-636190350518295662-3485238940&rd=2#cantsignin