I tried to use php mailer but errors as follows.
SMTP -> FROM SERVER:
SMTP -> FROM SERVER:
SMTP -> ERROR: EHLO not accepted from server:
SMTP -> FROM SERVER:
SMTP -> ERROR: HELO not accepted from server:
SMTP -> ERROR: AUTH not accepted from server:
SMTP -> NOTICE: EOF caught while checking if connectedSMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.
and my code
<?php
require("class.phpmailer.php")
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPDebug = 2;
$mail->Username = "admin#xxxxxxxxxxxx.in";
$mail->Password = "xxxxxxxx";
$mail->From = "admin#xxxxxxxxxxxx.in";
$mail->FromName = "Mailer";
$mail->AddAddress("xxxx#yahoo.co.in", "mine");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Here is the subject"
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send()) {
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
I was getting this due to wrong port for SSL.
SSL = 465
TLS = 587
See:
http://mail.google.com/support/bin/answer.py?hl=en&answer=13287
Some servers (especially shared hosting) will block you from using SSL with SMTP, I had the same problem once.
Either change host if you can, try using the default PHP mail() function or send through another mail server that does not require SSL e.g. port 25 not 465.
Something like AuthSMTP would be your best bet for an alternate mail server.
I had the same problems, it seems that we have
to set the SMPTSecure value.
First I changed the port from 465 to 587 and added:
$mail->SMTPSecure = "tls";
and it worked :)
If you are working in your localhost just go to the PHP Extention and enable or check the php_openssl
it will be able to access the SSL ports.
try this code
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 = 465;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'ssl';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "admin#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "admin123";
$mail->setFrom('admin3#gmail.com', 'development'); //add sender email address.
$mail->addAddress('admins#gmail.com', "development"); //Set who the message is to be sent to.
//Set the subject line
$mail->Subject = $response->subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = 'Name: '.$data['name'].'<br />Location: '.$data['location'].'<br />Email: '.$data['email'].'<br />Phone:'.$data['phone'].'<br />ailment: '.$data['ailment'].'<br />symptoms: '.$data['symptoms'];
//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.gif');
//$mail->SMTPAuth = true;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Had the same issue, Change port No in opencart mail setting to 587 and works fine
May be because of fire wall?
If you can't sign in to Google Talk,
or you're receiving an error that
says, Could not authenticate to
server, check if you have personal
firewall software installed, or if
your computer is behind a proxy server
that requires a username and password.
http://www.google.com/support/talk/bin/answer.py?hl=en&answer=30998
I use the same script for several clients and only run into this problem when deploying to Amazon EC2 cloud providers (such as Openshift).
These are tried and tested settings in phpmailer:
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587;
'but' Google blocks these services as an 'anti-spam' / political maneuver, and this has caught me out because it works locally and on most hosting providers, there is nothing much you can do when they don't accept outbound messages from your hosts DNS / IP. Accept it and move on by looking for another smtp server to route messages through.
not sure but try $mail->Host = "smtp.gmail.com" =>$mail->Host = "smtp.google.com"
Related
I followed all of the instructions in this question:
SMTP connect() failed PHPmailer - PHP
But still I never succeeded in getting the PHPMailer to work. I searched elsewhere - no solutions.
You can view the results here: https://unidrones.co.za/JuneSecond
When I try to send a test email using my gmail account credentials, it returns the "SMTP connect() failed" error.
I am using this template code:
<?php
require 'PHPMailerAutoload.php';
if(isset($_POST['send']))
{
$email = $_POST['email'];
$password = $_POST['password'];
$to_id = $_POST['toid'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $email;
$mail->Password = $password;
$mail->addAddress($to_id);
$mail->Subject = $subject;
$mail->msgHTML($message);
if (!$mail->send()) {
$error = "Mailer Error: " . $mail->ErrorInfo;
echo '<p id="para">'.$error.'</p>';
}
else {
echo '<p id="para">Message sent!</p>';
}
}
else{
echo '<p id="para">Please enter valid data</p>';
}
?>
Edit: I don't know if there's a new way to send emails through PHP now. All the tutorials and lessons I am using teaches it this way (i.e. using the PHPMailer library).
I had a tough time finding the PHPMailer library that includes the PHPMailerAutoload.php file, which makes me think it's a little outdated or deprecated, but how else would I send emails? I don't know.
The reason you're having a hard time finding PHPMailerAutoload.php is because it's old and no longer supported. Get the latest and base your code on the gmail example provided. If you're new to PHP, learn to use composer.
These three lines are conflicting:
$mail->Host = 'ssl://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
The ssl:// in Host overrides the tls in SMTPSecure, resulting in it trying to use implicit TLS to a port expecting explicit TLS. Either use ssl with port 465 or tls with port 587, not other combos. Regardless, it appears that's not your problem anyway.
As the troubleshooting guide says about this exact "SMTP connect() failed" error:
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 another 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 then goes on to describe several techniques you can use to try to diagnose exactly why you can't connect. Amazing stuff, documentation.
I tried your form with some random data and saw that you failed to include the most important error message in your question:
2018-06-02 14:47:25 SMTP ERROR: Failed to connect to server: Network is unreachable (101)
2018-06-02 14:47:25 SMTP connect() failed
That suggests your ISP is probably blocking outbound SMTP, so you have your answer - perhaps confirm that using the steps the guide suggests (telnet etc), and refer to your ISP's docs or support.
You also have a major omission - you're not setting a "from" address:
$mail->setFrom('myname#gmail.com', 'My Name');
Note that if you're sending through gmail, you can only use your account's address, or preset aliases (set in gmail prefs), not arbitrary addresses.
Meanwhile, this is a somewhat crazy thing to implement as you have anyway - why would anyone ever enter their gmail credentials on a form like that?
did you try to use the following config?
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->CharSet = 'UTF-8';
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 4; // 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 = 'your gamil id'; // SMTP username
$mail->Password = 'gmail password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('set from address', 'name');
$mail->addAddress('recipient address', 'Joe User'); // Add a recipient
$mail->addReplyTo('reply address', 'Information');
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'myhost'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'myusername'; // SMTP username
$mail->Password = 'mypassword'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 1060; // TCP port to connect to
$mail->setFrom('test#gmail.com', 'test');
$mail->isHTML(true); // Set email format to HTML
$mail->addAddress('test#gmail.com'); // Add a recipient
for($i=0;$i<1;$i++){
$mail->Subject = "test bulk email ".$i;
$mail->Body = "this is email ".$i;
if(!$mail->Send())
{
$error_message = "Mailer Error: " . $mail->ErrorInfo;
}
}
echo $error_message;
When I run this code, I get this error:
Connection: opened 2018-03-05 09:24:25 SERVER -> CLIENT: 2018-03-05 09:24:25 SMTP NOTICE: EOF caught while checking if connected 2018-03-05 09:24:25 Connection: closed 2018-03-05 09:24:25 SMTP Error: Could not connect to SMTP host.
How do I fix this?
You're using an unusual port number for SMTP submission, so check you have the right one for your host. It would usually be 587 when using SMTPSecure = 'tls'.
You're setting a gmail from address while sending through another host; that will cause you to fail SPF checks and your messages will either be blocked or spam filtered.
If you want to send to a list, follow the mailing list example provided with PHPMailer - your code contains some mistakes that will probably result in duplicate messages.
Mail not send to user via SMTP using sendgrid.it shows SMTP error
code
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.sendgrid.net"; // SMTP server
$mail->SMTPSecure = "SSL";
$mail->SMTPAuth = true;
$mail->Username = "szdfsdf";
$mail->Password = "sdfsdfsdfsdf";
$mail->Port = "465";
$mail->From = "tests#gmail.com";
$mail->FromName = "Test";
$mail->AddAddress("test#services.in");
$message = "hi how r u in medapps?";
$message = trim($message);
$mail->Subject = "from test";
$mail->Body = trim($message);
if(!$mail->Send()){
echo "Mailer error: ".$mail->ErrorInfo;
}
else{
echo "Mail triggered to alert the status!";
}
result
Mailer error: SMTP Error: Could not connect to SMTP host.
When connecting to SendGrid via SMTP using API Keys, you need to use apikey as the username.
Also, since you just shared that API Key publicly here, you need to destroy it and make a new one. It's no longer secure.
SendGrid does support port 465 for SSL connectiong, but as Jim said, you need to test your connection and see if your server is allowing that connection. If not, try a different port as recommended on that article.
I would recommend trying port 587 with a TLS connection.
so i have been working with gmail and using php mailer and it was working fine.
i tried to do the same with yahoo mail but it doent seem to work.
i have tried various ports and settings but it isnt working.
here is the code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHPMailer - GMail SMTP test</title>
</head>
<body>
<?php
//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.mail.yahoo.com';
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587; //995 and 465 port tried but not working
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tsl';//only ssl tried not working
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "sender#yahoo.com";
//Password to use for SMTP authentication
$mail->Password = "sender_pass";
//Set who the message is to be sent from
$mail->setFrom('sender_#yahoo.com', 'sender_name');
//Set who the message is to be sent to
$mail->addAddress('receiver#yahoo.com', 'receiver_name');
//Set the subject line
$mail->Subject = 'PHPMailer YMail SMTP test';
$mail->msgHTML(file_get_contents('phpmailer/examples/contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
$mail->addAttachment('phpmailer/examples/images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
</body>
</html>
and i get the following error:
SERVER -> CLIENT: 220 smtp.mail.yahoo.com ESMTP ready
CLIENT -> SERVER: EHLO localhost
CLIENT -> SERVER: AUTH LOGIN
SERVER -> CLIENT: 530 5.7.0 Must issue a STARTTLS command first
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 Bye
SMTP connect() failed.
Mailer Error: SMTP connect() failed.
The correct is tls, you have a spell mistake:
$mail->SMTPSecure = 'tls'
2020 - Google brought me here for something similar.
Finally got phpmailer working with yahoo.
My Findings:
* Ports -> Both ssl/465 and tls/587 work with Yahoo. You dont have to
worry about that.
* From Address -> Cannot be something arbitrary, just use your
yahoo account email here
* ReplyTo Address -> This too must be a valid yahoo account
or you may leave this blank.
And ...
for programmatic usage of yahoo with phpmailer, you need to use another passwd ( not the password that you use to login manually via the web - got this tip from a non stackoverflow site, maybe expert xchange? ). This password is called the App Password. You have to generate it from inside your account.
Account Info -> Account Settings -> Manage Your App Passwords
-> Select Other App -> Key in 'AutoMailer'
or anything you like and generate the passwd - Its a 4 word passwd.
Use this passwd in your phpmailer script.
Everything should be fine now.
The yahoo SMTP host is: smtp.mail.yahoo.fr, the port: 465 and SSL is required.
So with PHPMailer:
$mail->Host = "smtp.mail.yahoo.fr, ";
$mail->Username = "youryahooadd#yahoo.fr";
$mail->Password = "youryahoopw";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail = new PHPMailer();
$body = "<h1>hello, world!</h1>"
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.mail.yahoo.com"; // sets YAHOO as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "yourusername#gmail.com"; // GMAIL username
$mail->Password = "yourpassword"; // GMAIL password
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->Subject = "PHPMailer Test Subject via smtp (Gmail), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "whoto#otherdomain.com";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
I would like to send an email using Gmail SMTP server through PHP Mailer.
this is my code
<?php
require_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'MyUsername#gmail.com';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;
$mail->From = 'MyUsername#gmail.com';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('anotherValidGmail#gmail.com');
$mail->AddReplyTo('phoenixd110#gmail.com', 'Information');
$mail->IsHTML(true);
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->Body = "Hello";
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message sent!";
}
?>
but I receive this following error
Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail#gmail.com
SMTP server error: SMTP AUTH is required for message submission on port 587
my domain is vatandesign.ir
$mail = new PHPMailer(); // create a new object
$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->Username = "email#gmail.com";
$mail->Password = "password";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
This code above has been tested and worked for me.
It could be that you needed $mail->SMTPSecure = 'ssl';
Also make sure you don't have two step verification switched on for that account as that can cause problems also.
UPDATED
You could try changing $mail->SMTP to:
$mail->SMTPSecure = 'tls';
It's worth noting that some SMTP servers block connections.
Some SMTP servers don't support SSL (or TLS) connections.
So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.
I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.
All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.
The solution was that I had to remove this line:
$mail->isSMTP();
Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.
Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.
You can generate an application specific password by following these instructions:
https://support.google.com/accounts/answer/185833
Then set $mail->Password to your application specific password.
It seems that your server fails to establish a connection to Gmail SMTP server.
Here are some hints to troubleshoot this:
1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini).
2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config.
I hope you'll sort this out quickly!
Google treat Gmail accounts differently depending on the available user information, probably to curb spammers.
I couldn't use SMTP until I did the phone verification. Made another account to double check and I was able to confirm it.
this code working fine for me
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 0;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = $hostname;
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = $sender;
$mail->Password = $mail_password;
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "ssl";
//Set TCP port to connect to
$mail->Port = 465;
$mail->From = $sender;
$mail->FromName = $sender_name;
$mail->addAddress($to);
$mail->isHTML(true);
$mail->Subject = $Subject;
$mail->Body = $Body;
$mail->AltBody = "This is the plain text version of the email content";
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo 'Mail Sent Successfully';
}
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
If you are using cPanel you should just click the wee box that allows you to send to external servers by SMTP.
Login to CPanel > Tweak Settings > All> "Restrict outgoing SMTP to
root, exim, and mailman (FKA SMTP Tweak)"
As answered here:
"Password not accepted from server: 535 Incorrect authentication data" when sending with GMail and phpMailer
Anderscc has got it correct. Thanks. It worked for me but not 100%.
I had to set
$mail->SMTPDebug = 0;
Setting it to 1, can cause errors especially if you are passing some data as json to next page. Example - Performing verification if mail is sent, using json to pass data through ajax.
I had to lower my gmail account security settings to get rid of errors:
" SMTP connect() failed " and " SMTP ERROR: Password command failed "
Solution:
This problem can be caused by either 'less secure' applications trying to use the email account (this is according to google help, not sure how they judge what is secure and what is not) OR if you are trying to login several time in a row OR if you change countries (for example use VPN, move code to different server or actually try to login from different part of the world).
Links that fix the problem (you must be logged into google account):
view recent attempts to use the account and accept suspicious access.
link to disable the feature of blocking suspicious apps/technologies:
https://www.google.com/settings/u/1/security/lesssecureapps
Note:
You can go to the following stackoverflow answer link for more detailed reference.
https://stackoverflow.com/a/25175234
I have found a solution and it is working.
Basic settings:
$mail->IsHTML(true);
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_CLIENT;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = 587;
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = 'user#gmail.com';
//Password to use for SMTP authentication
$mail->Password = 'PASSWORD_HERE';
//Set who the message is to be sent from
$mail->setFrom('email#email.ext', 'From where it is sent');
//Set an alternative reply-to address
$mail->addReplyTo('email#email.ext', 'Reply To');
//Set who the message is to be sent to
$mail->addAddress('email#email.ext');
//Set the subject line
$mail->Subject = 'Email subject';
// Body message
$mail->Body = '
<h1>Message details</h1>
';
// Send message
if( $mail->send() ) {
echo "Sent"
} else {
echo "Not Sent";
}
If you've already tried everything, try doing this:
Disable the feature of blocking suspicious apps/technologies https://www.google.com/settings/u/1/security/lesssecureapps
Clear the Captcha https://accounts.google.com/b/0/DisplayUnlockCaptcha
Do the test.
Here are some articles of interest that helped me solve this:
https://support.google.com/a/thread/108782439/smtp-error-password-command-failed-535-5-7-8-username-and-password-not-accepted?hl=en&msgid=108963583
https://bobcares.com/blog/phpmailer-smtp-error-password-command-failed/
I just wanted to share my experience with phpMailer , that was working locally (XAMPP) but wasn't working on my hosting provider.
I turned on phpMailer error reporting
$mail->SMTPDebug=2
I got 'Connection refused Error'
I email my host provider for the issue, and they said that they would open the SMTP PORTS 25, 465, 587.
Then I got the following error response "SMTP ERROR: Password command failed:"...."Please log in via your web browser and then try again"...."SMTP Error: Could not authenticate."
So google checks if your are logged in to your account (I was when I ran the script locally through my browser) and then allows you to send mail through the phpMailer script.
To fix that:
1: go to your Google account -> security
2: Scroll to the Key Icon and choose "2 way verification" and follow the procedure
3: When done go back to the key icon from google account -> security and choose the second option "create app passwords" and follow the procedure to get the password.
Now go to your phpMailer object and change your Google password with the password given from the above procedure.
You are done.
The code:
require_once('class.phpmailer.php');
$phpMailerObj= new PHPMailer();
$phpMailerObj->isSMTP();
$phpMailerObj->SMTPDebug = 0;
$phpMailerObj->Debugoutput = 'html';
$phpMailerObj->Host = 'smtp.gmail.com';
$phpMailerObj->Port = 587;
$phpMailerObj->SMTPSecure = 'tls';
$phpMailerObj->SMTPAuth = true;
$phpMailerObj->Username = "YOUR EMAIL";
$phpMailerObj->Password = "THE NEW PASSWORD FROM GOOGLE ";
$phpMailerObj->setFrom('YOUR EMAIL ADDRESS', 'THE NAME OF THE SENDER',0);
$phpMailerObj->addAddress('RECEIVER EMAIL ADDRESS', 'RECEIVER NAME');
$phpMailerObj->Subject = 'SUBJECT';
$phpMailerObj->Body ='MESSAGE';
if (!phpMailerObj->send()) {
echo "phpMailerObjer Error: " . $phpMailerObj->ErrorInfo;
return 0;
} else {
echo "Message sent!";
return 1;
}
If anyone there who is not getting a working answer, they can try this.
Note: I am using PHPMailer 5.2.23.
<?php
date_default_timezone_set('Asia/Kolkata');
require './Libraries/PHPMailer5/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
// Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'localhost';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "yourWebmailUsermail";
$mail->Password = "yourWebmailPassword";
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->setFrom('formEmailAddress', 'First Last');
$mail->addAddress('toEmailAddress', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("<h1>Hi Test Mail</h1>");
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
I think it is connection issue you can get code here http://skillrow.com/sending-mail-using-smtp-and-php/
include(“smtpfile.php“);
include(“saslfile.php“); // for SASL authentication $from=”my#website.com“; //from mail id
$smtp=new smtp_class;
$smtp->host_name=”www.abc.com“; // name of host
$smtp->host_port=25;//port of host
$smtp->timeout=10;
$smtp->data_timeout=0;
$smtp->debug=1;
$smtp->html_debug=1;
$smtp->pop3_auth_host=””;
$smtp->ssl=0;
$smtp->start_tls=0;
$smtp->localhost=”localhost“;
$smtp->direct_delivery=0;
$smtp->user=”smtp username”;
$smtp->realm=””;
$smtp->password=”smtp password“;
$smtp->workstation=””;
$smtp->authentication_mechanism=””;
$mail=$smtp->SendMessage($from,array($to),array(“From:$from”,”To: $to”,”Subject: $subject”,”Date: ”.strftime(“%a, %d %b %Y %H:%M:%S %Z”)),”$message”);
if($mail){
echo “Mail sent“;
}else{
echo $smtp->error;
}