PHPMailer - 无法验证

问题描述:

我一直在PHP服务器上运行PHPMailer一年。一切都很好,直到4天前,当我开始收到以下错误:PHPMailer - 无法验证

SMTP Error: Could not authenticate.

允许不够安全的应用是ON

这里是代码:

function SendEmail($to,$cc,$bcc,$subject,$body) { 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer(true); 
$mail->SMTPDebug = 1; 
try { 
$addresses = explode(',', $to); 
foreach ($addresses as $address) { 
    $mail->AddAddress($address); 
} 
if($cc!=''){ 
    $mail->addCustomHeader("CC: " . $cc); 
} 
if($bcc!=''){ 
    $mail->addCustomHeader("BCC: " . $bcc); 
} 
$mail->IsSMTP(); 
$mail->SMTPAuth = true; // turn on SMTP authentication 
$mail->SMTPSecure = "tls";     // sets the prefix to the servier 
$mail->Host = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
$mail->Port = 587; 
$mail->Username = "[email protected]"; // SMTP username 
$mail->Password = "myemailpass"; // SMTP password 
$webmaster_email = "[email protected]"; //Reply to this email ID 
$name=$email; 
$mail->From = $webmaster_email; 
$mail->FromName = "Service"; 
//$mail->AddReplyTo($webmaster_email, "DiFractal Customer Service"); 
$mail->WordWrap = 50; // set word wrap 
$mail->IsHTML(true); // send as HTML 
$mail->Subject = $subject; 
$mail->Body = $body; 
return $mail->Send(); 
} catch (phpmailerException $e) { 
$myfile = fopen("debug_email.txt", "w"); 
      fwrite($myfile,$e->errorMessage() . "\n" . $mail->ErrorInfo); 
      fclose($myfile);//Pretty error messages from PHPMailer 
} catch (Exception $e) { 
$myfile = fopen("debug_email_stp.txt", "w"); 
      fwrite($myfile,$e->getMessage()); 
      fclose($myfile);//Pretty error messages from PHPMailer 
      } 
} 

注意我刚刚更新PHPMailer到最新版本,试图解决这个问题,但没有任何改变!旧版本5.2.2仍然有同样的问题!

编辑:我刚刚有一个成功的电子邮件通过谷歌和正确发送。这现在让我质疑,如果它是滞后问题或类似的东西。有谁知道phpmailer如何在高负载或高负载可能导致上述错误的功能?

+0

一个很常见的问题。您是否检查过[** google **](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=phpmailer%20gmail%20could%20not%20authenticate)? –

+0

@Michael_B当然。对我而言,标准流程是在发布堆栈之前,先研究谷歌搜索结果的第一页。 –

+0

对于我来说,当一个问题非常普遍时,标准流程就是询问。大多数人不像你那么勤奋:-) –

请务必检查谷歌的使用限制的需要! PHPMailer不会告诉你,它只会给你无法验证错误的细节,但原因可能是因为你的限制。

@https://support.google.com/a/answer/166852?hl=en

升级到一个新的帐户与谷歌的业务,并切换到该帐户。问题解决了。

尽量要: myaccount.google.com - >“关联的应用&网站”,并把“允许不够安全的应用”“ON”。 备选: 尝试将SMTP端口更改为:465(也是Gmail)。

+0

您的第一个建议已在问题中得到确认(请参阅** bold **文本)。 –

我有类似的问题,并设置从地址

$mail->setFrom('[email protected]', 'Webmaster');