Why PhpMailer failes on some machines? - php

I'm using PHPMailer to send emails. My problem is that from my PC everything works just fine, but on other devices, like someone else laptop or PC gives the following error:
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.The following From address failed: confidential#confidential.com Mailer Error: The following From address failed: confidential#confidential.com
SMTP server error: AUTH command used when not advertised
Can someone give some light on this one please?!
My code:
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = stripslashes($_POST['textarea']);
$body = str_replace(' ',' ',$body);
$body = str_replace('/media/','http://www.confidential.com/media/', $body);
$body = $body;
//$body = eregi_replace("[\]",'',$body);
$mail->From = "confidential#confidential.com";
$mail->FromName = "confidential confidential";
$mail->CharSet = 'UTF-8';
$mail->Subject = $_POST['subject'];
//$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host = "confidential.com"; // SMTP server
//$mail->SMTPAuth = false; // enable SMTP authentication
//$mail->Port = 25; // set the SMTP port for the GMAIL server
//$mail->Username = "confidential#confidential.com"; // SMTP account username
//$mail->Password = "confidential"; // SMTP account password
$mail->SetFrom('confidential#confidential.com', 'confidential confidential');
$mail->AddReplyTo("confidential#confidential.com","confidential confidential");
//$mail->SMTPDebug = 1;
Furthermore, when I use SMTP authentication I have these next errors, on every device:
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.SMTP Error: The following recipients failed: someemail#test.com Mailer Error: SMTP Error: The following recipients failed: someemail#test.com
SMTP server error: Authentication failed
UPDATE:
Full errors resulted:
SMTP -> FROM SERVER:220-sclabo15.ds.systemsunit.com ESMTP Exim 4.86_1 #1 Wed, 16 Mar 2016 13:10:25 +0200 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
SMTP -> FROM SERVER: 250-sclabo15.ds.systemsunit.com Hello confidential.com [176.223.213.66] 250-SIZE 52428800 250-8BITMIME 250-PIPELINING 250-AUTH PLAIN LOGIN 250-STARTTLS 250 HELP
SMTP -> ERROR: Password not accepted from server: 535 Incorrect authentication data
SMTP -> FROM SERVER:250 Reset OK
SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.SMTP -> FROM SERVER:250 OK
SMTP -> FROM SERVER:550 Authentication failed
SMTP -> ERROR: RCPT not accepted from server: 550 Authentication failed
SMTP Error: The following recipients failed: confidential#confidential.com Mailer Error: SMTP Error: The following recipients failed: confidential#confidential.com
SMTP server error: Authentication failed

does the password use any non-ASCII characters? – Martin #Martin,
if '#' is non ASCII, then yes. - Anonymous
Your issue may be that your password contains the # character. This character is a bit of a pain as it doesn't quite fit as being "safe" but is often not actually removed or cleaned by many cleaning functions. It is a URL address special character and has repeatedly come up as being a cause for similar SMTP errors:
https://sourceforge.net/p/davmail/bugs/539/
https://meta.discourse.org/t/bug-smtp-password-field-does-not-escape-comment-sign-hash/23344/6
Also check your string encoding for the username and password strings. Your reference to $mail->CharSet = 'UTF-8'; might be causing the PHPMailer to encode the password for the SMTP (I'm not sure though).
http://www.easy-smtp.com/smtp-troubleshooting-guide
There are also various other knock on events such as you will be blocked from the server for to many failed authentication attempts. Frustrating, but needs to be noted.
http://www.fehcom.de/qmail/smtpauth.html
http://www.samlogic.net/articles/smtp-commands-reference-auth.htm
Solution:
Debug and change your password to not contain the # character and see if that improves your situation. But please note your potential for being blocked for too many false logins and you'd need to wait some time for a clear connection to te SMTP server again.

$mail->SMTPDebug = 2; enables SMTP debug information (for testing)

Related

Error: SMTP Error: Could not authenticate [duplicate]

I created an account on Gmail (becase the previous one was giving me the same problem) so that my application could send emails using google smtp server.
I'm using the PHPMailer library and asked it to show any log errors.
I always get a message similar to this. It vary a little, sometimes it is shorter and sometimes longer, depending on my configuration
2015-08-01 05:07:01 CLIENT -> SERVER: EHLO www.lavile.com
2015-08-01 05:07:01 CLIENT -> SERVER: AUTH LOGIN
2015-08-01 05:07:01 CLIENT -> SERVER: Y29udGF0by5sYXZpbGVAZ21haWwuY29t
2015-08-01 05:07:01 CLIENT -> SERVER: WXlhdUgxMnM= 2015-08-01 05:07:01 SMTP ERROR: Password command failed: 534-5.7.9 Please log in with your web browser and then try again. Learn more at 534 5.7.9 https://support.google.com/mail/answer/78754 b16sm3352387qga.48 - gsmtp
2015-08-01 05:07:01 SMTP Error: Could not authenticate.
2015-08-01 05:07:01 CLIENT -> SERVER: QUIT
2015-08-01 05:07:01 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting string(82) "SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting"
This is my script
$mail->Username = "myusername#gmail.com";
$mail->Password = "mypassword";
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->SetFrom($mail->Username, 'Contato Lavile');
$mail->addAddress($mail->Username, "Contato Lavile");
$mail->Subject = 'Novo contato no site Lavile';
$mail->Body = $text;
$mail->IsHTML(true);
$mail->AltBody = $text;
Also GMAIL is sending automatic emails to myself alerting me that a new login attempt was blocked. I even changed some settings on my gmail to make it work but everything failed
Any idead? Do you know what could be happening?
I would recommend to actually read the error message:
... 534-5.7.9 Please log in with your web browser and then try again. Learn more at 534 5.7.9 https://support.google.com/mail/answer/78754 b16sm3352387qga.48 - gsmtp
When reading this site you will find something about supporting client which provide only less secure methods of authorization. Your client is one of these so follow the link to learn how to enable support for these application in gmail.
You can send email without own smtp server:
1) get recipient mx hostname from dns
2) send email to port 25
Not saying this is necessarily what happened to the topic opener but I had exactly this same error today. And the reason I got it was because I created a new e-mail address and you first need to log in to Gmail via the browser once before you can use smtp.
After logging in to the Gmail web client via the browser and going over the initial flow I was able to send via the smtp endpoint.
$mail->Host = "smtp.gmail.com"; should be something like:
$mail->Host = "mail.yourdomain.com";//replace your domain name

problem with PHPMAILER Could not connect to SMTP host

I Have Problem With PHPMAILER it's Work Good in localhost but in server give me error
PHPMAILER Code
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
function sendMail($driver, $driverUser){
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'wastaapplication#gmail.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('wastaapplication#gmail.com', 'Wasta Driver');
$mail->addAddress($driver, $driverUser); // Add a recipient
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'New Order';
$mail->Body = ' مرحبا ' . '<strong>' . $driverUser . '</strong>' . ' لديك طلبيه جديده برجاء مراجعه برنامج الطيارين ';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
Error
2019-04-30 05:14:51 SERVER -> CLIENT: 220-server.issgroups.org ESMTP Exim 4.91 #1 Tue, 30 Apr 2019 07:14:51 +0200 220-We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail.
2019-04-30 05:14:51 CLIENT -> SERVER: EHLO wastetkheer.com
2019-04-30 05:14:51 SERVER -> CLIENT: 250-server.issgroups.org Hello wastetkheer.com [138.201.107.252]250-SIZE 52428800250-8BITMIME250-PIPELINING250-AUTH PLAIN LOGIN250-STARTTLS250 HELP
2019-04-30 05:14:51 CLIENT -> SERVER: STARTTLS
2019-04-30 05:14:51 SERVER -> CLIENT: 220 TLS go ahead
SMTP Error: Could not connect to SMTP host.
2019-04-30 05:14:51 CLIENT -> SERVER: QUIT
2019-04-30 05:14:51
2019-04-30 05:14:51
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.
i'm trying change SMTPDebug To 1
& //$mail->isSMTP();
it's work good in local host not in server
I guess the problem is with the SMTP authentication, but I couldn´t find the problem.
Please search before posting as this has been answered many times before.
It's failing immediately after STARTTLS, indicating a TLS error. This is very common with gmail because their CA root certificates changed about a year ago to ones that are not present in many older OSs. It's working for you on localhost because your local OS does not have outdated CA certificates.
Read the troubleshooting guide which tells you exactly how to deal with this.
It could also be down to your ISP redirecting SMTP traffic to their own mail server, causing a certificate name mismatch - the guide provides ways of diagnosing the exact problem.

SMTP showing error after sending few mails to user from localhost

I am working with phpmailer and smtp to send mail from local host to gmail. The problem is firstly i send mail and it was working fine but now its giving smtp connection failed. Here is my code. I am new to this, kindly help me out.
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'smag.ghaznavi1#gmail.com'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->SMTPDebug=2;
$mail->setFrom('info#example.com', 'PMS');
//$mail->addReplyTo('info#example.com', 'CodexWorld');
$mail->addAddress('smag.ghaznavi1#gmail.com'); // Add a recipient
//$mail->addCC('cc#example.com');
//$mail->addBCC('bcc#example.com');
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>Your password reset link</h1>';
$bodyContent .= "Dear user,\n\nIf this e-mail does not apply to you please ignore it. It appears that you have requested a password reset at our website www.yoursitehere.com\n\nTo reset your password, please click the link below. If you cannot click it, please paste it into your web browser's address bar.\n\n" . $pwrurl . "\n\nThanks,\nThe Administration";
$mail->Subject = 'Email from Project Management System Admin';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
smag.ghaznavi1#gmail.com2018-07-04 16:43:58 SERVER -> CLIENT: 220
smtp.gmail.com ESMTP o4-v6sm5724500wmo.20 - gsmtp
2018-07-04 16:43:58 CLIENT -> SERVER: EHLO localhost
2018-07-04 16:43:58 SERVER -> CLIENT: 250-smtp.gmail.com at your service,
[39.54.130.23]250-SIZE 35882577250-8BITMIME250-STARTTLS250-
ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2018-07-04 16:43:58 CLIENT -> SERVER: STARTTLS
2018-07-04 16:43:58 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-07-04 16:43:59 CLIENT -> SERVER: QUIT
2018-07-04 16:43:59 SERVER -> CLIENT:
2018-07-04 16:43:59 SMTP ERROR: QUIT command failed:
SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent.Mailer Error: SMTP connect() failed.
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Check if enabled "Allow less secure apps" in your google account - link.
Also try to enter in your google account and visit page "displayCaptcha" - link
And you should provide your code to understand the problem
It's failing to start TLS, which is usually an indicator that your CA certificates are out of date, especially when using Gmail. It's nothing to do with the "less secure apps" setting or the captcha issue - though you may run into those later.
Follow the link to the troubleshooting guide in the error message which describes this exact error, and tells you how to fix it (by updating your CA certs - not by disabling certificate verification!). It's uncanny, almost as if that link was there purely to help you resolve problems like this without having to ask questions and wait for answers...

Cannot understand PHPMailer debug log

I am trying to use PHPMailer,
I have enabled opensll and it loads
I use XAMPP and PHPStorm
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP z88sm9679wrb.26 - gsmtp
CLIENT -> SERVER: EHLO PhpStorm 2016.1.2
SERVER -> CLIENT: 501-5.5.4 HELO/EHLO argument "PhpStorm 2016.1.2" invalid, closing connection.501 5.5.4 https://support.google.com/mail/?p=helo z88sm9679wrb.26 - gsmtp
SMTP ERROR: EHLO command failed: 501-5.5.4 HELO/EHLO argument "PhpStorm 2016.1.2" invalid, closing connection.501 5.5.4 https://support.google.com/mail/?p=helo z88sm9679wrb.26 - gsmtp
CLIENT -> SERVER: HELO PhpStorm 2016.1.2
SERVER -> CLIENT:
SMTP ERROR: HELO command failed:
SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not connect to SMTP host.
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Here is my code
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require '../PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587; //ssl : 465 --- tls:587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "te.professionnel#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "*****";
//Set who the message is to be sent from
$mail->setFrom('te.professionnel#gmail.com', 'Abou May');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('abou.may#gmail.com', 'Abou May');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I have read lots of documentation, bu I cannot figure out the reason of this error.
Can someone givz me an example code?
What is wrong ?
I read all the section on troubleshooting
"SMTP Error: Could not connect to SMTP host."
This may also appear as SMTP connect() failed or Called Mail() without being connected in debug output. This is often reported as a PHPMailer problem, but it's almost always down to local DNS failure, firewall blocking (for example as GoDaddy does) or other issue on your local network. It means that PHPMailer is unable to contact the SMTP server you have specified in the Host property, but doesn't say exactly why. It can also be caused by not having the openssl extension loaded (See encryption notes below).
I also encounter this problem before.This is not the problem of PhpMailer.But in fact,this is cause by Gmail smtp is denied the connection,therefore you cant connect.
In order for you to solve this problem,you need to
Configure an OAuth2 app in Google Developer Console.
Step is here
If you still cant manage to solve it,I recommend you to use Postmark as your mail server,it is a lot easier to set up the connection.
Here is the guide for the setup of Postmark.For me is a lot easier.Hope it helps.
This is nothing to do with gmail or oauth.
The problem is this line:
CLIENT -> SERVER: EHLO PhpStorm 2016.1.2
When PHPMailer's SMTP client says hello to a server like this, by default it passes the name returned by the internal method serverHostname(). Normally this returns something like localhost.localdomain, or mymac.local (i.e. a real host name), but if that's not available, it tries to figure out what to use - and one of the things it falls back to is the $_SERVER['SERVER_NAME'] super global, which in this case contains PhpStorm 2016.1.2 (I'd guess because you're testing using PHPStorm's built-in web server?), which is not a valid host name, hence the error. This isn't a good move on PHPStorm's part and is probably worth reporting as a bug.
Fortunately you can override automatic determination of the client hostname using the Helo property, which exists precisely for occasions like this. Just do this:
$mail->Helo = 'my.host.name';
substituting either whatever your real host name is, or by calling some other function which gives a usable result.

Can't send email with php using gmail - Authorising error

I'm new to PHP and trying to build a website with registration form. Users should receive an email with a confirmation link (since I have a free hosting server I am trying to use gmail server). The thing is I'm struggling with the email php code. I am trying to use PHPMailer functions as follows:
<?php
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "MYEMAIL#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "MYPASSWORD";
//Set who the message is to be sent from
$mail->setFrom('example#example.com', 'Name S');
//Set an alternative reply-to address
$mail->addReplyTo('example#example.com', 'Name S');
//Set who the message is to be sent to
$mail->addAddress('email#gmail.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML("HELLo");
//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I tried both ports 587 and 465. Also tried both ssl and tls. Every time I try to run the code I get the following error:
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP gw4sm17090153wjc.45 - gsmtp
CLIENT -> SERVER: EHLO spec.dr-manny.co.uk SERVER -> CLIENT:
250-smtp.gmail.com at your service, [185.27.134.36]250-SIZE
35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN
OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250
SMTPUTF8 CLIENT -> SERVER: AUTH LOGIN SERVER -> CLIENT: 334
VXNlcm5hbWU6 CLIENT -> SERVER: bWFubnlzYWVkaUBnbWFpbC5jb20= SERVER ->
CLIENT: 334 UGFzc3dvcmQ6 CLIENT -> SERVER: a2luZ29uZW1vaDk5 SERVER ->
CLIENT: 534-5.7.14
Please log in via your web browser
and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14
https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 -
gsmtp SMTP ERROR: Password command failed: 534-5.7.14
Please log in via your web browser
and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14
https://support.google.com/mail/answer/78754 gw4sm17090153wjc.45 -
gsmtp SMTP Error: Could not authenticate. CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection gw4sm17090153wjc.45 -
gsmtp SMTP connect() failed.
Error: SMTP connect() failed.
Also, I got an email from Gmail with the subject "Someone has your password". I opened the email and found this "
Hi Manny,
Someone just used your password to try to sign in to your Google Account MYEMAIL#gmail.com, using an application such as an email client or mobile device."
I received this email about 15 mins after I ran the php page.
I turned off "2-step verification" and turned off "allow less secure apps". Nothing seems to help. Anyone please could help me?
I had the same issue last year: the code (VB.Net) was OK, so where the credentials. The problem was that gmail didn't like that some app in a web server far away (my hosting) where trying o use your same username.
I fixed it (in two different occasions) by:
Logging in gmail once from that server (using remote desktop).
Logging in here https://accounts.google.com/DisplayUnlockCaptcha from my pc to "unlock" remote connections.
After that test your code again.

Categories