如何使用php阻止收件箱中的垃圾邮件

时间:2013-08-01 06:42:00

标签: php security email-spam

我已经为我的一个客户端编写了一个联系我们表单及其进程页面

当用户提交表单时,会生成一封邮件并将其重定向到我的客户收件箱

在这里,我在一天结束时收到数百封未知的垃圾邮件,我不知道它是从哪里产生的,

由于这个原因,我们的网站在谷歌和其他搜索引擎的黑名单中被列入危险

任何人都可以给我一个阻止这封垃圾邮件的解决方案,也请找到我的php邮件代码的附件

以下是我的PHP代码

<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$email = $_POST["email"];

$to = "test@myaddress.com";

$cname = htmlentities(ucfirst($_POST['name']));

$subject = "Subject Comes here";

$body= "Name : ";
$body.= $cname;

$body .= "\nE-mail : ";
$body .= htmlentities($_POST['email']);

$body.= "\nMessage : ";
$body.= htmlentities($_POST['message']);

    function is_valid_email($email)
    {
        return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
    }

function contains_bad_str($str_to_test) {
  $bad_strings = array(
                "content-type:"
                ,"mime-version:"
                ,"multipart/mixed"
        ,"Content-Transfer-Encoding:"
                ,"bcc:"
        ,"cc:"
        ,"to:"
  );

  foreach($bad_strings as $bad_string) {
    if(eregi($bad_string, strtolower($str_to_test))) {
      header('Location: status.php?status=failed');
    }
  }
}

function contains_newlines($str_to_test) {
   if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
    header('Location: status.php?status=failed');
   }
} 

if (!is_valid_email($email)) {
  header('Location: status.php?status=failed');
}

contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str(body);

contains_newlines($email);
contains_newlines($subject);

$headers = "From: $email";
mail($to, $subject, $body, $headers);
header('Location: status.php?status=success');
}
?>

2 个答案:

答案 0 :(得分:1)

或使用应保持为空的隐藏字段。 Spambots有填充所有输入字段的习惯。

有关此内容的更多信息: How to prevent robots from automatically filling up a form?

答案 1 :(得分:0)

使用验证码防止垃圾邮件机器人滥用您的联系表单,并确保只有人才可以使用它。