74

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.

2
  • 2
    This is strange, as the error pretty clearly points to a username/password error rather than a protocol problem. You don't happen to have special characters in your password that might get transferred wrongly?
    – Pekka
    Commented Oct 16, 2010 at 17:18
  • 1. open imap and 2. myaccount.google.com/lesssecureapps
    – Ruthe
    Commented Oct 7, 2018 at 15:11

21 Answers 21

83

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

7
  • 3
    I am sorry for the late response. For those of you cannot able to see the Allow less secure apps dialog box. 1. Go to your account 2. Connected apps & sites (sign-in & security tab) 3. You should see three tabs under 'Connected apps & sites'. But if you only see - 'Apps connected to your account' and 'Saved passwords'. 4. On the sidebar click 'Signing in to Google' - Turnoff your 2-step verification. 5. Now you will see the Allow less secure apps. Turn if On.
    – Dexter
    Commented Sep 20, 2016 at 7:29
  • I have not seen the Allow less secure apps section but the App Password. More info support.google.com/accounts/answer/185833?hl=en
    – guido
    Commented Jun 16, 2017 at 14:48
  • @TheHive the same happens to me, had to turn off the 2-step verification. Thank a lot.
    – Arefe
    Commented May 19, 2018 at 14:47
  • 3
    I have a question: is there a way, we can accomplish without turn off the 2-step verification? I would like to have it for the Gmail account if possible.
    – Arefe
    Commented May 19, 2018 at 14:57
  • 4
    the "Allow less secure apps" setting is no longer available from May 30, 2022
    – Sal Celli
    Commented Aug 31, 2022 at 18:40
36

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

1
  • may require you to wait a few min, didn't work instantly for me... hit continue twice.
    – enko
    Commented Apr 14, 2014 at 22:10
29

Try this instead :

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

I tested it and its working perfectly without no other change

2
  • I don't understand it! what's the change? Commented Feb 5, 2020 at 11:25
  • Cammel letter order $Correo->UserName
    – Anvar Pk
    Commented Feb 6, 2020 at 6:16
14

Because Allow less secure apps is no longer available

enter image description here

The solution was to enable 2-step verification and generate app password

app

select mail and computer from the list then click generate

copy the code shown in the box and replace your google password with your app password it works like a charm.

app password

2
  • 1
    Note that you can't create any 'app passwords' unless the 2-step verification is turned on. Make sure to do this first otherwise you will not see an option to set the "App passwords'. To turn it on, log into myaccount.google.com -> Security -> "Signing in to Google" -> 2-Step verification -> follow the prompts there in. Commented Dec 26, 2022 at 8:32
  • Note : set backup phone and email
    – Think Big
    Commented Jan 12 at 16:33
10

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

2
  • 2
    You can just use single quotes. Only the single quote itself would have to be escaped. Commented Sep 11, 2015 at 14:15
  • It's not a "special character" in the usual meaning. You've used a nonexistent variable $ch3 your string.
    – Sebi2020
    Commented Mar 11, 2022 at 11:20
6

I experienced the same error when configuring the WP-Mail-SMTP plugin in Wordpress.

The problem would persist even when I have 'triple checked' the settings and login credentials, and am able to log in manually using a browser.

There's a list of steps you can take to fix this.

  1. Create a new password for the Gmail account you want to use
  2. Enable less secure apps in Google Security settings
  3. Use the Display Unlock Captcha page to give your app or website permission to sign in to Gmail. Click Continue or follow the instructions.
  4. Sign in using the app or website. The smtp settings that work for me are 1) SMTP Host: smtp.gmail.com 2) SMTP port: 587 3) Encryption: TLS 4) Authentication: SMTP authentication 5) Username: [email protected] 6) Password: examplesecret
1
  • You don't need to enable Forwarding, POP or IMAP in Gmail to get this to work.
    – pyfork
    Commented Jul 20, 2017 at 8:29
6

my solution is:

  1. change gmail password
  2. on gmail "Manage your google Account" > Security > Turn on 3rd party app Access
  3. This the new step that i discover by UnlockingCaptcha that told in this site, the exact site is https://accounts.google.com/b/0/DisplayUnlockCaptcha, but maybe you want to read the former site first.

That all, hope it works for you

1
  • Thanks. This's the solution for me. Upvoted for 2020 Commented Jan 10, 2021 at 14:36
4

I received this error because of percentage signs in the password.

1
  • Working for me. Thanks
    – ScottLee
    Commented Feb 18, 2020 at 11:41
3

For me I had a special characters in my password field, and I put it like $mail->Password = " por$ch3" which work for gmail smpt server but not for other; so I just changed double quotes to single quotes and it works for me. $mail->Password = ' por$ch3';

3
3

If you still face error in sending email, with the same error message. Try this:

$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';

just Before the line:

$send = $mail->Send();

or in other sense, before calling the Send() Function.

Tested and Working.

1
  • 2
    This is also not a solution. Commented Apr 12, 2018 at 5:48
3

The other post is correct to resolve the issue but doesn't address how to do it if the 2-step-verification is turned on. The option to allow the less secure apps is NOT available then. Here is an answer to how to do it:

a. Go to the URL of `https://myaccount.google.com/` and click `Sing-in and security`

b. Click on the app password. 

enter image description here

You will reach a page like this,

enter image description here

c. Create name of your app and generate a password for the respective app.  

d. Use that password acquired here inside the app.

This should resolve the issue.

0
2

I had the same issue and did all the tips including Gmail setting (e.g. less secure apps access) with no luck. But finally when I changed password to something different, for some reason it worked! FYI, the initial password did not have any special characters.

2
  1. first go to https://myaccount.google.com
  2. Select Security tab
  3. Scroll down and select 'Less secure app access'
  4. Turn on access

This will solve my “SMTP Error: Could not authenticate” in PHPMailer error.

2

The correct answer: Go to "Manage your google accounts => Security => Signing in to Google => App passwords". Generate your maill account password there (that will be used from other device)

1

I had the same issue and did all the tips with no luck. Finally when I changed password to something different, for some reason it worked! (the initial password or the new one did not have any special characters)

0
0

I had the same problem with authentication. The fix was to set up 2-step verification and create an application specific password for the device ( error messages for blocking the device will appear in your account settings->"Notifications and alerts" if you problem is the same with mine)

0

It was the selinux issue. I just updated the below given part in /etc/selinux/config file SELINUX=permissive (it was SELINUX=enforcing before). then just reboot the system by giving reboot Now the mail goes without any hassle. Configuration

From Email Address : [[email protected]]
From Name : [your domain name]
SMTP Host   : smtp.gmail.com
Type of Encryption : SSL
SMTP Port : 465
SMTP Authentication  : YES
Username : [your mail id]
Password : [your password]
0

SMTP Error: could not authenticate I had the same problem. The following troubleshooting steps helped me.

  • I turned off two-factor authentication in my gmail account.
  • I allowed less secure apps to access my gmail account. 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).
  • At this step, when I tried to register a user, I would get the same error. Google would sent me a warning message that someone has my password and the login was blocked.
  • Gmail will then provide you with options. You either click whether the activity was yours or not yours. Click the option that the activity was yours.
  • Try registration again. It should now work.
0

There is no issue with your code.

Follow below two simple steps to send emails from phpmailer.

  • You have to disable 2-step verification setting for google account if you have enabled.

  • Turn ON allow access to less secure app.

0

I had the same issue which was fixed following the instructions below

Test enabling “Access for less secure apps” (which just means the client/app doesn’t use OAuth 2.0 - https://oauth.net/2/) for the account you are trying to access. It's found in the account settings on the Security tab, Account permissions (not available to accounts with 2-step verification enabled): https://support.google.com/accounts/answer/6010255?hl=en

original link for the answer: https://support.google.com/mail/thread/5621336?msgid=6292199

0

SOLVED

In 2022 the reason i found that

Source: https://www.courier.com/error-solutions/smtp-error-could-not-authenticate/

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