Sending Mail in PHP : SMTP Error: could not authenticate - php

I am trying to send mail in PHP. I use same code for localhost and server. But when I use the code on server, it doesn't seem to work:
SMTP Error: Could not authenticate. Message was not sent.Mailer error: SMTP Error: Could not authenticate.
Here is my code for your reference.
require("class.phpmailer.php"); // path to the PHPMailer class
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Mailer = "smtp";
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "myname"; // SMTP username
$mail->Password = "password"; // SMTP password
$mail->From = "me#gmail.com";
$mail->AddAddress("sample#gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
I've done lots of searching, nothing pops up. Any help would be greatly appreciated.

Try TLS and port 587:
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
As you can see you could also specify that you want SSL connection in the SMTPSecure variable and just use the host as smtp.gmail.com
Also try replacing this $mail->IsSMTP(); with this $mail->Mailer = "smtp";

just check this out it may be helpful:
it's telnet for smtp connection working or not? In windows 7 firewall blocked(default) smtp connection so,
http://support.microsoft.com/kb/153119

Some hosting providers, such as 1and1 block outgoing email ports, such as 25, 465 and 587. The only solution AFAIK is to change hosting provider.
Try making a simple php file that just does a "fsokopen" to the port you are trying to use, and see if that works

It happen because Password contain special character. I tried using "\"before special character like Password="djgh\^dfgfjk". But it didn't help me.
I just create new id and simple password without any special character and amazingly it's work.
Use password with out special character.

It can happen due to various reasons. In my case it worked by changing from
$phpmailer->isSMTP();
to
$phpmailer->Mailer = 'smtp';
worked.

Related

PHPMailer not working: Is there a new way of doing this?

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';
}

mail not send via SMTP API using sendgrid

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.

phpmailer issue. Unable to get rid of the smtp connect() error

I'm trying to send a mail using phpmailer but getting this error "SMTP connect() failed". I've already allowed less secure apps on gmail. Is all the smtp setting correct? Please guide me where I'm wrong.
<?php
require_once 'PHPMailer-master/PHPMailerAutoload.php';
require 'PHPMailer-master/class.phpmailer.php';
require 'PHPMailer-master/class.smtp.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->host = 'smtp.gmail.com';
$mail->username = 'mymail#gmail.com';
$mail->password = 'mypassword';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = true;
$mail->isHTML();
$mail->Subject = 'form data';
$mail->Body = 'this is the body of message';
$mail->FromName = 'The Form';
$mail->AddAddress('junaidshekh21#gmail.com','Junaid Shaikh');
if($mail->send())
{
echo "sent successfully";
die();
}
else
{
echo "could not send";
}
?>
Use ErrorInfo in phpmail to see what's wrong
add this before sending the email
$mail->SMTPDebug = 2; //enables SMTP debug information (for testing)
and change from
echo "could not send";
to
echo "Mailer Error: " . $mail->ErrorInfo;
Notes:
Read the phpmail troubleshooting, specifically the part that talks
about "SMTP Error: Could not connect to SMTP host."
Use the official gmail example from the phpmailer github repo.
You may have to allow less secure apps to access your gmail
account
Your ip may be blocked by google.
UPDATE:
From the official phpmailer gmail example:
$mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
Your PHPmailer settings are ok i think it was less secure app access problem.
you must make your gmail to less secure to get access.
got to this url https://www.google.com/settings/security/lesssecureapps
to access your gmail as SMTP.

PHPMailer not working in my server

I built a droplet with Ubuntu with DigitalOcean and I'm trying to configure it to send emails with SMTP.
I know DigitalOcean blocks SMTP over IPv6 but not over IPv4 so I disabled IPv6 as this post says.
My script still doesn't work. I've tried with ports 25, 465 and 587. TLS and SSL.
I've installed sendmail for Ubuntu 14.04 but not working.
This is my script:
<?php
require 'mail/PHPMailerAutoload.php';
$mail = new PHPMailer;
$to = $_GET['email'];
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'rafawins#gmail.com';
$mail->Password = '***';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('rafawins#gmail.com', 'Rafael');
$mail->addAddress($to);
$mail->isHTML(true);
$mail->Subject = 'Subject';
$contents = ob_get_contents();
$mail->Body = "ao!";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
print_r(error_get_last());
?>
The error presented is:
SMTP connect() failed.
I'm interested in sending email using SMTP so ->isSMTP() is required!
Where am I wrong?
Thank you very much.
EDIT:
doing: telnet smtp.gmail.com 587
I get:
Trying 74.125.133.108...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP w6sm13897014wjy.31 - gsmtp
and doing: openssl s_client -connect smtp.gmail.com:465 I get an answer as well...
What's wrong?
Beware of using Gmail from different devices. google doesn't allow and block immediately a location from where an account is used where it isn't supposed to use (in the eyes of google ofcourse).
On live server for send Mail using SMTP, Do it.
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
and Comment $mail->IsSMTP();
It's Working For Me....
Try commenting out line 8.
// $mail->isSMTP();

PHP mailer error

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"

Categories