PHPMailer 的致命错误
FatalError with PHPMailer
我正在尝试使用 PHPMailer 通过 gmail SMTP 服务器发送邮件,但出现致命错误,我找不到任何解决方案
我的php文件
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require "phpmailer/PHPMailer.php";
require "phpmailer/SMTP.php";
require "phpmailer/Exception.php";
$error_msg=null;
$success_msg=null;
if ($_POST) {
//$name = isset($_POST['name']) ? filter_var($_POST['name'], FILTER_SANITIZE_STRING) : null;
//$email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) : null;
//$message = htmlspecialchars($_POST['message']);
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) || empty($email) || empty($message)) {
$error_msg = 'Fill out required entry fields!';
}
if (is_null($error_msg)) {
$mail = new PHPMailer(TRUE);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "mail@gmail.com";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress("mail@gmail.com");
$mail->Subject = "Nowa wiadomość z formularza kontaktowego";
$mail->Body = $message;
$mail->send();
$success_msg = "Message sent.";
}
}
?>
html形式
<div class="h-100 d-flex justify-content-center align-items-center">
<div class="row">
<div class="col-md-12 rounded">
<?php echo $lang['contact_desc']?>
<?php if ($error_msg) : ?>
<div class="alert alert-danger"><?php echo $error_msg; ?></div>
<?php endif; ?>
<?php if ($success_msg) : ?>
<div class="alert alert-success"><?php echo $success_msg; ?></div>
<?php endif; ?>
<form id="myform" method="post">
<label for="name"><?php echo $lang['name']?></label>
<input type="text" name="name" id="name" placeholder="<?php echo $lang['name']?>" required>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="example@example.com" required>
<label for="message"><?php echo $lang['message']?></label>
<textarea name="message" id="message" placeholder="<?php echo $lang['msg_content']?>" required></textarea>
<input type="submit" name="submit" value="<?php echo $lang['submit']?>">
</form>
</div>
</div>
</div>
我得到的错误:
致命错误:未捕获错误:未定义常量 PHPMailer\PHPMailer\FILTER_FLAG_HOST_REQUIRED" in C:\xampp\htdocs\phpmailer\PHPMailer.php:3598 堆栈跟踪:#0 C:\xampp\htdocs\phpmailer\PHPMailer.php(3564): PHPMailer\PHPMailer\PHPMailer::isValidHost('localhost') #1 C:\xampp\htdocs\phpmailer\PHPMailer.php(2304): PHPMailer\PHPMailer\PHPMailer->serverHostname( ) #2 C:\xampp\htdocs\phpmailer\PHPMailer.php(1421): PHPMailer\PHPMailer\PHPMailer->createHeader() #3 C:\xampp\htdocs\phpmailer\PHPMailer.php(1316): PHPMailer\PHPMailer\PHPMailer->preSend() #4 C:\xampp\htdocs\scripts\form.php(43): PHPMailer\PHPMailer\PHPMailer->send() #5 C:\xampp\htdocs\contact.php(3): include('C:\xampp\htdocs...') #6 {main} throw in C:\xampp\htdocs\phpmailer\PHPMailer.php on line 3598
有什么想法吗?
FILTER_FLAG_HOST_REQUIRED
filter flag 已在 PHP 7.3 中弃用,PHPMailer 代码库中不再引用它。因此,一个简单的修复方法应该是简单地更新 PHPMailer。如果您使用的是 composer,这将是自动的并且非常简单,但您不是,因此您必须下载最新版本并按照自述文件自行安装。
我正在尝试使用 PHPMailer 通过 gmail SMTP 服务器发送邮件,但出现致命错误,我找不到任何解决方案
我的php文件
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require "phpmailer/PHPMailer.php";
require "phpmailer/SMTP.php";
require "phpmailer/Exception.php";
$error_msg=null;
$success_msg=null;
if ($_POST) {
//$name = isset($_POST['name']) ? filter_var($_POST['name'], FILTER_SANITIZE_STRING) : null;
//$email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) : null;
//$message = htmlspecialchars($_POST['message']);
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name) || empty($email) || empty($message)) {
$error_msg = 'Fill out required entry fields!';
}
if (is_null($error_msg)) {
$mail = new PHPMailer(TRUE);
$mail->isSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "mail@gmail.com";
$mail->Password = "";
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->isHTML(true);
$mail->setFrom($email, $name);
$mail->addAddress("mail@gmail.com");
$mail->Subject = "Nowa wiadomość z formularza kontaktowego";
$mail->Body = $message;
$mail->send();
$success_msg = "Message sent.";
}
}
?>
html形式
<div class="h-100 d-flex justify-content-center align-items-center">
<div class="row">
<div class="col-md-12 rounded">
<?php echo $lang['contact_desc']?>
<?php if ($error_msg) : ?>
<div class="alert alert-danger"><?php echo $error_msg; ?></div>
<?php endif; ?>
<?php if ($success_msg) : ?>
<div class="alert alert-success"><?php echo $success_msg; ?></div>
<?php endif; ?>
<form id="myform" method="post">
<label for="name"><?php echo $lang['name']?></label>
<input type="text" name="name" id="name" placeholder="<?php echo $lang['name']?>" required>
<label for="email">Email</label>
<input type="email" name="email" id="email" placeholder="example@example.com" required>
<label for="message"><?php echo $lang['message']?></label>
<textarea name="message" id="message" placeholder="<?php echo $lang['msg_content']?>" required></textarea>
<input type="submit" name="submit" value="<?php echo $lang['submit']?>">
</form>
</div>
</div>
</div>
我得到的错误:
致命错误:未捕获错误:未定义常量 PHPMailer\PHPMailer\FILTER_FLAG_HOST_REQUIRED" in C:\xampp\htdocs\phpmailer\PHPMailer.php:3598 堆栈跟踪:#0 C:\xampp\htdocs\phpmailer\PHPMailer.php(3564): PHPMailer\PHPMailer\PHPMailer::isValidHost('localhost') #1 C:\xampp\htdocs\phpmailer\PHPMailer.php(2304): PHPMailer\PHPMailer\PHPMailer->serverHostname( ) #2 C:\xampp\htdocs\phpmailer\PHPMailer.php(1421): PHPMailer\PHPMailer\PHPMailer->createHeader() #3 C:\xampp\htdocs\phpmailer\PHPMailer.php(1316): PHPMailer\PHPMailer\PHPMailer->preSend() #4 C:\xampp\htdocs\scripts\form.php(43): PHPMailer\PHPMailer\PHPMailer->send() #5 C:\xampp\htdocs\contact.php(3): include('C:\xampp\htdocs...') #6 {main} throw in C:\xampp\htdocs\phpmailer\PHPMailer.php on line 3598
有什么想法吗?
FILTER_FLAG_HOST_REQUIRED
filter flag 已在 PHP 7.3 中弃用,PHPMailer 代码库中不再引用它。因此,一个简单的修复方法应该是简单地更新 PHPMailer。如果您使用的是 composer,这将是自动的并且非常简单,但您不是,因此您必须下载最新版本并按照自述文件自行安装。