Phpmailer: Username and Password not accepted. SMTP could not authenticate.

Created on 15 Apr 2019  路  9Comments  路  Source: PHPMailer/PHPMailer

I have been finding an answer for a day now and I can't still figure out what is wrong with my code.
I am running phpmailer on a free live hosting site (infinityfree) and i have trouble figuring out the problem to send emails. I am receiving the error below. I made sure everything is not misspelled and had anything removed that should not be in code. Followed the instructions carefully. Tweaked settings in my google accounts as per the instructions on the troubleshooting guide . None of the worked so far.

Error

2019-04-15 00:13:03 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 7sm165115092wrc.81 - gsmtp
2019-04-15 00:13:03 CLIENT -> SERVER: EHLO www.lisandrolaman.cf
2019-04-15 00:13:03 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.191]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-04-15 00:13:03 CLIENT -> SERVER: STARTTLS
2019-04-15 00:13:04 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-04-15 00:13:04 CLIENT -> SERVER: EHLO www.lisandrolaman.cf
2019-04-15 00:13:04 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.191]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-04-15 00:13:04 CLIENT -> SERVER: AUTH LOGIN
2019-04-15 00:13:04 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-04-15 00:13:04 CLIENT -> SERVER: <credentials hidden>
2019-04-15 00:13:04 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-04-15 00:13:04 CLIENT -> SERVER: <credentials hidden>
2019-04-15 00:13:04 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials 7sm165115092wrc.81 - gsmtp
2019-04-15 00:13:04 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials 7sm165115092wrc.81 - gsmtp
SMTP Error: Could not authenticate.
2019-04-15 00:13:04 CLIENT -> SERVER: QUIT
2019-04-15 00:13:04 SERVER -> CLIENT: 221 2.0.0 closing connection 7sm165115092wrc.81 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent! Error: SMTP Error: Could not authenticate.

Below is my php code

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

if (isset($_POST['submit'])){

require_once 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {



$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = '[email protected]';
$mail->Password = 'example';


$mail->setFrom($_POST['email'],$_POST['name']);
$mail->addAddress('[email protected]');
$mail->addReplyTo($_POST['email']);

$mail->isHTML(true);
$mail->Subject='Form Submission:';
$mail->Body='<h1>Name :'.$_POST['name'].'<br>Email: '.$_POST['email']. '<br>Message: 
'.$_POST['message'].'</h1>';

$mail->send();
echo 'Message has been sent!';

} catch(Exception $e){
        echo "Message could not be sent! Error: {$mail->ErrorInfo}"; 
}
 }


?>

Most helpful comment

Update: Ok for some reason my "Less secure apps" setting was not updated properly. My mail is now sending. To get around this you have to enable in your google account Less secure app access. Just login to your google account, in the search bar type in "app" and from the drop-down choose the "Less secure app access" and turn it on.

All 9 comments

This exact problem is dealt with in detail in the troubleshooting guide; it's most likely that you need to visit the "unlock captcha" page.

Thanks for the response. I actually have visited the unlock captcha page already and enable my access to other apps but it does not work in my case. i tried yahoo smtp also receiving the same error. i have it uploaded on my website. it's on my contact form. www.lisandrolaman.cf

@lmnlis is it working ? I did enable third party access to mine

please av been stuck with this same issue. can you show me the get past this problem.

@lmnlis is it working ? I did enable third party access to mine

how did you go about this?
mmmmmmmmmmmmmmm

please av been stuck with this same issue. can you show me the get past this problem.

@lmnlis is it working ? I did enable third party access to mine

how did you go about this?
mmmmmmmmmmmmmmm

I am having this exact same issue. Have taken the steps to unlock captcha, turned on less secure apps access even. But am still receiving the username password failed error message. Have used the gmail PHPMailer template.

Update: Ok for some reason my "Less secure apps" setting was not updated properly. My mail is now sending. To get around this you have to enable in your google account Less secure app access. Just login to your google account, in the search bar type in "app" and from the drop-down choose the "Less secure app access" and turn it on.

thanks a million gkurl, I'd stuck with the same issue for days. Tried your solution...voila mail sent.

Milion thanks again.

Obrigado @gkurl , sua solu莽茫o me ajudou muito.

Para quem estiver com esse problema tamb茅m, segue link de passo a passo do Google. Em seguida, v谩 at茅 a se莽茫o "Remover o acesso do site ou app".

Error 1: Password Command Failed: 534-5.7.9 Application-Specific Password Required
Error 2: SMTP ERROR: Password Command Failed: 535-5.7.8 Username And Password Not Accepted

This happens mostly when SMTP Mail Server Credentials were correct but Application Specific Password was not provided.

Create App Specific Password From Here and pasate it in place of gmail password
https://support.google.com/mail/answer/185833?hl=en

Was this page helpful?
0 / 5 - 0 ratings