我创建了一个WordPress模板,它可以通过ajax和wp_mail函数发送电子邮件。除了提供错误响应的wp_mail
函数之外,一切都还可以,因此不会发送电子邮件。我已经尝试了近一周研究可能的解决方案,但没有成功。下面是我的代码,希望你能帮助我找出问题所在。
显示的错误在此行
抛出新的异常('无法发送电子邮件。检查AJAX处理程序 fruu');
位于functions.php
文件中。
模板functions.php文件
wp_enqueue_script('jquery');
function makeBooking() {
try {
if (empty($_POST['start']) || empty($_POST['names']) || empty($_POST['email']) || empty($_POST['bphone']) || empty($_POST['adult']) || empty($_POST['child'])) {
throw new Exception('Bad form parameters. Check the markup to make sure you are naming the inputs correctly.');
}
if (!is_email($_POST['email'])) {
throw new Exception('Kindly enter a valid email.');
}
$e = explode(" - ", $_POST['start']);
$date1 = new DateTime($e['0']);
$date2 = new DateTime($e['1']);
$diff = $date2->diff($date1)->format("%a");
if(($diff < '2') ? $s='': $s='s');
if (($_POST['adult'] < '2') ? $ss = '' : $ss = "s");
if ($_POST['child'] == 'no') {
$cs = "No Children";
} else {
if ($_POST['child'] < '2') {
$cs = "1 Child";
} else {
$cs = $_POST['child'].' Children';
}
}
if ($_POST['req'] == '') {
$req = "";
} else {
$req = " \n\nSpecial Request: ".$_POST['req'];
}
$subject = 'New Booking Request from: '.$_POST['names'];
$headers = 'From: '.$_POST['names'].' <'.$_POST['email'].'>';
$send_to = "booking@mysite.com";
$message = "Booking Duration: ".$diff." day".$s." from ".date("l M dS, Y", strtotime($e['0']))." to ".date("l M dS, Y", strtotime($e['1']))." \n\nBooking Party: ". $_POST['adult'] . " Adult".$ss." and ".$cs.". \n\nContact Number: +" . $_POST['bphone']."".$req;
if (wp_mail($send_to, $subject, $message, $headers)) {
echo json_encode(array('status' => 'success', 'message' => 'Contact message sent.'));
exit;
} else {
throw new Exception('Failed to send email. Check AJAX handler fruu.'); //THIS IS THE ERROR THAT IS RETURNED BY THE SCRIPT
}
} catch (Exception $e) {
echo json_encode(array('status' => 'error', 'message' => $e->getMessage()));
exit;
}
}
add_action("wp_ajax_makeBooking", "makeBooking");
add_action('wp_ajax_nopriv_makeBooking', 'makeBooking');
表格
<form class="contact modal-form" name="contact" id="booking_form">
<input type="hidden" name="form_send" value="send" />
<input required class="col-lg-12 form-control" name="start" id="start" placeholder="Duration of Stay" type="text">
<input required class="col-lg-12 form-control" name="end" id="end" value="" placeholder="Departure Date" type="text">
<input required class="form-control col-lg-12" name="names" id="names" value="" placeholder="Full Names" type="text">
<input required class="form-control col-lg-12" name="email" id="email" value="" placeholder="Email Address" type="email">
<input required class="form-control col-lg-12" name="bphone" id="bphone" value="" placeholder="Phone Number (e.g 2547xx 123xx4)" type="tel">
<select required style="" class="col-lg-12 form-control" name="adult" id="adult">
<option selected="selected" value="">Adults</option>
<?php
echo '<option value="1">1 Adult</option>';
$i = '2';
while ($i < '11') {
$k = $i;
echo '<option value="'.$k.'">'.$k.' Adults</option>';
$i++;
}
?>
</select>
<select required style="" class="col-lg-12 form-control" name="child" id="child">
<option selected="selected" value="">Children</option>
<?php
echo '<option value="no">No Children</option>';
echo '<option value="1">1 Child</option>';
$i = '2';
while ($i < '11') {
$k = $i;
echo '<option value="'.$k.'">'.$k.' Children</option>';
$i++;
}
?>
</select>
<textarea class="form-control col-lg-12" name="req" id="req" placeholder="Special Request"></textarea>
<input type="hidden" name="action" value="makeBooking" />
<button class="btn btn-success" type="submit" id="sendBooking"><i class="fa fa-spinner fa-pulse fa-fw hide fa-2x" id="sending"></i><span id="text" class=''>Send</span></button>
</form>
jQuery代码,请注意我遗漏了带有数据验证规则的代码
$('#sendBooking').click(function(e) {
$.ajax({
url:"/wp-admin/admin-ajax.php",
type:'POST',
dataType: 'JSON',
data:$("#booking_form").serialize(),
cache: false,
success: function(data){
show_ok(data);
},
error: function(){
$("#sending").addClass("hide");
$("#text").removeClass("hide");
$("#sendBooking").removeClass("disabled");
$("#msg_not_sent").removeClass("hide");
}
});
e.preventDefault();
});
提前致谢。
答案 0 :(得分:0)
确保您在ajax调用中获得所有序列化数据。如果需要使用PHP unserialize函数:
glUniform1i()
然后改变
parse_str($_POST["data"], $_POST);
到您的实际电子邮件并再次查看。
答案 1 :(得分:0)