简体   繁体   中英

Sending Mail in PHP : SMTP Error: could not authenticate

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM