此代码用于发送邮件但不幸的是邮件转到垃圾邮件文件夹。我尝试了gmail和hotmail,但案例是一样的。 php代码是:
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$to_Email = "zinan09@gmail.com"; //Replace with recipient email address
$subject = 'My email from Somebody out there...'; //Subject line for emails
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Name is too short or empty!');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email!');
exit();
}
if(strlen($user_Message)<5) //check emtpy message
{
header('HTTP/1.1 500 Too short message! Please enter something.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_Email.'';
@$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Could not send mail! Sorry..');
exit();
}else{
echo 'Hi '.$user_Name .', Thank you for your email! ';
echo 'Your email has already arrived in my Inbox, all I need to do is Check it.';
}
}
?>
我是否需要添加或修改任何缺失的功能来解决此问题
答案 0 :(得分:5)
尝试将From
标头设置为实际的服务器域地址(即no-reply@mysite.com),并将用户电子邮件移至Reply-To
标头。这样可以平息反垃圾邮件过滤器。
$headers = 'From: no-reply@mysite.com' . "\r\n";
$headers .= 'Reply-To: '.$user_Email.'';
答案 1 :(得分:0)
正如我在评论中所说,你可能想看看你的内容。
此外,您的电子邮件发件人与电子邮件收件人相同。我会修改它,使它成为一个你无法回复的静态电子邮件。
此外,您的邮件有5个变量,不确定原因,但如果您使用标题,则可以将它们塞进去。
例如
$message = wordwrap($message, 70);
$to = "info@somewebsite.com"; // change this propperty for your own email
$subject = "Een gebruiker heeft een vraag of opmerking";
$body = 'yadidyadi'. $message ;
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: noreply@jellyfishwebdesign.nl"; // make this one static.. i made mine noreply
if (mail ($to, $subject, $body, $headers)) {
echo 'yay, mail send';
}
修改强>
只是要指出,这与你的问题无关,你的第一次检查,if($_POST)
有点不相关,因为你想检查某个按钮是否被按下。如果你在这个文件中为不同的进程放入不同的代码,那么无论如何你都会被解雇,尽管如果你正在编辑一个帖子,在数据库中输入一些东西......这个文件将会被调用。