简体   繁体   English

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

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

I am failing to send email in my application hosted on appfog i am using the following code which works fine on localhost but fail on appfog. 我无法在托管在appfog上的应用程序中发送电子邮件,而我正在使用以下代码,该代码在localhost上正常运行,但在appfog上失败。 JPhpMailer extend class.PhpMailer.php 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 = 'me@gmail.com';
                    $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()){抛出新的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'));
     }

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 = "myemail@gmail.com";  // SMTP username
$mail->Password = "mypass"; // SMTP password

$mail->From = "myemail@gmail.com";
$mail->FromName = "myname";
$mail->AddAddress("myaddress@gmail.com", "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";

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 希望对你有帮助

在此处输入图片说明

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 = 'me@gmail.com';
$mailer->Password = 'password';
$mailer->SetFrom('me@gmail.com', 'Name'); 
$mailer->AddAddress('you@gmail.com'); 
$mailer->Subject = 'This is a test';
$mailer->Body = 'Test body';
$mailer->Send();

Hope this helps? 希望这可以帮助?

Print your PHPMailer object and check PORT on object and you given PORT 打印您的PHPMailer对象,并在该对象上检查PORT ,然后您指定了PORT

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

Step 1:- Go to https://myaccount.google.com/security#signin then App passwords generate app password . 步骤1:-转到https://myaccount.google.com/security#signin,然后应用密码会生成应用密码

Step 2:- Paste that 16 digit password $mailer->Password 步骤2:-粘贴16位密码$ mailer-> Password

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. 小提示:您应该仔细阅读phpmailer的调试消息。 There could be a proper link to an answer of the problem. 可能存在与问题答案的正确链接。

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-636190350518518295662-3485238940&rd=2#cantsignin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM