PHPMailer - 无法验证

时间:2016-09-22 15:30:09

标签: php phpmailer

我已经在PHP服务器上运行PHPMailer一年了。一切都很好,直到3天前我开始收到以下错误:

  

SMTP错误:无法进行身份验证。

允许不太安全的应用启用

以下是代码:

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 = "myemail@gmail.com"; // SMTP username
$mail->Password = "myemailpass"; // SMTP password
$webmaster_email = "myemail@gmail.com"; //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如何在高负载下运行,或者如果高负载会导致上述错误?

3 个答案:

答案 0 :(得分:0)

尝试去: myaccount.google.com - > "关联的应用&网站" ,并转为"允许安全性较低的应用" " ON" 。 替代方案: 尝试将SMTP端口更改为:465(也是gmail)。

答案 1 :(得分:0)

我遇到类似问题,需要设置发件人地址

$mail->setFrom('myemail@gmail.com', 'Webmaster');

答案 2 :(得分:0)

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

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

升级到使用Google商家的新帐户并切换到该帐户。问题解决了。