Search code examples
phpsmtplocalhostphpmailer

phpmailer username & password not accepted


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 = 'someemail@gmail.com';
$mail->Password = 'somepassword';
$mail->SMTPSecure = 'tls';                                                       
$mail->Port = 25;    
                    
$mail->setFrom('someemail@gmail.com', 'Mailer');
$mail->addAddress('testemail@yahoo.com', 'Receiver');      
$mail->addReplyTo('someemail@gmail.com', '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?


Solution

  • 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.