I am composing a email function for sending newsletters to many recipients in PHP.
<?php
require_once('class.phpmailer.php');
require_once('class.smtp.php');
$mailer = new Mailer();
Class Mailer{
function __construct(){
$dbCon = mysqli_connect("localhost", "root", "", "dbppa");
}
public function sendMail($e,$message,$subject){
$mail = new PhpMailer;
$body = $message;
$subject = "PPA Newsletter";
$clientName = $e;
$mail->SMTPDebug = 2;
$mail->CharSet = 'utf-8';
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'tls://smtp.gmail.com:587';
$mail->Port = 587;
$mail->Username = "myemail@mail.com";
$mail->Password = "password";
$mail->SMTPSecure = 'tls';
$mail->setFrom("myemail@mail.com","PPA Administrator");
$mail->addReplyTo('myemail@mail.com', 'PPA Admin');
$mail->Subject = $subject;
$mail->AltBody = "This is the official news letter from PPA ";
$mail->Body = $body;
$address = $e;
$mail->AddAddress($address, $clientName);
if(!$mail->Send()){
echo "NOT SENT";
}
else{
echo "SENT";
}
}
}//class mailer
?>
In the code given above. I provided correct email username and email password. From Gmail. But it does not send the given email message to the recipient(s). It always return an ErrorInfo of SMTP Error:
Could not authenticate. SMTP connect() failed.
Why? Please help.