1
votes

I'm trying to corporate sending email before submitted the form in wordpress page. I'm able to do the AJAX part and send email successfully. However, the email is not 100% delivered. I wonder why?

This is the code


    var data = {};
    data.donorEmail = $("#email").val();
    data.action = "mail_action";
    $("#donorSubmit").click(function(e){
        $.post('http://www.myurl.com/wp-admin/admin-ajax.php',data, onSuccess);
    });

    function onSuccess(results)
    {
        if( results == "00")
            document.forms['donorForm'].submit() // After the email is sent then submit the form to another website. 
    }

And this is the code in functions.php

add_action('wp_ajax_mail_action', 'sending_mail');
add_action('wp_ajax_nopriv_mail_action', 'sending_mail');

function sending_mail(){
    if(isset($_POST['email']))
    {
        $to = "[email protected]";
        $subject = "Donation";
        $message = $_POST['email']; 

        if(mail($to, $subject, $message))
        {
            echo "0";
        }
    }
}

With this code I receive the email all the time. However, the content of the email which should be the email of user doesn't come with the email all the time. Sometime it's just a blank content.

1
Out of interest Toy, what page does your form post to if Javascript is disabled?addedlovely

1 Answers

2
votes

As you see you make:

vardata = {'donorEmail':$('#email').val(),'action':'mail_action' };

but in your php file you request the $_POST['email'] instead of $_POST['donorEmail'];

try calling $_POST['donorEmail'];