Phpmailer authentication failed but no credential changes - php

I am using PHPMAILER to send confirmation emails etc. I started to get the following error 2 days ago (I thought it might just go away but seems to be an actual issue):
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
I checked the credentials and they are ok. I am using office365 exchange login and tried to login and it works. It looks like I am on version 6.05 and the current version is 6.5 could that be the cause?
Here is what a snippet of the credentials look like:
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
function tep_htmlmail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
$mail->Host = 'smtp.office365.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email#mysite.com'; // SMTP username
$mail->Password = 'MyPassword'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
$mail->Port = 587; // TCP port to connect to
//Server settings
//$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP();
//Recipients
$mail->setFrom($from_email_address, $from_email_name);
$mail->addReplyTo($from_email_address, $from_email_name);

Related

Why can't I connect to the gmail SMTP server using tls?

My target is to send an email to myself using PHPMailer. I am using Gmail SMTP as a start (I have enabled less secure app access and disabled two factor authentication), and am trying to connect from localhost (using MAMP). This is my code:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/Exception.php';
require 'PHPMailer-master/src/PHPMailer.php';
require 'PHPMailer-master/src/SMTP.php';
$mail = new PHPmailer(true);
try {
$mail->SetFrom('myemail','myname');
$mail->AddAddress('myemail','mynickname');
$mail->Body = 'There is a great disturbance in the Force.';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = 'tls';
$mail->Username = 'myemail#gmail.com';
$mail->Password = 'mypassword';
$mail->Port = 587;
$mail->Send();
}
catch (Exception $e){
echo $e->errorMessage();
}
This gives me a SMTP connect() failed error. I checked the troubleshooting page for this and have found that this error occurs when there is a Firewall or DNS failure, so I have tried disabling the firewall on my computer altogether, but to no avail. What could be the problem?
It is worth noting that I have tried connecting to the smtp server on cmd using telnet smtp.gmail.com 587 and that didn't work either. What could be the problem here?
Edit: I changed $mail->SMTPSecure = 'tls'; and $mail->Port = 587; to $mail->SMTPSecure = 'ssl'; and $mail->Port = 587; respectively it worked. Why does tls not work?
Use
$mail->SMTPDebug = 4;
In the try{ section to help narrow down the errors

Could not authenticate Google account through PHPMailer

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 = '********';

How to find the information from the hosting service 1&1 in order to send emails using PHP mailer?

I currently have a website which uses PHPmailer to send emails. I am hosting it with 1&1.fr, but cannot find the information in order to actually send emails. Here is the following information that I need:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
On the 1&1.fr website, they give out the following information:
In the image, they specify multiple ports as well as an entrance/exit server; which ones am I supposed to pick and enter into my PHP file.
The rest of my code works fine (it works when I use my gmail account using 000webhost).
Any help would be greatly appreciated.
// define the $mail // just in case you miss it as it is missing in your code.
$mail = new PHPMailer();
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2; // this is to enable debug if there are errors
$mail->isSMTP(); //Tell PHPMailer to use SMTP
//Set the hostname of the mail server
$mail->Host = 'auth.smtp.1and1.fr';
// Enable authentication so you must provide username and password for SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Here you are telling to use a secure connection with TLS/SSL
//Set the SMTP port number
$mail->Port = 587; // if specified tls. try also 465 as defined in the picture you post
// TCP port for secure connections. 465 is the secure port for outgoing
// emails and 993 is for incoming email using IMAP. If you use POP3 the
//incoming emails are received on 995 port number.
Hope now is more clear.

phpmailer SMTP client information

I am updating some contact forms on several websites i made, and i am using phpmailer for it.
I'm using the SMTP method where you fill in the username and password of the email client where it needs to send to, like below:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mailout.one.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'info#company.com'; // SMTP username
$mail->Password = 'mypass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('info#company.com', $email);
$mail->addAddress('info#company.com');
$mail->addReplyTo(''.$email.'');
My problem is i have to get my client his/hers emailaddress with their password, how could i solve this without asking my clients for their email information.

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.

Categories