php - 如果数据丢失,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">×</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>
解决方案
将最后一个 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';
- 使用 FILTER_SANITIZE_EMAIL 从电子邮件中删除所有非法字符。
- FILTER_VALIDATE_EMAIL 过滤器验证电子邮件地址。
- 检查是否不是空的正文和主题。
- 尝试发送电子邮件并在发生致命错误时正确捕获它并返回解释性消息。
- 如果
sendemail
函数返回正确设置成功消息或设置错误消息。
推荐阅读
- javascript - TypeError:event.preventDefault 不是函数
- sql - 我可以在 access.form 而不是每个 form.event 中 c.connect 一次吗?
- css - 在两列行css中对齐div
- javascript - 在 Vercel 上托管时 NuxtJS 生产中的 URIError
- google-chrome - 如何设置 Chrome 插件清单,以便内容脚本仅在某些子域上运行?
- javascript - 在 Cypress 中,如何从定义的数组列表中单击特定值?
- python-3.x - 我正在尝试通过串行通信读取传感器数据,但是当我尝试读取它时,它只是在 python 中运行,但它没有显示任何值
- php - 从 Firebase Admin SDK (PHP) 调用云函数
- powerbi - 计算每行的列日期和最小切片器日期之间的日期差异
- ruby-on-rails - 如何上传多个图像并同时更新一个实体。Rails API 和 JS 前端