1

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";
}

?>
Nikhil Bharadwaj
  • 867
  • 4
  • 24
  • 42
  • The email app usually tells you why the message when to the spam folder. What is that message? –  Jan 19 '17 at 04:51
  • @jeff The message says "Why is this message in Spam? It's written in a different language to the one that you usually use". The HTML content in PHP code is in Germen and I usually use English for communication. Could that be the reason? – Nikhil Bharadwaj Jan 19 '17 at 04:54
  • You can't directly control how mail gets put in spam as there are lots of filters with different rules. Yes, the language issue for YOU appears to be the problem, but may work for someone else. Others will see the limited HTML, or the form in HTML, or the lack of words and consider it spam. Just use one of the websites that can appraise your email and go from there, – Robbie Jan 19 '17 at 06:36
  • But the reason for commenting, if someone added two new-line characters in the form, and your sendmail wasn't correctly configured, then they could use that mail form for sending mail to others. Make sure you validate (clean) anything going into mail() from an external source, most specifically remove any newlines. Better still, use a library (e.g. swiftmailer) that will help reduce vulnerabilities – Robbie Jan 19 '17 at 06:38

0 Answers0