smtp gmail server php mailer not working - php

I know this question has been asked many a times before. But the solutions didn't seem to help me . So hereby I am posting my code:
<?php
require("class.phpmailer.php");
require("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host="smtp.gmail.com";
$mail->SMTPDebug=1;
$mail->SMTPAuth=true;
$mail->SMTPSecure="ssl";
$mail->Port=465;
$mail->Username ="me#gmail.com";
$mail->Password="mypassword";
$mail->AddAddress("to#yahoo.co.in");
$mail->SetFrom("me#gmail.com", "myname");
$mail->AddReplyTo("someone#gmail.com","someone");
$mail->Subject="Greetings from ME";
$mail->Body="Greeting !! I am glad you received this message";
$mail->WordWrap=50;
if($mail->send())
{
echo "Mail sent successfully";
}
else
{
echo "Sorry mail could not be sent because of error ".$mail->ErrorInfo;
}
?>
The SMTP Debug is:
CLIENT -> SERVER: EHLO messengerclick.byethost3.com 2015-07-26 12:49:42 CLIENT -> SERVER: AUTH LOGIN 2015-07-26 12:49:42
CLIENT -> SERVER: Y2hlcm9ja3oxOUBnbWFpbC5jb20= 2015-07-26 12:49:42
CLIENT -> SERVER: Y2hldGFucm9ja3MxOQ== 2015-07-26 12:49:43
SMTP ERROR: Password command failed: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 sc16sm22415466wjb.28 - gsmtp 2015-07-26 12:49:43
SMTP Error: Could not authenticate. 2015-07-26 12:49:43
CLIENT -> SERVER: QUIT 2015-07-26 12:49:43 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Sorry mail could not be sent because of error SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I have enabled access for unknown apps on my gmail account.
I have also clicked on continue on this link
http://www.google.com/accounts/DisplayUnlockCaptcha
after signing in.
I am running this from my byethost account. It is working fine for smtp.mail.yahoo.com
Please help me know what I am doing wrong.

If you’re sure your password is correct, you can try these troubleshooting tips:
If you've turned on 2-Step Verification for your account, you might need to enter an App password.
Sign in to your account from the web version of Gmail at https://mail.google.com. Afterwards try accessing your messages in
your mail app again.
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.
Your mail app might not support the latest security standards. Learn how to allow less secure apps access to your account.
Make sure your mail app isn't set to check for new email too frequently. If your mail app checks for new messages more than once
every 10 minutes, the app’s access to your account could be blocked.
Try changing your password according to our tips on creating a strong password.
Source.
Here, you can allow less secure apps. Click on Turn On to allow less secure apps having your username and password.

If you are facing similar issue
Try this out:
If you had your password last modified before 2014 change your password to stronger one.
It worked for me

Related

Failed to authenticate on SMTP server in Laravel 6

I am trying to use smtp email functionality in my Laravel 6 Application.
This is my .env file code :
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=******#gmail.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls
Here it is the funny part :
It is working fine in my local host
It is also working in my server (without http)
It stopped working when I applied https in my server.
It gives me below error :
Failed to authenticate on SMTP server with username "*********#gmail.com" using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "534", with message "534-5.7.14
534-5.7.14 Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 g6sm37628450pja.2 - gsmtp
". Authenticator PLAIN returned Expected response code 235 but got code "534", with message "534-5.7.14
534-5.7.14 Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 g6sm37628450pja.2 - gsmtp
". Authenticator XOAUTH2 returned Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials g6sm37628450pja.2 - gsmtp
".
Note :
Allow less secure logged is enabled
This same email is not used in three application for the SMTP email functionality.
the email is working in localhost, the less secure account account verification is correct
But you can't use your email in more than 3 projects, so you should stop using this email on other accounts.
Check if your gmail password has the $ character, if so change it.
it worked for me

PHPMailer EOF caught while checking if connected

We've been using PHPMailer to send SMTP mail through Mailgun for years now.
We've recently upgraded our logging, and we've noticed that some mail just doesn't send.
Here is the SMTP log we're seeing:
Connection: opening to smtp.mailgun.org:587, timeout=300, options=array()
Connection: opened
SERVER -> CLIENT: SMTP
NOTICE: EOF caught while checking if connected
Connection: closed
SMTP Error: Could not connect to SMTP host.
SMTP Error: Could not connect to SMTP host.
This particular email was attempted to be sent with the Start TLS method via port 587.
How come most of our mail gets through but now and then we get this error?
Does anyone know what "EOF caught while checking if connected" could be caused by?
We're using the latest stable build of PHPMailer v6.1.5.
Thanks!
Thanks to advice from #Syncro and #Álvaro González and Mailgun themselves, I ended up building in a retry mechanism that attempts to send mail 3 times before failing.
So far this seems to be working, I will report back if anything changes!

"SMTP Error: Could not authenticate." [duplicate]

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 = "foo#gmail.com";
$Correo->Password = "gmailpassword";
$Correo->SetFrom('foo#gmail.com','De Yo');
$Correo->FromName = "From";
$Correo->AddAddress("bar#hotmail.com");
$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.
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
this is GMail issue
read this Google Help (https://support.google.com/mail/answer/14257?p=client_login&rd=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.
Close your browser and try accessing your messages in your email client again.
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.
Click Continue.
Restart your mail client and try accessing messages in your email client again.
Try this instead :
$Correo->Username = "foo#gmail.com";
I tested it and its working perfectly without no other change
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
Because Allow less secure apps is no longer available
The solution was to enable 2-step verification and generate app password
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.
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.
Create a new password for the Gmail account you want to use
Enable less secure apps in Google Security settings
Use the Display Unlock Captcha page to give your app or website permission to sign in to Gmail. Click Continue or follow the instructions.
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: example#gmail.com 6) Password: examplesecret
my solution is:
change gmail password
on gmail "Manage your google Account" > Security > Turn on 3rd party app Access
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
I received this error because of percentage signs in the password.
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';
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.
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.
You will reach a page like this,
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.
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.
first go to https://myaccount.google.com
Select Security tab
Scroll down and select 'Less secure app access'
Turn on access
This will solve my “SMTP Error: Could not authenticate” in PHPMailer error.
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)
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)
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)
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 : [noreply#yourdomain.com]
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]
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.
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.
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
SOLVED
In 2022 the reason i found that
You need to do two-factor authentication in your mail service.
I am using Mail.ru https://account.mail.ru/user/2-step-auth/passwords/
Source:
https://www.courier.com/error-solutions/smtp-error-could-not-authenticate/

PHPMailer (via Gmail SMTP) works perfectly in localhost but not on actual website

I have recently learned about this mailing system called PHPMailer and I started testing it out.
Initially, I did the testing locally on localhost (using the free MAMP server) and everything worked perfectly fine. Then I uploaded my contents onto my website, only to realise that it did not work online.
Below is the debugging report with $mail->SMTPDebug = 2;:
SERVER -> CLIENT: 220-a2plcpnl0392.prod.iad2.secureserver.net ESMTP Exim 4.85 #2 Thu, 28 Jan 2016 00:03:50 -0700 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
CLIENT -> SERVER: EHLO test.naitsabes.com
SERVER -> CLIENT: 250-a2plcpnl0392.prod.iad2.secureserver.net Hello a2plcpnl0392.prod.iad2.secureserver.net [198.71.231.68]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 TLS go ahead
CLIENT -> SERVER: EHLO test.naitsabes.com
SERVER -> CLIENT: 250-a2plcpnl0392.prod.iad2.secureserver.net Hello a2plcpnl0392.prod.iad2.secureserver.net [198.71.231.68]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250 HELP
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 334 xxx
CLIENT -> SERVER: ZGxldW5nQGNvbm5lY3Qua2VsbGV0dHNjaG9vbC5jb20=
SERVER -> CLIENT: 334 xxx
CLIENT -> SERVER: OTYxYmVlcGJlZXAxNjk=
SERVER -> CLIENT: 535 Incorrect authentication data
SMTP ERROR: Password command failed: 535 Incorrect authentication data
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 a2plcpnl0392.prod.iad2.secureserver.net closing connection
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
It seems to me that Gmail says the password is incorrect or something (but what do I know, this is the first time I used PHPMailer), but I'm not sure what exactly has gone wrong as the PHP code in my local server is perfectly identical which means the password should be correct.
In case this helps, my faulty (???) code is located at http://test.naitsabes.com but it is password protected to avoid people spamming my Gmail inbox. However, if it helps to enter the protected area for debugging purposes, there is a temporary login account with both username and password set to visitor.
I will also provide the link to my source code (on GitHub) upon request but I won't do so on this post to avoid being misunderstood as spamming external links.
Thank you, and much help will be appreciated :)
P.S. I know this question has been posted once by someone else already but by the looks of it, the (original) question is still unsolved with not a single answer.
The problem seems to be your host (godaddy.com) preventing you from connecting to SMTP servers other then theirs. The request never hits any Google server (secureserver.net is a godaddy.com server).
To really solve this problem I would contact them to be sure this is the case and read this article about SMTP email relays as described here
First of all, make sure you enabled outgoing SMTP connections in your Control Panel (assuming you are running on a shared webhost).
If that doesn't work, please share the mail part of your code.

"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 = "foo#gmail.com";
$Correo->Password = "gmailpassword";
$Correo->SetFrom('foo#gmail.com','De Yo');
$Correo->FromName = "From";
$Correo->AddAddress("bar#hotmail.com");
$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.
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
this is GMail issue
read this Google Help (https://support.google.com/mail/answer/14257?p=client_login&rd=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.
Close your browser and try accessing your messages in your email client again.
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.
Click Continue.
Restart your mail client and try accessing messages in your email client again.
Try this instead :
$Correo->Username = "foo#gmail.com";
I tested it and its working perfectly without no other change
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
Because Allow less secure apps is no longer available
The solution was to enable 2-step verification and generate app password
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.
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.
Create a new password for the Gmail account you want to use
Enable less secure apps in Google Security settings
Use the Display Unlock Captcha page to give your app or website permission to sign in to Gmail. Click Continue or follow the instructions.
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: example#gmail.com 6) Password: examplesecret
my solution is:
change gmail password
on gmail "Manage your google Account" > Security > Turn on 3rd party app Access
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
I received this error because of percentage signs in the password.
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';
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.
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.
You will reach a page like this,
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.
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.
first go to https://myaccount.google.com
Select Security tab
Scroll down and select 'Less secure app access'
Turn on access
This will solve my “SMTP Error: Could not authenticate” in PHPMailer error.
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)
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)
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)
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 : [noreply#yourdomain.com]
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]
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.
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.
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
SOLVED
In 2022 the reason i found that
You need to do two-factor authentication in your mail service.
I am using Mail.ru https://account.mail.ru/user/2-step-auth/passwords/
Source:
https://www.courier.com/error-solutions/smtp-error-could-not-authenticate/

Categories