PHPMailer 不会给出错误消息或发送电子邮件只是白屏

PHPMailer doesnt give error messages or send emails just a white screen

我正在尝试使用 PHP 邮件程序发送电子邮件。什么都没发生,我得到的只是网络浏览器中的白屏。我使用了不同的代码示例,将 ssl 更改为 tls 端口 587 到 465 使用需要 autoload.php phpmailer.php smtp.php 任何我能找到的东西来指示问题出在哪里。该代码与 https://subinsb.com/send-mails-via-smtp-server-gmail-outlook-php 上的代码完全相同,除了电子邮件凭据等。如果您有任何想法,或者如果您看到我没有看到的东西,我们将不胜感激。

<?php
$account = "xxxxxxxxx@gmail.com";
$password = 'xxxxxx';
$from = 'xxxxxxxx@gmail.com';
$from_name = 'Adam Johnson';
$subject = 'Test';
$msg = 'This is a test';
$to = "xxxxxxxxx@hotmail.com";

require ('/PHPMailer/class.phpmailer.php');

$mail->SMTPDebug = 3;
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->Username = $account;
$mail->Password = $password;
$mail->SMTPSecure = 'tls';
$mail->From = $from;
$mail->FromName = $from_name;
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $msg;
$mail->addAddress($to);

if (!mail->send()){
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
else {
echo 'Message sent!';
}
?>

这个

require ('/PHPMailer/class.phpmailer.php');

$mail->SMTPDebug = 3;
$mail = new PHPMailer();

应该是这样的;

require ('/PHPMailer/class.phpmailer.php');

$mail = new PHPMailer();
$mail->SMTPDebug = 3;

您尚未在代码中初始化 $mail 变量,但您正试图在一个不存在的对象上设置 $mail->SMTPDebug = 3;

编辑

代码中还有一个错字。 if (!mail->send()){ 行缺少 $ 变量声明。确保它显示为 if (!$mail->send()){