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.