When I trying to send a email using PHP SMTP email server, following error has occurred. SMTP Error: Could not authenticate. Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
try {
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'xxxx@gmail.com'; //SMTP username
$mail->Password = 'xxxxxxx'; //SMTP password
$mail->SMTPSecure = 'tls'; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('tawiqha@gmail.com', 'reset');
$mail->addAddress($email); //Add a recipient
$mail->addReplyTo('no-replay@gmail.com', 'no-replay');
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'اعادة تعيين كلمة السر';
$mail->Body = $message;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}