0

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.

10
  • are you sure that you've given correct gmail id and password? Commented Oct 9, 2012 at 7:23
  • yes! it send mail from localhost but not from server. Commented Oct 9, 2012 at 7:23
  • try adding this $mail->SMTPSecure = "ssl"; Commented Oct 9, 2012 at 7:25
  • That'll set prefix to the server Commented Oct 9, 2012 at 7:25
  • 1
    Login to your Gmail on Gmail itself, does it show some sort of warning that "something"is trying to access your account by a certain ip-address? Because in that case you have to allow that ip-address to be trusted. After that it should work. At least, that was the solution in my case. I also highly suggest not to use your private mail but a special mail address for the project. Otherwise you could be banned yourself from Gmail ;)
    – Jelmer
    Commented Oct 9, 2012 at 7:28

5 Answers 5

2

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

0

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

0

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

0

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.

0

It can happen due to various reasons. In my case it worked by changing from

$phpmailer->isSMTP();

to

$phpmailer->Mailer     = 'smtp';

worked.

Not the answer you're looking for? Browse other questions tagged or ask your own question.