PHP mail() not working when receiving an ajax POST request
ajax, email, jquery, php
Solution
Your first problem is that you are using `$_POST['dataObj']` on the server side while the data is only available right in `$_POST` (dataObj is a javascript variable name that won't be passed on to the server). You can get the email for example by reading `$_POST['email']`. Try to change your send.php to this:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$to_email = "myemail@gmail.com";
$subject = 'Email subject';
$headers = 'From: '.$_POST['email'];
$message = 'Name: '.$_POST['name'].' \n';
$message .= 'Email: '.$_POST['email'].' \n\n';
$message .= 'Message:\n'.$_POST['message'].' \n';
if (mail($to_email, $subject, $message, $headers)) {
http_response_code(200);
} else {
http_response_code(500);
}
} else {
http_response_code(403);
}
Note: it's not safe to use $_POST directly in the $headers-variable without some kind of escaping. An attacker can set additional parameters if you leave it like that. Google on that before using it on production.
It might be that something in the sending process is blocking mails with malformed headers more easily than your linux setup, if you receive an 200. However you should have gotten an misformed email with the php code you provided, is that correct? Answer to this post in case the error still occurs afterwards.
Problem
I have setup a form that uses jQuery ajax to send a POST request to an external php file, which then sends the email. I am getting a 200 status code but not receiving the email. Here is the jQuery: ``` var ajaxSend = function(dataObj) { $.ajax({ type: 'POST', url: 'send.php', data: dataObj, success: function() {console.log('Ajax Success!');}, error: function() {console.log('Ajax Error!');}, statusCode: { 200: function() {console.log('200 Everything ok!');}, 400: function() {console.log('400 Bad request');}, 403: function() {console.log('403 Forbidden');}, 500: function() {console.log('500 Server error');} } }); } $('.contact-form input[type="submit"]').click(function(e) { if($('form')[0].checkValidity()) { //checks if insetred value meets the HTML5 required attribute e.preventDefault(); var dataObj = { name: $.trim($('.contact-form input[type="text"]').val()), email: $.trim($('.contact-form input[type="email"]').val()), message: $.trim($('.contact-form textarea').val()) }; ajaxSend(dataObj); } else { console.log("Invalid form"); } }); ``` Here is the php file: ``` if ($_SERVER["REQUEST_METHOD"] == "POST") { $to_email = "myemail@gmail.com"; $data = json_decode($_POST['dataObj']); $subject = 'Email subject'; $headers = 'From: '.$data['email']; $message = 'Name: '.$data['name'].' \n'; $message .= 'Email: '.$data['email'].' \n\n'; $message .= 'Message:\n'.$data['message'].' \n'; if (mail($to_email, $subject, $message, $headers)) { http_response_code(200); } else { http_response_code(500); } } else { http_response_code(403); } ``` The web hosting is windows, I've tryed using a POST request without and it works just fine, i receive the email. I don't think it's a php.ini problem?