I'm trying to send an email from my site hosted online; it's actually executing the $mail->send() command successfully but the mail doesn't reach its recipient. I'm using the same code in localhost and it works properly. Here's my code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'phpmailer/Exception.php';
require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
function SendEmail(){
try {
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host='smtp.gmail.com';
$mail->Port=587;
$mail->SMTPAuth=true;
$mail->SMTPSecure='tls';
$mail->Username='myusernameemail';
$mail->Password='mypassword';
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->setFrom('testmail','Test Mail');
$mail->addAddress('myusernameemail');
$mail->addReplyTo('testmail','Test Mail');
$mail->isHTML(true);
$mail->Subject='TestTitle';
$mail->Body='<h1 align=center>TestContent</h1>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
I get the 'Message has been sent' but the email never reaches its recipient, unlike the localhost one which does.