【问题标题】:PHPMailer SMTP Error: Could not authenticate using gmail as smtp serverPHPMailer SMTP 错误:无法使用 gmail 作为 smtp 服务器进行身份验证
【发布时间】:2012-12-23 09:51:28
【问题描述】:

我无法在appfog 上托管的应用程序中发送电子邮件 我正在使用以下代码,它在 localhost 上运行良好,但在 appfog 上失败。 JPhpMailer 扩展类.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();

这是 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'));
     }

【问题讨论】:

  • 您的服务器上是否安装了 Open SSL?做一个 PHPInfo 来找出答案。
  • 感谢 Gavin,openssl 没有安装在我的托管服务器上,但也没有安装在我的本地主机上,但它运行良好
  • 尝试 tls 而不是 ssl

标签: php smtp gmail phpmailer appfog


【解决方案1】:

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

小提示:你应该仔细阅读phpmailer的调试信息。问题的答案可能有适当的链接。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我的帐户在 Outlook 中,例如 example@domain.com
    【解决方案3】:

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

    希望对大家有所帮助

    【讨论】:

    • 呃...是的,这个,这个确实,乘以一百万。我希望我能给你 +1000
    【解决方案4】:

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

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

    【讨论】:

    • 它对我不起作用并显示错误
    • 为什么我们需要使用 print_r
    【解决方案5】:

    下面的代码对我有用:

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

    【讨论】:

    • Sry 端口应该是 $mail-&gt;Port = 587; or $mail-&gt;Port = 465; 用于 gmail。在我的代码中更新。
    • 嘿@Sumit,我发现我必须在 appfog 上安装 ssl(在我的计划中启用),而我现在还没有……所以切换回使用邮件而不是 SMTP!Sight !
    【解决方案6】:

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

    我无法找到 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();
    

    希望这有帮助吗?

    【讨论】:

    • thx Gavin ...我只是尝试它仍然在 if(!$this->SmtpConnect().. 我不知道是什么原因导致我将邮件主机更改为 smtp.gmail.com 和将 SMPTSecure 添加到 ssl 但我没有更改任何其他内容
    • 您的脚本顶部有ini_set('display_errors', 1); error_reporting(E_ALL); 吗?可能会引发异常,您可以看到导致它的错误。当我第一次尝试时,PHPMailer 在身份验证等方面返回了一些错误。
    • 我做到了,它给了我这个错误 SMTP 错误:无法连接到 SMTP 主机。在我看到的所有帖子中,他们说这是因为您的服务器(appfog)上未启用 ssl,你做了什么让它工作@Gavin
    • @Spidaboy7 - 不幸的是,我所做的只是创建了一个新的 PHP 实例并部署了上面的代码,使用 class.phpmailer.phpclass.smtp.php。我认为我确实在欧盟西部设置了它。如果你愿意,我可以重新创建它吗?
    • 我创建了一个新实例(除了上传三个文件外没有配置),它立即工作。您的 gmail 帐户是否设置了两个因素身份验证?我这样做了,而且我必须提供我的应用程序密码,而不是我的“正常”密码。
    猜你喜欢
    • 2015-07-23
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 2017-06-22
    • 2017-03-10
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多