Spam Filter in Contact Form



PHP Snippet 1:

<?php
        // Create Variables
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];

        // Function to deal with errors
        function died($error) {
            echo 'We are very sorry, but there were error(s) found with the form you submitted.';
            echo 'These errors appear below.<br><br>';
            echo $error.'<br>';
            echo 'Please press <b>back</b> and fix these errors.';
            die();
        }

        // Validate email address
        $error_message = "";
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $error_message .= 'The email address you entered does not appear to be valid.<br>';
        }
        if(strlen($error_message) > 0) {
            died($error_message);
        }

        // Prevent spammers from using contact form
            //Create an array containing the words in the message
            $MessageArray = explode(" ", $message);
            //Get SPAM words from file and store them in an array
            $SpamWords = file_get_contents('spamwords.txt');
            $SpamArray = explode("\r\n", $SpamWords);
            //Cycle through all the words in the message
            foreach($MessageArray as $word){
                //Check the word for SPAM words, if it is don't send the email
                if(in_array($word, $SpamArray)){
                    echo '<h1>Spam Guard</h1>';
                    echo '<p>Here in European Community, the <a href="http://www.legislation.gov.uk/uksi/2003/2426/pdfs/uksi_20032426_en.pdf">Privacy and Electronic Communications Regulations 2003</a> cover the sending of email marketing. This legislation says that organisations must only send marketing emails to anyone if they have agreed to receive them, except where there is a clearly defined customer relationship.</p>';
                    echo '<p>It appears that you are attempting to send an unsolicited message (e.g. a marketing message).</p>';
                    echo '<p>We as an organisation do not send unsolicited messages and we request that you do the same for us.</p>';
                    echo '<p>If you are not attempting to send an unsolicited message, there may be an error in the system so please accept our apologies.</p>';
                    die();
                }
            }
            //If we've made it to this point, our message doesn't contain any obvious SPAM words

        // Formulate Email
        $formcontent='Message: \n $message \n \n From: $name $email';
        $recipient = << my email address >>;
        $subject = 'Contact Form Message';
        $mailheader = 'From: $name <$email> \r\n';
        mail($recipient, $subject, $formcontent, $mailheader) or die('Error!');
        echo 'Thank you for contacting us.  We will be in touch with you very soon via your email address<br>' . $email;
        ?>

PHP Snippet 2:

$SpamArray = explode("\r\n", $SpamWords);

PHP Snippet 3:

$SpamArray = array_map("trim", file('/spamwords.txt'));