我正在尝试从在线托管的网站发送电子邮件;它实际上已成功执行$ mail-> send()命令,但邮件未到达其收件人。我在本地主机中使用相同的代码,并且可以正常工作。这是我的代码:
<?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}";
}
}
?>
我收到“消息已发送”,但电子邮件从未到达收件人,这与本地主机不同。