Could not authenticate Google account through PHPMailer - php

I'm using PHPMailer in a Simple Script For Send Email's Through Gmail, and I'm getting an this error (I'm sure that the email and password combination is correct):
!-- 2020-12-02 14:13:16 CLIENT -> SERVER: EHLO localhost
2020-12-02 14:13:16 CLIENT -> SERVER: STARTTLS
2020-12-02 14:13:17 CLIENT -> SERVER: EHLO localhost
SMTP Error: Could not authenticate.
2020-12-02 14:13:17 CLIENT -> SERVER: QUIT
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Allow less secure apps is ON
This is the way I implement the phpMailer
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
require './mailer/autoload.php';
$msg = "";
$mail = new PHPMailer();
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
$mail->isSMTP();
$mail->Host = 'smtp.google.com';
$mail->SMTPAuth = true;
$mail->Username = '********#gmail.com';
$mail->Password = '********';
$mail->SMTPSecure = "tls";
$mail->Port = 25;
$mail->CharSet= 'UTF-8';
$mail->setFrom('*******#gmail.com', 'Mailer');
$mail->addAddress($_POST["mail"]);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = '<h2>E-mail</h2>';
$mail->AltBody = $_POST["content"];
$mail->send();
} catch (Exception $e) {
$msg = "An Error has Ocurr";
}
How can I solve this issue?

SMTP port 25 is not used with TLS, you should use port 587 for TLS/STARTTLS or 456 for SSL. And it seems that you've also used the incorrect host URL, which should be smtp.google.com. The required configuration is stated here: https://support.google.com/mail/answer/7126229.
So you should probably change:
...
$mail->Host = 'smtp.google.com';
$mail->Port = 25;
...
To:
...
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
...
Depending on your situation, it might not be best practice to use Google's default SMTP. It is OK for personal use, but if you want to send more automated emails, you should look for other options. The default Google SMTP is strictly rate limited for example.
First of all, Google SMTP Relay yields a little more configurability if you need it.
When you really want to send automated or bulk emails, you should look into a provider specifically for this. It is not what the Google SMTP servers are made for and you will quickly notice by emails not being sent out or delivered properly.

It would really help if you actually read the error message and took the advice it gives you, by reading the guide it links to.
First of all, you're only showing client debug output, so you can't see what the server is saying, and so you can't tell what's going on, as the docs say. Do this:
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
Without seeing what that says, you're working blind.
That said, you get kicked out immediately after EHLO, and the only thing you have said is:
2020-12-02 14:13:17 CLIENT -> SERVER: EHLO localhost
Unfortunately this is untrue, and I'd guess that gmail is calling you out on it. localhost is by definition not an internet routable address, and any reverse lookup on the name will never match the IP you are connecting from, which is not localhost. If that is happening automatically, override it manually by setting the client host explicitly:
$mail->Helo = 'myhost.example.com';
While RFCs mandate port 587 for SMTP+STARTTLS, gmail supports it on port 25 too, and you can see that your STARTTLS command is working successfully, so that's not the problem here.

use port 587
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = '********#gmail.com';
$mail->Password = '********';

Related

phpmailer hangs on send() [duplicate]

I had successfully setup a web app using WAMPSERVER on a desktop used by a few people internally, this used PHPMailer to an internal SMTP server without encryption or authentication and it worked.
That desktop crashed and I've migrated to a "new" desktop. I had an SVN setup so I was even using most of the same files and config. One difference which might matter is that the old desktop was 64-bit and the new is 32-bit. This means I'm using different versions of WAMPSERVER.
The mailer just hangs. I don't get a PHP error or a PHP timeout. I just never reach the end of my script. The crazy part about this is that it works with authentication, ssl, and gmail. It just won't work with the extra simple case I need.
This works:
<?php
require('class.phpmailer.php');
$mail=new PHPMailer();
$mail->ISSMTP();
$mail->Host='smtp.gmail.com';
$mail->Subject='test subj';
$mail->Body='the body email test';
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "myemail#gmail.com"; // GMAIL username
$mail->Password = "mypassword"; // GMAIL password
$mail->AddAddress('toemail#supersecret.com', 'John Doe');
$mail->SetFrom('something#gmail.com', 'First Last');
$mail->Send();
?>
this used to, but now does not:
<?php
require('class.phpmailer.php');
$mail=new PHPMailer();
$mail->ISSMTP();
$mail->Host='smtp.internal.com';
$mail->Subject='test subj';
$mail->Body='the body email test';
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->AddAddress('myaddress#somewhere.com', 'John Doe');
$mail->SetFrom('someaddress#mightbereal.com', 'First Last');
$mail->Send();
?>
The only thing I get from debug is
CLIENT -> SMTP: EHLO thedesktophostname
No errors display on the page and nothing in the apache log, where I normally get PHP errors, if they don't display.
I can telnet to the host from the desktop on port 25 and even type in the EHLO command and get a good response from the server.
I don't remember having this issue before, although it's possibly I've already solved it once. I couldn't find anything that helped here or on The Google.
Please help. Thanks.
Hijacking the post to say i had the same issue but had set the port to 465 without setting SMTPSecure to 'ssl' in the example its set TLS by default
If you have a server hosted in Hostgator (shared hosting), this is what solved for me:
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
(even though the official example in PHPMailer suggests using ENCRYPTION_STARTTLS)
sadly this probably won't ever help anyone else who has this same problem, but I was able to get everything working by just changing the port to 465.
Eventually found solution for my configuration.
Just add ssl:// to smtp.google.com
$mail->Host = 'ssl://smtp.gmail.com';
I had the same issue. Nothing displays after the send method.
I realized that the encryption was wrong, I did use SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
// Enable TLS encryption, `PHPMailer::ENCRYPTION_SMTPS` also accepted

PHPMailer cannot send email

My project contains a function to send email, being PHPMailer. It runs well to send email from my localhost server, but it stopped sending email today, and now it shows this error message
SMTP Error: Could not connect to SMTP host.
I added this code $mail->SMTPDebug = 1; to view debug errors and is now showing me this message:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: The requested name is valid, but no data of the requested type was found. (0) SMTP Error: Could not connect to SMTP host
I already have enabled extension=php_openssl.dll in php.ini.
This is my code:
$mail = new PHPMailer();
$mail->SMTPSecure = 'ssl';
$mail->IsSMTP();
$mail->Username = "myemail#gmail.com"; // your GMail user name
$mail->Password = "password";
$mail->AddAddress($email); // recipients email
$mail->FromName = "username"; // readable name
$mail->IsHTML(true);
$mail->Subject = "title";
$mail->Body = " Message";
$mail->SMTPDebug = 1;
$mail->Host = "ssl://smtp.gmail.com"; // GMail
$mail->Port = 465;
$mail->IsSMTP(); // use SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->From = $mail->Username;
if($mail->Send()){
} else {
}
thanks
This is commonly reported as a PHPMailer problem (and there are many duplicates of this question), but it's almost always down to local DNS failure, firewall blocking or other network issue on your local network.
First, make sure you are using the latest PHPMailer.
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead:
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
or more succinctly:
$mail->Host = 'tls://smtp.gmail.com:587';
You can your connectivity this by running some commands on your server (you will need dnsutils and telnet packages installed). First check DNS is working:
dig +short smtp.gmail.com
You will get something like this if your DNS is working:
gmail-smtp-msa.l.google.com.
173.194.67.108
173.194.67.109
Next try to telnet to the host on the port you need:
telnet smtp.gmail.com 587
This should give you something like this:
Trying 173.194.67.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP ex2sm16805587wjd.30 - gsmtp
(Enter quit to get out of that).
If either of these fail, PHPMailer will not work either. So go fix your network, then try again. If you are not in control of your own firewall or DNS, you probably need to raise a support ticket with your ISP to fix this. If they won't fix it, you need to replace your ISP.
Back in PHPMailer, you can get lower-level feedback on the connection by setting:
$mail->SMTPDebug = 4;
A simple google search revealed this forum - http://forums.devshed.com/php-development-5/unable-to-find-the-socket-transport-ssl-667689.html

Mail not sending with PHPMailer over SSL using SMTP

I am trying to use PHPMailer to send e-mails over SMTP but so far have had no luck. I've gone through a number of SO questions, PHPMailer tutorials and forum posts but still cannot get it to work. I'll document as many of my failed attempts as I can remember to save time, but firstly here is the code I am using:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "myEmail#gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail#gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail#gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail#me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Firstly, when I run this code now I get two different errors. On my local server I get the error:
SMTP -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail#gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail#gmail.com : Called Mail() without being connected
I get moreorless the same error running the same code on my web server, but the first line is:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
Obviously it's worth pointing out that I'm not using the literal "myEmail#gmail.com" but I've substituted my own email out for this post.
Things I've tried
- Using the iCloud SMTP server
- Using a different port
- Enabling the OpenSSL extension in my php.ini file
- Copying code from various PHPMailer examples
- Using Google's "DisplayUnlockCaptcha" system to enable connections
- Sending to and from different addresses
- Removing the "#gmail.com" from the Username property
- A number of other things I can't remember
This has now been driving me mad for about a day, so if anyone can solve it they will be a hero.
Thanks
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail#gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
That is a working configuration.
try to replace what you have
Don't use SSL on port 465, it's been deprecated since 1998 and is only used by Microsoft products that didn't get the memo; use TLS on port 587 instead: So, the code below should work very well for you.
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the
Firstly, use these settings for Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
But also, what firewall have you got set up?
If you're filtering out TCP ports 465/995, and maybe 587, you'll need to configure some exceptions or take them off your rules list.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I got a similar failure with SMTP whenever my client machine changes network connection (e.g., home vs. office network) and somehow restarting network service (or rebooting the machine) resolves the issue for me. Not sure if this would apply to your case, but just in case.
sudo /etc/init.d/networking restart # for ubuntu
First, Google created the "use less secure accounts method" function:
https://myaccount.google.com/security
Then created the another permission:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
Hope it helps.

Unable to send mail with PHP (phpmailer) via Office365 in Hostgator. How to fix this?

I want to be able to send mails in Hostgator via office365. I was able to do it with Gmail, but can not set it up to work with office365.
It works on my 2 other servers. The only problem is Hostgator.
Do I have to fix something or Hostgator have to take some action?
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "pod51014.outlook.com";
$mail->Port = 587;
$mail->Username = "usernamehere";
$mail->Password = "************";
/* ... addaddres, reply, subject, message -> the usual stuff you need ... */
$mail->Send();
?>
I just keep getting following response:
SMTP -> ERROR: Failed to connect to server: Connection refused (111)
I was on the support chat with them and the 587 port should be open.
I think hostgator is blocking outgoing emails but accepts incoming emails.
If your hosting provider doesn't allow outbound SMTP mail, I suggest you take a look at Microsoft Graph - a REST API which let's you also send e-mails and do much more. You can use for example oauth2-azure library to interact with it very easily from your PHP code.
Try these things. Maybe something will work.
Set the host to:
$mail->Host = 'smtp.office365.com';
Do not set a port at all:
//$mail->Port = 587;

PHPMailer with GMail: SMTP Error

I am making use of PHPMailer to send mail through GMail. The code I use is straight from a tutorial and it works perfectly on my laptop. However, testing this on a Windows 2003 Server - it seems to always return an SMPT error:
SMTP Error: Could not connect to SMTP
host. Mailer Error: SMTP Error: Could
not connect to SMTP host.
Here is the settings I use in PHPMailer:
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // use ssl
$mail->Host = "smtp.gmail.com"; // GMAIL's SMTP server
$mail->Port = 465; // SMTP port used by GMAIL server
Can I say with confidence that this isn't a port issue, since I am connecting to another server on port 465 and it is sending mail. If not, please explain.
How can I resolve this issue?
Thanks all for any help
First thing notice off-hand: Gmail uses TLS. Don't know if having SSL instead of TLS will make much of a difference but SSL is the predecessor to TLS.
I recommend checking out also, its phpmailer customized for using gmail. PHPGMailer
To use PHPMailer with gmail, don't use SSL/465 (it's been deprecated since 1998), use TLS/587 as Noctrine suggests, and here's how:
include 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "tls://smtp.gmail.com"; // GMAIL's SMTP server
$mail->Port = 587; // SMTP port used by GMAIL server
...
You should find that works.

Categories