2016-09-22 86 views
5

我一直在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

對於我來說,當一個問題非常普遍時,標準流程就是詢問。大多數人不像你那麼勤奮:-) –

回答

0

請務必檢查谷歌的使用限制的需要! PHPMailer不會告訴你,它只會給你無法驗證錯誤的細節,但原因可能是因爲你的限制。

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

升級到一個新的帳戶與谷歌的業務,並切換到該帳戶。問題解決了。

0

儘量要: myaccount.google.com - >「關聯的應用&網站」,並把「允許不夠安全的應用」「ON」。 備選: 嘗試將SMTP端口更改爲:465(也是Gmail)。

+0

您的第一個建議已在問題中得到確認(請參閱** bold **文本)。 –

0

我有類似的問題,並設置從地址

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