我正在使用PHP邮件功能从HTML联系表单发送邮件。
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>
如果我只是从JavaScript提交表单,邮件就会转到主要收件箱,这很好。但是,如果我使用Ajax调用发送邮件,邮件将进入垃圾邮件文件夹。可能是什么原因?
邮件转到主要收件箱的JavaScript
document.getElementById('form1').submit();
邮件发送到垃圾邮件文件夹的JavaScript
$.post("contact_us.php", $("#form1").serialize(), function(response) {
//DO something with response
});
PHP代码
<?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";
}
?>