通过PHPMailer发送电子邮件时出现身份验证错误


Authentication Error while sending email via PHPMailer

以下是我使用PHPMailer发送邮件但出现错误的代码。

 public function changePassword(){
   if($this->request->is('post')){
   require_once(ROOT .DS. 'vendor' . DS  . 'PHPMailer' . DS .  'class.phpmailer.php');
   $email = 'abc@gmail.com';
   $mail = new PHPMailer(); // create a new object
   $mail->IsSMTP(); // enable SMTP
   $mail->isHTML(true);
   $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
   $mail->SMTPAuth = true; // authentication enabled
   $mail->SMTPSecure = 'TLS'; // secure transfer enabled REQUIRED for Gmail
   $mail->Host = "smtp.gmail.com";
   $mail->Port = 587; // or 587
   $mail->IsHTML(true);
   $mail->Username = "xyz@gmail.com";
   $mail->Password = "dead_gone";
   $mail->SetFrom("xyz@gmail.com");
   $mail->AddAddress($email);
   $mail->Subject = "password recovery";
   $mail->Body = "your password is:- sdx_12345  click here to log in <a href ='http://localhost/cake/logins'> click here  </a>  ";
   if(!$mail->Send()) {
     echo "Mailer Error: " . $mail->ErrorInfo;
   } else {
       echo "mail sent";
     }
 }

}

错误:

SMTP->错误:未接受来自服务器的AUTH:530 5.7.0必须首先发出STARTTLS命令。v3sm8416695第17页-gsmtpSMTP错误:无法进行身份验证。Mailer错误:SMTP错误:无法进行身份验证。

此属性区分大小写:

$mail->SMTPSecure = 'tls'

如果你的代码基于PHPMailer提供的gmail示例,这将有所帮助——这并不是说你是第一个连接到gmail的人!

尝试通过端口465进行SSL连接,并从谷歌查看您帐户中的安全设置

您还应该放置

$mail->IsHTML(true);

在你设置好你的身体和附件之后。

编辑:

TLS也可能是小写的。