Sending Mail in PHP : SMTP Error: could not authenticate

Sandip Armal Patil picture Sandip Armal Patil · Oct 9, 2012 · Viewed 19.1k times · Source

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 = "[email protected]";
$mail->AddAddress("[email protected]");

$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.

Answer

transilvlad picture transilvlad · Oct 9, 2012

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";