我正在使用 wp_mail() 接收通过我的 wordpress 博客中的联系表格提交的邮件。wp_mail() 返回 true 但问题是我没有收到任何邮件。我还尝试将邮件地址从 gmail 更改为 hotmail,但没有运气。
我的联系人模板中的 Ajax 代码
$('#send').click(function() {
//For Validation
function validateText(name) {
var pattern = /^[a-zA-Z'-.\s]+$/;
if (pattern.test(name)) {
return true;
}
return false;
}
//For Validation
function validateMail(mail) {
var pattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,3}$/;
//var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
if (pattern.test(mail)) {
return true;
}
return false;
}
//Getting values from the form
var name = $('#name').val(), mail = $('#mailid').val(), query = $('#message').val(), error = 1;
//For Validation
if(name == "" || !(validateText(name))) {
$('#name').addClass('error');
error = 0;
}
////For Validation
if(mail == "" || !(validateMail(mail))) {
$('#mailid').addClass('error');
error = 0;
}
//For Validation
if(query == "") {
$('#message').addClass('error');
error = 0;
}
if(!error) { // If validation fails
return false;
}
$('#sendAlert').show();
$('#send').html('Sending...');
$.post(ajax_object.ajaxurl, { // Using ajax post method to send data
action: 'ajax_action',
sendmail: 'nothing',
name: name,
mail: mail,
query: query
}, function(data) {
$('#send').html('Send');
alert(data); // Alerting response
return false;
});
});
在 Functions.php 中
function ajax_action_stuff() {
if(isset($_POST['sendmail'])) {
function set_html_content_type()
{
return 'text/html';
}
if(isset($_POST['name']) && isset($_POST['mail']) && isset($_POST['query'])) {
$name = $_POST['name'];
$email = $_POST['mail'];
$query = $_POST['query'];
$to = 'vigneshmoha@gmail.com';
if($name == "" || $email == "" || $query == "") {
echo "Fail";
return false;
}
$subject = "Website - Query from ".$name;
$message = "Hi,
<p><strong>Name</strong>:".$name."</p>
<p><strong>Mail</strong>:".$email."</p>
<h3><strong>Query</h3>
<p>".$query."</p>";
$headers[] = 'From: no-reply@gmail.com'."\r\n";
$headers[] = '';
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
$mailsent = wp_mail( $to, $subject, $message, $headers);
remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); // reset content-type to to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
if($mailsent) {
echo $to;
} else {
echo 'error';
}
} else {
echo 'error';
}
} else {
echo 'error';
}
die();
}
add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' );
add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' );