HelloWorld HelloWorld - 1 month ago 15
Ajax Question

How to send an email with $_POST data submitted with AJAX

I want to submit some data from localStorage to

Autoform.submitStoredData = function() {
var data = localStorage.tbRecoveredData;
if(data) {
jQuery.ajax ({
type: "POST",
url:"http://www.thewebsite.com/Autoform.php",
data: data,
success: function() {
console.log("success");
},
error: function(xhr,status,error) {
console.log("payload failed to submit with xhr: " + xhr + ", status: " + status + ", and error: " + error);
}
});
localStorage.removeItem("tbRecoveredData");
}
};


I am getting "success" in the console so far so good. Autoform PHP looks like this:

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = $_POST;
mail('myemail@gmail.com', 'OK SO here at least is the captured string', $data);
}

?>


This does nothing or at least no email gets sent. I admit I dont know much about PHP, I've tried googling without much luck. Do I need to wrap this in a sort of self invoking function or something because it seems like the PHP code is not being executed. Any help is appreciated thanks!

Answer

Okay, if you try putting the below code and finding it to be NULL:

var_dump(mail(...));

Then you need to configure your server to make the PHP work with it's built in mail() function. There are several ways to do it: