Search code examples
phpphpmailer

PHPmailer unable to login with correct info


my problem

I tried sending a mail through PHPMailer but when I sent the mail I get the following error:

 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 b4sm17820744wrv.42 - gsmtp
SMTP Error: Could not authenticate.

code

        $mail->IsSMTP();
        $mail->Host = "tls://smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "tls";
        $mail->Username = "******@gmail.com";
        $mail->Password = "*********";
        $mail->Port = "587";
        $mail->SMTPDebug = 1;

I tried

  • Allow less secure apps
  • Enable IMAP & POP
  • manually login to check if username and password are correct(they are)
  • check the error message on google

Solution

  • The main thing you did wrong here is this:

    $mail->SMTPDebug = 1;
    

    That will only show you what the SMTP client (i.e. PHPMailer) is saying, not what Gmail is saying; you would need to set it to 2 in order to see the server responses. What's most likely is that gmail was returning a message like this:

    5.7.14 Please log in via your web browser and then try again

    Gmail is very picky about logging in using new mechanisms, especially when you enable "less secure" apps. So when you said:

    manually login to check if username and password are correct(they are)

    the act of doing that will have cleared the block and allowed your code to work, so it was nothing to do with your code. The gmail example provided with PHPMailer doesn't actually do anything different to what you already had; the basic settings are the same as what you already had.

    Also note that this exact problem is covered in the PHPMailer troubleshooting guide, which should always be the first place you look for PHPMailer answers.