PHPMailer 的致命错误

2022-01-17 00:00:00 smtp php phpmailer fatal-error

我正在尝试使用 PHPMailer 通过 gmail SMTP 服务器发送邮件,但出现致命错误,我找不到任何解决方案

我的 php 文件

isSMTP();$mail->Host = "smtp.gmail.com";$mail->SMTPAuth = true;$mail->用户名=mail@gmail.com";$mail->密码=";$mail->SMTPSecure = "tls";$mail->端口 = 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 = "消息已发送.";}}?>

html表单

我得到的错误:

致命错误:未捕获错误:未定义常量 PHPMailerPHPMailerFILTER_FLAG_HOST_REQUIRED"在 C:xampphtdocsphpmailerPHPMailer.php:3598 堆栈跟踪:#0 C:xampphtdocsphpmailerPHPMailer.php(3564): PHPMailerPHPMailerPHPMailer::isValidHost('localhost') #1 C:xampphtdocsphpmailerPHPMailer.php(2304): PHPMailerPHPMailerPHPMailer->serverHostname() #2 C:xampphtdocsphpmailerPHPMailer.php(1421): PHPMailerPHPMailerPHPMailer->createHeader() #3 C:xampphtdocsphpmailerPHPMailer.php(1316): PHPMailerPHPMailerPHPMailer->preSend() #4 C:xampphtdocsscriptsform.php(43): PHPMailerPHPMailerPHPMailer->send() #5 C:xampphtdocscontact.php(3): include('C:xampphtdocs...') #6 {main}在第 3598 行的 C:xampphtdocsphpmailerPHPMailer.php 中抛出

有什么想法吗?

解决方案

FILTER_FLAG_HOST_REQUIRED filter flag 在 PHP 7.3 中被弃用,并且在 PHPMailer 代码库中不再有任何对它的引用.所以一个简单的修复应该是简单地更新 PHPMailer.如果您使用 composer,这将是自动的并且非常简单,但您不是,因此您必须下载最新版本并按照自述文件自行安装.

I'm trying to use PHPMailer to send mails with gmail SMTP server but I get a fatal error and I cannot find any solution

my php file

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

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 form

<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>

The error I get:

Fatal error: Uncaught Error: Undefined constant PHPMailerPHPMailerFILTER_FLAG_HOST_REQUIRED" in C:xampphtdocsphpmailerPHPMailer.php:3598 Stack trace: #0 C:xampphtdocsphpmailerPHPMailer.php(3564): PHPMailerPHPMailerPHPMailer::isValidHost('localhost') #1 C:xampphtdocsphpmailerPHPMailer.php(2304): PHPMailerPHPMailerPHPMailer->serverHostname() #2 C:xampphtdocsphpmailerPHPMailer.php(1421): PHPMailerPHPMailerPHPMailer->createHeader() #3 C:xampphtdocsphpmailerPHPMailer.php(1316): PHPMailerPHPMailerPHPMailer->preSend() #4 C:xampphtdocsscriptsform.php(43): PHPMailerPHPMailerPHPMailer->send() #5 C:xampphtdocscontact.php(3): include('C:xampphtdocs...') #6 {main} thrown in C:xampphtdocsphpmailerPHPMailer.php on line 3598

Any ideas?

解决方案

The FILTER_FLAG_HOST_REQUIRED filter flag was deprecated in PHP 7.3 and there are no longer any references to it in the PHPMailer code base. So a straightforward fix should be to simply update PHPMailer. If you were using composer this would be automatic and very simple, but you're not, so you'll have to download the latest version and install it yourself as per the readme.

相关文章