I have a form in my website that should have a capability of sending an email using PHPmailer via gmail. I chose PHPmailer because the free hosting site I'm using does not allow the use of mail() function in php. I know this question has been around in a long time but none of the answers I read from other similar posts worked for me. The root problem is I'm getting an error and I'm not sure if its PHP code related or gmail issue. This is just the last line of error SMTP Error: Could not authenticate
.
I tweaked settings from gmail by turning on access on less-secured app in google settings, went to this link already ->http://www.google.com/accounts/DisplayUnlockCaptcha , reset my password twice without special characters and updated them in my php code. Nothing worked yet so far.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if (isset($_POST['submit'])){
require_once 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 2;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = '@@@'; //My user name
$mail->Password = '@@@'; //My password
$mail->setFrom($_POST['email'],$_POST['name']);
$mail->addAddress('lmnlisbautista@gmail.com');
$mail->addReplyTo($_POST['email']);
$mail->isHTML(true);
$mail->Subject='Form Submission:';
$mail->Body='<h1>Name :'.$_POST['name'].'<br>Email: '.$_POST['email'].
'<br>Message: '.$_POST['message'].'</h1>';
$mail->send();
echo 'Message has been sent!';
} catch(Exception $e){
echo "Message could not be sent! Error: {$mail->ErrorInfo}";
}
}
?>