Ranster
Ranster

Reputation: 93

Unable to connect to gmail using phpMailer

My php website has been using phpMailer to connect to a Gmail account using its email address & password, and then send out a HTML email from that account. Things have been working for a year but recently I have been getting the following error when attempting to send through phpMailer for some of the emaill addresses:

SMTP -> ERROR: Password not accepted from server: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/answer/14257 x142sm1800199iod.31 - gsmtp

Below are my PHP codes:

//initialize variable
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true;  
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->IsHTML(true);

//set pw and user
$mail->Username = $from_email;  // SMTP username/email
$mail->Password = $pw; // SMTP password

$mail->From = $from_email;
$mail->FromName = $from_name;
$mail->AddAddress($email,$name);//add recipient address and name 
$mail->Subject = $subject;
$mail->Body = $body;//HTML email body

//send mail
$mail->Send();

Weirdly enough this problem only occurs for SOME email accounts, and for the accounts that it has problem, the problem occurs regardless of whether the "Allow less secure apps" setting is turned on or not under their account.

Since this had been working fine for over a year and this "Username and Password not accepted" error only came up recently and only for SOME email accounts, I am wondering if anything changed on Google's end to allow apps to connect to email accounts.

Any help/info would be greatly appreciated.

Upvotes: 1

Views: 172

Answers (1)

Synchro
Synchro

Reputation: 37770

Yes google did change their authentication, and yes, allowing less secure apps doesn't always work.

Fortunately phpmailer added support for XOAUTH2 a while ago, so if you update to the latest version and read the docs, examples and wiki, you'll be fine.

Upvotes: 2

Related Questions