Wordpress AJAX sending email

2020-08-01 05:09发布

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 = "myemail@mydomain.com";
        $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条回答
ゆ 、 Hurt°
2楼-- · 2020-08-01 05:50

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'];

查看更多
登录 后发表回答