admin管理员组文章数量:1023827
I'm trying to send emails through WordPress using the wp_mail()
function and it doesn't seem to work.
It's displaying 0
in Chrome 'Network' response
with
Status Code:200 OK
This is my code part:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "[email protected]";
$host = "ssl://smtp.gmail:465";
$username = '[email protected]';
$password = 'passpass';
$email_subject = "You have a new email from $email via company website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $email_subject, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
What part might be wrong?
Edit:
This is the ajax part:
// Send button for the "contact form".
$('#sendBtn').click(function(){
//get info
var fullname = $("#fullname").val();
var email = $("#email").val();
var text = $("#text").val();
//send info to php
$.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
$('#aboutUnsuccess').show("slow");
$('.form_content').hide("slow");
}
},
url: document.location.protocol+'//'+document.location.host+'/wp-admin/admin-ajax.php',
type: "POST",
/*action: 'submit_contact_form',*/
data: ({ "action": "submit_contact_form", "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
$('.form_content').hide('slow', function() {
$('.form_content').hide( "slow" );
});
//show textboxes
$('#aboutSuccess').show("slow");
$( "#aboutSuccess" ).append( "<iframe id=\"pixel-thing\" src=\".html\" width=\"1\" height=\"1\" border=\"0\"></iframe>" );
}
}
});
});
I'm trying to send emails through WordPress using the wp_mail()
function and it doesn't seem to work.
It's displaying 0
in Chrome 'Network' response
with
Status Code:200 OK
This is my code part:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "[email protected]";
$host = "ssl://smtp.gmail:465";
$username = '[email protected]';
$password = 'passpass';
$email_subject = "You have a new email from $email via company website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $email_subject, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
What part might be wrong?
Edit:
This is the ajax part:
// Send button for the "contact form".
$('#sendBtn').click(function(){
//get info
var fullname = $("#fullname").val();
var email = $("#email").val();
var text = $("#text").val();
//send info to php
$.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
$('#aboutUnsuccess').show("slow");
$('.form_content').hide("slow");
}
},
url: document.location.protocol+'//'+document.location.host+'/wp-admin/admin-ajax.php',
type: "POST",
/*action: 'submit_contact_form',*/
data: ({ "action": "submit_contact_form", "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
$('.form_content').hide('slow', function() {
$('.form_content').hide( "slow" );
});
//show textboxes
$('#aboutSuccess').show("slow");
$( "#aboutSuccess" ).append( "<iframe id=\"pixel-thing\" src=\"http://54.xx.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html\" width=\"1\" height=\"1\" border=\"0\"></iframe>" );
}
}
});
});
Share
Improve this question
edited Apr 14, 2019 at 12:11
butlerblog
5,1313 gold badges28 silver badges44 bronze badges
asked Feb 27, 2015 at 10:54
Kar19Kar19
2551 gold badge9 silver badges19 bronze badges
1 Answer
Reset to default 3As this is an AJAX function your function must exit;
or die();
after the final line of executed code, in this case outside of the if statement before the final }
However I don't think this is the true issue, if you are getting a 0
returned in my experience it means the function is not running because admin-ajax.php returns 0
if it hits the end of the file and does not pick up your function.
Can you post the AJAX request?
Also for debugging (because your function doesn't actually return anything at the moment), before the exit;
or die();
you may want to add a response to check that your function is actually being executed, I usually use something like:
header("Content-Type: application/json", true);
echo json_encode( array("AJAX" => "Success") );
I'm trying to send emails through WordPress using the wp_mail()
function and it doesn't seem to work.
It's displaying 0
in Chrome 'Network' response
with
Status Code:200 OK
This is my code part:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "[email protected]";
$host = "ssl://smtp.gmail:465";
$username = '[email protected]';
$password = 'passpass';
$email_subject = "You have a new email from $email via company website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $email_subject, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
What part might be wrong?
Edit:
This is the ajax part:
// Send button for the "contact form".
$('#sendBtn').click(function(){
//get info
var fullname = $("#fullname").val();
var email = $("#email").val();
var text = $("#text").val();
//send info to php
$.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
$('#aboutUnsuccess').show("slow");
$('.form_content').hide("slow");
}
},
url: document.location.protocol+'//'+document.location.host+'/wp-admin/admin-ajax.php',
type: "POST",
/*action: 'submit_contact_form',*/
data: ({ "action": "submit_contact_form", "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
$('.form_content').hide('slow', function() {
$('.form_content').hide( "slow" );
});
//show textboxes
$('#aboutSuccess').show("slow");
$( "#aboutSuccess" ).append( "<iframe id=\"pixel-thing\" src=\".html\" width=\"1\" height=\"1\" border=\"0\"></iframe>" );
}
}
});
});
I'm trying to send emails through WordPress using the wp_mail()
function and it doesn't seem to work.
It's displaying 0
in Chrome 'Network' response
with
Status Code:200 OK
This is my code part:
// Contact form Ajax
add_action('wp_ajax_nopriv_submit_contact_form', 'submit_contact_form');
function submit_contact_form(){
if(isset($_POST['email'])) {
$email = $_POST['email'];
$email_to = "[email protected]";
$host = "ssl://smtp.gmail:465";
$username = '[email protected]';
$password = 'passpass';
$email_subject = "You have a new email from $email via company website";
$message = $_POST['text'];
$headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
/*$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));*/
//$mail = $smtp->send($email_to, $headers, $message);
wp_mail( $email_to, $email_subject, $message );
/*if (PEAR::isError($mail)) {
echo($mail->getMessage());
} else {
echo("Message successfully sent!\n");
}*/
}
}
error_reporting(E_ALL);
ini_set("display_errors", 1);
What part might be wrong?
Edit:
This is the ajax part:
// Send button for the "contact form".
$('#sendBtn').click(function(){
//get info
var fullname = $("#fullname").val();
var email = $("#email").val();
var text = $("#text").val();
//send info to php
$.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
$('#aboutUnsuccess').show("slow");
$('.form_content').hide("slow");
}
},
url: document.location.protocol+'//'+document.location.host+'/wp-admin/admin-ajax.php',
type: "POST",
/*action: 'submit_contact_form',*/
data: ({ "action": "submit_contact_form", "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
$('.form_content').hide('slow', function() {
$('.form_content').hide( "slow" );
});
//show textboxes
$('#aboutSuccess').show("slow");
$( "#aboutSuccess" ).append( "<iframe id=\"pixel-thing\" src=\"http://54.xx.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html\" width=\"1\" height=\"1\" border=\"0\"></iframe>" );
}
}
});
});
Share
Improve this question
edited Apr 14, 2019 at 12:11
butlerblog
5,1313 gold badges28 silver badges44 bronze badges
asked Feb 27, 2015 at 10:54
Kar19Kar19
2551 gold badge9 silver badges19 bronze badges
1 Answer
Reset to default 3As this is an AJAX function your function must exit;
or die();
after the final line of executed code, in this case outside of the if statement before the final }
However I don't think this is the true issue, if you are getting a 0
returned in my experience it means the function is not running because admin-ajax.php returns 0
if it hits the end of the file and does not pick up your function.
Can you post the AJAX request?
Also for debugging (because your function doesn't actually return anything at the moment), before the exit;
or die();
you may want to add a response to check that your function is actually being executed, I usually use something like:
header("Content-Type: application/json", true);
echo json_encode( array("AJAX" => "Success") );
版权声明:本文标题:plugins - Why wp_mail() function isn't sending any emails and displaying '0' in Chrome 'Network& 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745590676a2157866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论