我正在使用PHPMailer来构建联系表单,并且我正在寻找一个变量($ msg)以显示成功或失败消息,这取决于最终用户是否正确填写了表单。如果正确填写了表单,则($ msg)变量会成功执行回显,但是,如果用户未正确填写表单,则$(msg)变量将不会回显,而是收到一个致命错误,内容如下:
致命错误:未捕获PHPMailer \ PHPMailer \ Exception:无效地址:(Reply-To):在/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 < / p>
这是我的代码,如果最终用户未完成表单或从服务器端失败,没有人知道如何获取($ 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>
答案 0 :(得分:1)
将最后一个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';
sendemail
函数返回正确的设置成功消息,否则返回错误消息。答案 1 :(得分:0)
我认为以下代码将帮助您理解和解决问题。
$email = isset($_POST['email']) ? $_POST['email']:'';
$subject = isset($_POST['subject']) ? $_POST['subject']:'';
$body = isset($_POST['body']) ? $_POST['body']:'';
$is_valid_email = filter_var($email, FILTER_VALIDATE_EMAIL);
$status = false;
if ($email!='' && $is_valid_email==true && $subject!='' && $body!='') {
$status = sendemail('info@purplelimetree.com', $email, $subject, $body)
}
if($status==true){
$msg = 'Email has been sent, Thank you!';
} else
$msg = 'Email failed, please try again later';
}