我有一个联系表单,提交后会通过PHPMailer发送信息,并应该返回警报成功或警报危险通知并重置表单。
问题:如果我注释了contact.php上的$mail->send();
行,则会显示警报成功,并且表单将重置-如预期的那样。但是,如果我不理会$mail->send();
,则会收到电子邮件,但没有任何警报显示,并且该表单也不会重置。
我在这里做什么错了?
contact-form.html
<form id="contact-form" method="post" action="contact.php" name="contact-form" role="form">
<div class="form-group">
<i class="fa fa-user grey-text"></i>
<label for="contact-name">Full Name</label>
<input type="text" class="form-control" id="contact-name" name="contact-name"/>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<i class="fa fa-envelope grey-text"></i>
<label for="contact-email">Email</label>
<input type="email" class="form-control" id="contact-email" name="contact-email" required="required"aria-describedby="emailHelp" data-error="Please enter your email address"/>
<div class="help-block with-errors"></div>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="messages"></div>
<div class="text-right">
<button type="submit" class="btn btn-custom" name="submit">Submit</button>
</div>
</form>
contact.php
<?php
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
//Server settings
$mail->isSMTP(); // set SMTP
$mail->Host = 'mailserver.com'; // Server
$mail->SMTPAuth = true; // Enable SMTP auth
$mail->Username = 'info@domain.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls'; // Enable TLSd
$mail->Port = 587; // TCP port
//Recipients
$mail->setFrom('info@domain.com', 'Web Contact'); // FROM
$mail->addAddress('admin@domain.com', 'Admin - Domain'); // TO
// message that will be displayed when everything is OK :)
$okMessage = 'Thank you for your message. We will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error. Please try again later!';
//Content
$mail->isHTML(true); // Set to HTML
$mail->Subject = 'Contact Form Message';
$mail->Body = "Full Name: ".$_POST['contact-name']."<br />Email Address: ".$_POST['contact-email']."<br /><br />";
$mail->send();
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
?>
contact.js
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
// if the validator good
if (!e.isDefaultPrevented()) {
var url = "contact.php";
// POST values in the background the the script URL
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
// data = JSON object that contact.php returns
// apply success/danger
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
// Bootstrap alert box HTML
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable" role="alert"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#contact-form').find('.messages').html(alertBox);
// empty the form
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
答案 0 :(得分:2)
我最初的猜测是,不查看您的日志,而是$ mailer-> send()抛出了致命错误,而没有被您的try / catch捕获。您正在使用哪个版本的PHP?另外,您的contact.js不在监视错误,我建议让ajax也监视错误。
如果使用的是> PHP 7.0,请尝试此操作
contact.php
<?php
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
try {
//Server settings
$mail->isSMTP(); // set SMTP
$mail->Host = 'mailserver.com'; // Server
$mail->SMTPAuth = true; // Enable SMTP auth
$mail->Username = 'info@domain.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls'; // Enable TLSd
$mail->Port = 587; // TCP port
//Recipients
$mail->setFrom('info@domain.com', 'Web Contact'); // FROM
$mail->addAddress('admin@domain.com', 'Admin - Domain'); // TO
// message that will be displayed when everything is OK :)
$okMessage = 'Thank you for your message. We will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error. Please try again later!';
//Content
$mail->isHTML(true); // Set to HTML
$mail->Subject = 'Contact Form Message';
$mail->Body = "Full Name: ".htmlspecialchars($_POST['contact-name'])."<br />Email Address: ".htmlspecialchars($_POST['contact-email'])."<br /><br />";
$mail->send();
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
} catch (Error $e) {
// should log the fatal
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
contact.js
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
// if the validator good
if (!e.isDefaultPrevented()) {
var url = "contact.php";
// POST values in the background the the script URL
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
// data = JSON object that contact.php returns
// apply success/danger
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
// Bootstrap alert box HTML
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable" role="alert"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#contact-form').find('.messages').html(alertBox);
// empty the form
$('#contact-form')[0].reset();
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('The ajax request failed:' + errorThrown);
}
});
return false;
}
})
});