phpsmtpphpmailer

Unable to send mail via PHPMailer [SMTP->Error: Password not accepted from server: 535-5.7.8]


I've a PHP page that sends email upon submission. It was working fine but 2 days ago, it threw this message:

SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.

My code is as follow:

        $host = "smtp.gmail.com";
        $port = 465;
        $secure_comm = "ssl";
        
        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->IsHTML(true);
        $mail->Host = $host;
        $mail->Port = $port;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = $secure_comm;
        $mail->Username = $email_user;
        $mail->Password = $email_password;
        $mail->From = $email_user;
        $mail->FromName = $from;
        $mail->AddAddress($to);
        $mail->Subject = $email_subject;
        $mail->Body = $message;

        if(!$mail->Send())
            echo "Mailer Error: " . $mail->ErrorInfo;
        else
        {
           // Do something
        }

I am using PHP 8.0 & PHPMailer 5.2.0. I've also enabled "Allow less secure apps" on Google admin console. The login credentials are also correct.

Any help?


Solution

  • Go to your Google account, turn on 2-step verification, create an app password, and then use that password in PHPMailer instead of your Gmail password. After that, try sending the email again.