"SMTP Error: Could not authenticate" in PHPMailer
Asked Answered
W

21

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.

Welcome answered 16/10, 2010 at 16:57 Comment(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?Levina
1. open imap and 2. myaccount.google.com/lesssecureappsJumpy
M
29

Try this instead :

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

I tested it and its working perfectly without no other change

Mcdougald answered 16/10, 2010 at 17:19 Comment(2)
I don't understand it! what's the change?Dardanelles
Cammel letter order $Correo->UserNameScientistic
C
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

Consumptive answered 4/9, 2015 at 14:56 Comment(7)
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.Pansypant
I have not seen the Allow less secure apps section but the App Password. More info support.google.com/accounts/answer/185833?hl=enDuvall
@TheHive the same happens to me, had to turn off the 2-step verification. Thank a lot.Sideward
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.Sideward
Ok, I wrote an answer of how to do when the 2 step verification is turned on.Sideward
The cure after 3 hours!Conservatism
the "Allow less secure apps" setting is no longer available from May 30, 2022Handsel
G
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

Genni answered 30/4, 2013 at 11:33 Comment(1)
may require you to wait a few min, didn't work instantly for me... hit continue twice.Spurtle
M
29

Try this instead :

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

I tested it and its working perfectly without no other change

Mcdougald answered 16/10, 2010 at 17:19 Comment(2)
I don't understand it! what's the change?Dardanelles
Cammel letter order $Correo->UserNameScientistic
P
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

Phocis answered 28/6, 2022 at 2:10 Comment(2)
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.Moreira
Note : set backup phone and emailGiant
B
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

Biotype answered 1/8, 2012 at 9:57 Comment(2)
You can just use single quotes. Only the single quote itself would have to be escaped.Thielen
It's not a "special character" in the usual meaning. You've used a nonexistent variable $ch3 your string.Nutation
D
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
Discontinue answered 20/7, 2017 at 8:24 Comment(1)
You don't need to enable Forwarding, POP or IMAP in Gmail to get this to work.Discontinue
H
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

Harumscarum answered 15/7, 2020 at 15:37 Comment(1)
Thanks. This's the solution for me. Upvoted for 2020Casein
G
4

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

Granniah answered 5/12, 2011 at 13:22 Comment(1)
Working for me. ThanksServomechanism
C
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';

Corned answered 11/6, 2013 at 12:17 Comment(3)
I hope you're still not using that password because 1. You've just supplied it to the Internet at large 2. It's not at all secure. dl.dropboxusercontent.com/u/209/zxcvbn/test/index.htmlTrustless
for more clarification it was just an exampleCorned
This is not solution.Cavit
C
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.

Chalcis answered 11/6, 2014 at 19:30 Comment(1)
This is also not a solution.Cavit
S
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.

Sideward answered 20/5, 2018 at 10:16 Comment(0)
O
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.

Opsonize answered 24/5, 2016 at 22:57 Comment(0)
E
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.

Erbes answered 29/4, 2020 at 13:7 Comment(0)
I
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)

Iago answered 13/4, 2022 at 9:49 Comment(0)
E
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)

Erleneerlewine answered 18/8, 2019 at 14:6 Comment(0)
O
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)

Occlusive answered 29/1, 2015 at 21:35 Comment(0)
B
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]
Bagasse answered 10/2, 2016 at 7:17 Comment(0)
C
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.
Chaetopod answered 14/12, 2017 at 11:47 Comment(0)
C
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.

Cavit answered 12/4, 2018 at 7:24 Comment(0)
L
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

Linctus answered 25/12, 2019 at 14:58 Comment(0)
S
0

SOLVED

In 2022 the reason i found that

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

Subscript answered 16/12, 2022 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.