0

I have send email funciton on my home.php wordpress file and function is below

$("#sendmailbtn").click(function(e) {
    var dataString = $(".pricing").html();
    e.preventDefault();
    var grandTotal = localStorage.getItem("GrandTotal");
    $.ajax({
    type: "POST",
    url: "/wp-admin/admin-ajax.php",
    datatype: "html",
    data: {'action': 'fahadsending_mail', 'more': dataString, 'grandTotal': grandTotal},
    success: function(response) {
        alert("success is " + response);
    },error:function(response){
        alert("error is " + response);
    }
    });
});

AND THE EMAIL CODE IN MY functions.php file is below where i am sending post to fahadsending_mail function but it ajax success function it alerts success is 0

    add_action('wp_ajax_fahadsending_mail', 'fahadsending_mail');
add_action('wp_ajax_fahadsending_mail', 'fahadsending_mail');

function fahadsending_mail(){

        $to = "asadkhan6164995@gmail.com";
        $subject = "Donation";
        $message = "message message message message message message message "; 

        if(wp_mail($to, $subject, $message))
        {
            echo "mail sent";
    } else {
        echo "mail not sent";
    }

}

and response comes in success function and it alert success is 0 and ajax post shows following action wplc_call_to_server_visitor cid 213 security 2f40c8e66f status 5 wplc_email no email set wplc_name Guest wplcsession 1456325128583

1

You need to allow AJAX requests for guests too. Usually when a WordPress AJAX call returns 0, it means that there are no capabilities for the user to perform that action.

Also, your debug info showed Guest.

Alter your function to include the nopriv AJAX for guests:

add_action('wp_ajax_nopriv_fahadsending_mail', 'fahadsending_mail');
add_action('wp_ajax_fahadsending_mail', 'fahadsending_mail');

function fahadsending_mail(){

    $to = "asadkhan6164995@gmail.com";
    $subject = "Donation";
    $message = "message message message message message message message "; 

    if( wp_mail($to, $subject, $message) ){
        echo "mail sent";
    } else {
        echo "mail not sent";
    }

    die(); // never forget to die() your AJAX reuqests

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.