PHPMailer SMTP 错误 : Could not authenticate using gmail as smtp server

标签 php smtp gmail phpmailer appfog

我无法在 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) { 如果 (!$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'));
     }

最佳答案

下面的代码对我有用:

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

关于PHPMailer SMTP 错误 : Could not authenticate using gmail as smtp server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233702/

相关文章:

C#通过Gmail账户发送邮件

security - 为什么登录失败时Gmail不提示是用户名还是密码错误?

android - 我如何在android中创建类似于gmail的协议(protocol)?

PHP、MySQL 错误 : Column count doesn't match value count at row 1

php - 计算 php 数组内的特定值

php - Excel 无法打开大型 xml 文件

php - 如何使用 mysql 和 laravel 制作 udemy 计时器

ssl - 扭曲的 SMTP 服务器,添加 SSL 支持,2 个工厂?

asp.net - SMTP 通过 ASP.Net 2 session 大小问题

javascript - 如何使用 puppeteer(node js ) 保持 gmail 登录状态