I'm using PHP mail function to send a mail from HTML contact form.
HTML
<!DOCTYPE html>
<html>
<body>
<form id="newform" action="contact.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
If I just submit the form from JavaScript the mail goes to primary inbox and it is fine. But if I use the Ajax call to send the mail, the mail goes into spam folder. What could be the reason?
JavaScript for which mail goes to primary inbox
document.getElementById('form1').submit();
JavaScript for which mail goes to spam folder
$.post("contact_us.php", $("#form1").serialize(), function(response) {
//DO something with response
});
PHP code
<?php
$subject = 'Hello People';
$from = "From: Hello <no-reply@hello.com>\r\n";
$to = 'hello@gmail.com';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$firstname = $_POST["firstname"];
$secName = $_POST["lastname"];
$message = '<form>
<table width="450px">
</tr>
<tr>
<td valign="top">
<label style="text-decoration:underline">First Name : </label>
</td>
<td valign="top">
<label>'. $firstname.'</label>
</td>
</tr>
<tr>
<td valign="top">
<label style="text-decoration:underline">Last Name : </label>
</td>
<td valign="top">
<label>'.$secName.'</label>
</td>
</tr>
</table>
</form>';
$result = mail($to, $subject, $message, $headers);
if(!$result) {
echo "Error";
} else {
echo "Success";
}
?>