"SMTP Error: Could not authenticate" in PHPMailer

I'm using PHPMailer in a Simple Script For Send Email's Through Gmail, and I'm getting an "Unknown Error" (At least for me!):

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

SMTP server error: 5.7.1 Username and Password not accepted. Learn more at 535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 p38sm2467302ybk.16

I've read about Configure OpenSSL For SSL/TLS Connections, and I did it. Apache And PHP Are properly-Configured (With OpenSSL extension Running in PHP and mod_ssl running in Apache 2.2.16).

This is The PHP Script:

 <?php
  require_once ("PHPMailer\class.phpmailer.php");
  $Correo = new PHPMailer();
  $Correo->IsSMTP();
  $Correo->SMTPAuth = true;
  $Correo->SMTPSecure = "tls";
  $Correo->Host = "smtp.gmail.com";
  $Correo->Port = 587;
  $Correo->UserName = "[email protected]";
  $Correo->Password = "gmailpassword";
  $Correo->SetFrom('[email protected]','De Yo');
  $Correo->FromName = "From";
  $Correo->AddAddress("[email protected]");
  $Correo->Subject = "Prueba con PHPMailer";
  $Correo->Body = "<H3>Bienvenido! Esto Funciona!</H3>";
  $Correo->IsHTML (true);
  if (!$Correo->Send())
  {
    echo "Error: $Correo->ErrorInfo";
  }
  else
  {
    echo "Message Sent!";
  }
?>

The Username and Password are OK, And I tried in Thunderbird, without any problem. I've also Used SSL Authentication and Port 465, getting the same Error.


Solution 1:

I encountered this problem. To get it working, I had to go to myaccount.google.com -> "Sign-in & security" -> "Apps with account access", and turn "Allow less secure apps" to "ON" (near the bottom of the page).

Alternatively you can follow this direct link to these settings

enter image description here

Solution 2:

this is GMail issue

read this Google Help (https://support.google.com/mail/answer/14257?p=client_login&rd=1)

  1. Open your web browser and sign in to Gmail at http://mail.google.com/mail. If you see a word verification request, type the letters in the distorted picture and finish signing in.
  2. Close your browser and try accessing your messages in your email client again.
  3. If you're still having problems, visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If necessary, enter the letters in the distorted picture.
  4. Click Continue.
  5. Restart your mail client and try accessing messages in your email client again.

SMTP Error: Could not authenticate

Gmail issue

Solution 3:

Try this instead :

$Correo->Username = "[email protected]";

I tested it and its working perfectly without no other change

Solution 4:

I received the same error and in mycase it was the password. My password has special characters.

If you supply the password without escaping the special characters the error will persist.

E.g $mail->Password = " por$ch3"; is valid but will not work using the code above .

The solution should be as follows: $mail->Password = "por\$ch3";

Note the Backslash I placed before the dollar character within my password. That should work if you have a password using special characters