0

I have never received the following error when working with PHPMailer:

2021-11-23 03:10:20 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted.
2021-11-23 03:10:20 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted.

I am indeed entering the correct username and password. Here is the code:

<?php

use phpmailer\PHPMailer\PHPMailer;
require_once '../phpmailer/PHPMailer.php';
require_once '../phpmailer/SMTP.php';
require_once '../phpmailer/Exception.php';

$mail = new PHPMailer;    
$mail->SMTPDebug = 4;  
                    
$mail->isSMTP();                                        
$mail->Host = 'smtp.gmail.com';                             
$mail->SMTPAuth = true;       
$mail->Username = '[email protected]';
$mail->Password = 'somepassword';
$mail->SMTPSecure = 'tls';                                                       
$mail->Port = 25;    
                    
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Receiver');      
$mail->addReplyTo('[email protected]', 'No Reply');
$mail->Body = $body;

$mail->isHTML(true);

if($mail->send()){
  echo "Email sent";
} else {
  echo "Error: There was an error sending email " . $mail->ErrorInfo;;
}

?>

I have tried to user the answer found here: Username and Password not accepted

And here: Username and Password not accepted

No luck.

I am using the correct username and password.

I changed the port to 465 and got the following error:

2021-11-23 03:20:20 Connection: opening to smtp.gmail.com:465, timeout=300, options=array()<br>
2021-11-23 03:20:20 Connection: opened<br>
2021-11-23 03:20:30 SMTP INBOUND: &quot;&quot;<br>
2021-11-23 03:20:30 SERVER -&gt; CLIENT: <br>
2021-11-23 03:20:30 Connection: closing due to error<br>
2021-11-23 03:20:30 Connection: closed<br>
SMTP connect() failed. 
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting<br>
Error: There was an error sending email SMTP connect() failed. 
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

What am I doing wrong?

2
  • 2
    For Gmail use port 587 and SMTPSecure use 'tls'. If you enabled 2 step verification, please create app password for use with your code.
    – vee
    Commented Nov 23, 2021 at 3:58
  • No, ssl mode on port 465 is the preferred config, though it's easier to diagnose TLS problems with tls mode on port 587. Failing to create a TLS connection suggests that your PHP configuration may be outdated, or that your OS may have an outdated CA certificate bundle. Please read the guide the error message links to more info on how to diagnose it further.
    – Synchro
    Commented Nov 23, 2021 at 8:15

2 Answers 2

1

I was able to solve my problem by going here:

https://myaccount.google.com/u/0/lesssecureapps

And set the Allow Secure Apps to On.

Once I made this update, I was able to send the email.

1

After May 30, 2022, Google required some changes for SMTP to work.

So, I did a step-by-step of what must be done to work:

1 - Access: https://myaccount.google.com/security?hl=en

2 - Turn on 2-Factor Authentication

2 factor auth

3 - Create a password in Apps

Create APP Pass

4 - Copy the Password App Created in your - PHP MAILER - $mail->password = '########';

copy and past

5 - Name APP Pass

Name app pass

Not the answer you're looking for? Browse other questions tagged or ask your own question.