This question already has answers here:
"SMTP Error: Could not authenticate" in PHPMailer
(21 answers)
Closed 8 years ago.
I receive this error: SMTP Error: Could not authenticate.
This is the code, i think that everything is ok, the password is ok, the emails are ok, ist in localhost,...
<?php
require './class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "email#gmail.com";
$mail->Password = "*********";
$mail->SetFrom('email#gmail.com', 'user');
$mail->AddAddress('email2#gmail.com', 'user');
$mail->Subject = 'this is a text email';
$mail->MsgHTML('content');
$mail->AltBody = 'This is a plain-text message body';
if(!$mail->Send()) {
echo "Error: " . $mail->ErrorInfo;
} else {
echo "Send!";
}
?>
Set SMTPDebug to 2 and see what you get but it seems like you're connecting and quite literally not able to authenticate with google's smtp server, if so your username or password is indeed wrong or being blocked (sometimes gmail will block logins from connections it's never seen before until you login and assure it everything's fine).
Related
This question already has answers here:
Phpmailer error "Could not instantiate mail function"
(18 answers)
Closed 3 years ago.
I have an issue in sending mail from PHPMailer. When I try to send a message, I get an error message which is
Message could not be sent. Mailer Error:Could not instantiate mail function.
My SMTP is correct, I can't find the problem. Can anyone help me find the problem? Thanks
<?php
require "autoload.php";
if(isset($_POST["submit"])){
$mail = new PHPMailer(true);
$sender = "demo#gmail.com";
//SMTP
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = demo#gmail.com; // SMTP username
$mail->Password = '1234567'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS
$mail->Port = 587; // TCP port to connect
$mail->From = "demo#gmail.com";
$mail->FromName = 'Mailer';
$mail->setFrom(demo#gmail.com, 'Mailer');
$mail->addAddress($_POST["receiver"]); // Name is optional
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
You have set various SMTP-related properties, but you have not told PHPMailer to use SMTP!
Add this line:
$mail->isSMTP();
You're also enabling exceptions (passing true to the constructor), but have not wrapped your code in a try/catch block. See the examples provided with PHPMailer for how to deal with that.
please recheck your configuration like , user/password, PORT number
Connect to smtp.gmail.com on port 465, if you're using SSL. Else use 587.
Also make sure less secure app is turn on
https://support.google.com/accounts/answer/6010255?hl=en
This question already has answers here:
SMTP connect() failed PHPmailer - PHP
(17 answers)
Closed 4 years ago.
I'm a beginner in php so I had written code for testing which will send an email using php.
I'm using "PHPMailer-5.2.27" to sent email with SMTP on php, but when I run the code the email not sent. I don't know what is the problem. Can anyone help me out!
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->Host = "smtp.gmail.com";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = "example#gmail.com";
$mail->Password = "xxxxxxxx";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Subject = "test mail";
$mail->Body = "just a test";
$mail->setFrom('example#gmail.com','aaaa');
$mail->addAddress('example#gmail.com');
if ($mail->send())
echo "mail is sent";
else
echo "something wrong ";
?>
Assuming you have allowed your Gmail account to authorize the server from where you are sending an email.
use $mail->ErrorInfo; in order to debug your issue, this will clarify what is the actual failure reason for sending an email you can refer the below code I have shared.
<?php
require "PHPMailer/PHPMailerAutoload.php";
echo !extension_loaded('openssl')?"Not Available":"Available"; //check openssl is available or not
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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 = "mygmailaccount#gmail.com";
$mail->Password = "**********";
$mail->SetFrom("example#gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("toaddress#gmail.com");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
This question already has answers here:
Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587. How to fix?
(13 answers)
Closed 6 years ago.
This is sample code from phpmailer file. I edit some of code and it gives me error. i tried to change port number as well but gives me same error ever time
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "mygmail#gmail.com"; // SMTP username
$mail->Password = "password_is_correct"; // SMTP password
$mail->From = "mygmail#gmail.com";
$mail->FromName = "Harsh Patel";
$mail->AddAddress("mygmail#gmail.com"); // name is optional
$mail->WordWrap = 50; // set word wrap to 50 characters
$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. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Try with these changes as you are using gmail smtp
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Im trying to create an email service with PHPMailer and im receiving and authentication error and the username and password are correct .
This is my code:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = “myemail#gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example#gmail.com', 'Nicolas El Mir');
//Set who the message is to be sent to
$mail->addAddress('example#gmail.com', 'Nicolas Mir');
//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
$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!";
}
?>
Thanks for your help !
Your problem starts here:
$mail->Username = “myemail#gmail.com";
$mail->Password = “my gmail password”;
//Set who the message is to be sent from
$mail->setFrom(‘example#gmail.com', 'Nicolas El Mir');
You see those curly/smart quotes? “ ” ‘
“myemail...
“my gmail password”
‘example...
They're choking your script.
$mail->Username = "myemail#gmail.com";
$mail->Password = "my gmail password";
//Set who the message is to be sent from
$mail->setFrom('example#gmail.com', 'Nicolas El Mir');
Had you error reporting set to catch/display, it would have told you about it.
http://php.net/manual/en/function.error-reporting.php
I'm really hoping that is your actual code too, rather than just a bad Word Processor paste.
Plus, make sure you do have a Webserver/PHP installed and that mail is enabled on it.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Plus, pulled from this answer https://stackoverflow.com/a/16048485/ which may be the same problem for you, the port number, etc.
$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.
and pulled from another answer https://stackoverflow.com/a/31194724/ in that question:
The solution was that I had to remove this line:
$mail->isSMTP();
I think it comes from your gmail account. Your account doesn't allow less secure apps. You can turn it on through this link Google security sttings
Try this copy libeay32.dll and ssleay32.dll to windows/system2. These two files you will find in php folder (main folder). With this you can enable php extensions. i.e curl etc. If this doesn't work, mention your error message. or check out some youtube videos! That would be suitable. Thank you.
I manage to sent message to my gmail account using phpmailer library through local
host in my xampp but the message has 'root user' shown instead of the gmail account set to $mail->SetFrom how to remove this 'root user' and show the appropriate name.
<?php
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true;
$mail->Mailer = "smtp";
$mail->SMTPSecure = "tls";
$mail->Username = "georgediamei1234#gmail.com";
$mail->Password = "password";
$mail->Host = 'ssl://smtp.gmail.com'; // SMTP server
$mail->Port = 465;
$mail->FromName ='georgediamei1234#gmail.com';
$mail->AddAddress("nowtonkhurai#gmail.com");
$mail->Subject = "";
$mail->Body = "Hello, <b>Is photo attached</b>!\n\n This message uses HTML entities!";
$mail->addStringAttachment("$string","08DsAlg.pdf", "base64","application/pdf");
if($mail->send()) {
echo "Email sent";
} else {
echo 'Not send '.$mail->ErrorInfo;
}
?>
$mail->setFrom('from#example.com', 'Mailer');
I also got stumbled with that root problem. You have to set a mail id and a name in setFrom property. As the question was asked 2 years ago it might not be helpful to you but it might help others who get stuck with the same stuff.
use both function for name and email address like this :
$mail->From = "from#example.com";
$mail->FromName = "Mailer";