首页 > 解决方案 > 如果数据丢失,php 不会回显错误消息 - 而是显示 php 致命错误

问题描述

我正在使用 PHPMailer 来构建联系表单,并且我正在寻找一个变量 ($msg) 来显示成功或失败消息,具体取决于最终用户是否正确填写了表单。如果表单正确完成, ($msg) 变量会成功回显,但是如果用户没有正确填写表单,则 $(msg) 变量不会回显,而是收到一个致命错误,内容如下:

致命错误:未捕获的 PHPMailer\PHPMailer\Exception:无效地址:(回复):在 /home/k4piavsj0bsc/public_html/phpmailer/src/PHPMailer.php:1004 堆栈跟踪:#0 /home/k4piavsj0bsc/public_html/phpmailer/ src/PHPMailer.php(973): PHPMailer\PHPMailer\PHPMailer->addOrEnqueueAnAddress('Reply-To', '', '') #1 /home/k4piavsj0bsc/public_html/contactForm.php(18): PHPMailer\PHPMailer\ PHPMailer->addReplyTo('') #2 /home/k4piavsj0bsc/public_html/contactForm.php(30): sendemail('info@purplelime...', '', '', '') #3 /home/k4piavsj0bsc /public_html/index.php(9): include('/home/k4piavsj0...') #4 {main} 在 /home/k4piavsj0bsc/public_html/phpmailer/src/PHPMailer.php 第 1004 行抛出

这是我的代码,如果最终用户尚未完成表单或从服务器端失败,是否有人知道如何获取 ($msg) 变量来显示?

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$msg = "";

if (isset($_POST['submit'])) {
    require 'phpmailer/src/Exception.php';
    require 'phpmailer/src/PHPMailer.php';
    require 'phpmailer/src/SMTP.php';

    function sendemail ($to, $reply, $subject, $body) {
        $mail = new PHPMailer(true);
        $mail->setFrom('test@gmail.com', 'PLT Contact Form - Email');
        $mail->addAddress($to);
        $mail->addReplyTo($reply);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->IsHTML(false);

        return $mail->send();
    }

    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $body = $_POST['body'];

    if (sendemail('info@test.com', $email, $subject, $body)) {
        $msg = 'Email has been sent, Thank you!';
    } else
        $msg = 'Email failed, please try again later';

  }

?>


<html>

<title>Contact Form Using PHPMailer</title>


<body>

    <div id="contactInnerWrapper">
        <a href="#"><span id="close">&times;</span></a>
        <h1 id="h1contactForm">Get in touch</h1>
            <form method="post" action="contactForm.php">
                <label for="email">Email address:</label><br>
                <input  type="email" name="email"  placeholder="Enter email" id="email">
                <label for="subject">Subject:</label>
                <input  type="text" name="subject" id="subject"><br>
                <label for="body">What would you like to ask us?</label><br>
                <textarea  type="text" name="body" rows="7" id="content"></textarea>
                <button type="submit" name="submit" id="submit" value="send">Submit</button>
            </form>
            <br><br>
            <?php echo $msg; ?>
    </div>

    <script type="text/javascript" src="general.js"></script>

 </body>

</html>

标签: php

解决方案


将最后一个 if 语句更改为此

$email = filter_var($email, FILTER_SANITIZE_EMAIL); // 
if (filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($subject) && !empty($body)) {
    try {
        if (sendemail('info@purplelimetree.com', $email, $subject, $body))
            $msg = 'Email has been sent, Thank you!';
        else
            $msg = 'Error sending email';
    }catch(Excpetion $e) {
        $msg = 'Sending email failed, error: '.$e->getMessage();
    }
} else
    $msg = 'Email failed, incomplete/invalid data';
  1. 使用 FILTER_SANITIZE_EMAIL 从电子邮件中删除所有非法字符。
  2. FILTER_VALIDATE_EMAIL 过滤器验证电子邮件地址。
  3. 检查是否不是空的正文和主题。
  4. 尝试发送电子邮件并在发生致命错误时正确捕获它并返回解释性消息。
  5. 如果sendemail函数返回正确设置成功消息或设置错误消息。

推荐阅读