Search code examples
phpphpmailer

PHPMailer - Could not authenticate


I've been running PHPMailer for a year now on a php server. Everything was fine until 3 days ago when I started getting the following error:

SMTP Error: Could not authenticate.

Allow less secure apps is ON

Here is the code:

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
            }
}

Note I just updated PHPMailer to the latest version to try to remedy the problem but nothing has changed! The old version 5.2.2 was still having the same problem!

EDIT: I just had one successful email go through to google and sent properly. Which now makes me question if it's lag issue or something of that sort. Does anyone know how phpmailer functions under high loads or if high loads can cause the above error?


Solution

  • Make sure you check google's usage limits! PHPMailer will not tell you particulars it will just give you the Could not authenticate error but the reason why can be because of your limits.

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

    Upgraded to a new account with google business and switched to that account. Issue resolved.