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

时间:2013-01-09 10:56:33

标签: php smtp gmail phpmailer appfog

我无法在我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 = '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()){                抛出新的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'));
     }

6 个答案:

答案 0 :(得分:6)

以下代码对我有用:

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

答案 1 :(得分:2)

我遇到了同样的问题。为了让它工作,我不得不去myaccount.google.com - &gt; &#34;连接的应用程序&amp;网站&#34;,并转向&#34;允许安全性较低的应用&#34;到&#34; ON&#34; (靠近页面底部)。

希望它有所帮助

enter image description here

答案 2 :(得分:1)

注册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();

希望这有帮助吗?

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

第2步: - 粘贴16位数密码 $ mailer-&gt;密码

答案 5 :(得分:0)

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

小提示:你应该仔细阅读phpmailer的调试信息。可以找到问题答案的正确链接。

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