Protecting your Contact form from SPAM

If you have ever added a contact form to your website you are no doubt, more than aware of the problems spammers can create. Thankfully there are two very easy to implement solutions that will reduce (and hopefully) remove all your contact form spam CAPTCHA and Akismet.

CAPTCHA

CAPTCHA stands for Completely Automated Turing Test To Tell Computers and Humans Apart – which is fancy geek talk to say CAPTCHA is a way to test that the person filling out your form is actually a person and not a spammer or robot. The usual way this is done is by showing the user a picture of a word or two (usually obscured) and asking the user to correctly decode it before be able to submit the form.

Using CAPTCHA to protect your contact form

One of the simplest ways to implement CAPTCHA on your contact form is to use http://recaptcha.net (as service that provides the added bonus of helping to digitize books at the same time). Implementation is very easy and usually only involves signing up and copying the sample code.

Problems with CAPTCHA

The biggest issue with CAPTCHA as a method of protecting your contact form is that spammers are slowly working out how to read the CAPTCHA images and submit spam despite your protection.

Users also have trouble using CAPTCHA and testing has shown as many as 26% of people with abort a form send because it has a CAPTCHA requirement. I don’t know about you but I can’t afford to lose that much potential business.

Using a Spam Filter to protect your contact form

Anyone that has used WordPress for any length of time is more that aware of how capable Akismet is at detecting and blocking spam from their blog comment forms. What most people don’t realise is that you can harness the same power that wordpress does to protect your own contact form from spam.

Using Akismet to protect your contact form from SPAM

  1. The first thing you need to use Akismet to protect your form is a WordPress.com account. Once you have that login and go to your profile page and record your API Key.
  2. Download the latest PHP5 libraries from http://akismet.com/development/
  3. Replace your mail send code with something similar to the following:
<?php
require_once("includes/Akismet.class.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$url = ""; //my contact form doesn't require a URL modify if your's does

if (isset($email) &&  !empty($email)) {

$apikey = 'AKISMET_API_KEY';
$blogurl = 'http://domain.com/';
$akismet = new Akismet($blogurl ,$apikey);
$akismet->setCommentAuthor($name);
$akismet->setCommentAuthorEmail($email);
$akismet->setCommentAuthorURL($url);
$akismet->setCommentContent($message);
$akismet->setPermalink('http://andrewbleakley.com/');
if($akismet->isCommentSpam()) {
$myFile = "spam.txt";
$fh = fopen($myFile, 'a') or die("can't open spam file");
$stringData = sprintf("Name: %s\r\nEmail: %s \r\nMessage: %s\r\n------------------------------------\r\n",$name,$email,$want,$message);
fwrite($fh, $stringData);
fclose($fh);
} else {
$header = "From: " . $name . " <" . $email . ">\r\n";
$to = "mail@domain.com";
$subject = "domain.com website contact";
$body = "I want: " . $want . "\r\n" . $message;
mail($to,$subject, $body, $header);
}

}
?>

The code snippet above will send your contact form details to Akismet to be tested for SPAM. If it succeeds it will continue mailing you the contact, if it fails the Akismet Is SPAM test it will store the contact in a file named spam.txt in the same folder as your contact form.

The original code snippet just ignored SPAM messages but I am not that quick to disregard possible work so I save them and make a habit of scanning the spam.txt file from time to time.

I hope you get something out of this code, if you want help implementing it at your end, feel free to hire me for an hour or so and I will tailor it suit your circumstance.

VN:F [1.9.13_1145]
Rating: 4.0/5 (1 vote cast)
Protecting your Contact form from SPAM, 4.0 out of 5 based on 1 rating

No related posts.

2 Responses to “Protecting your Contact form from SPAM”

  1. Facebook Fan Page says:

    Thanks for this walkthrough. It helped me figure out a client of mines PHP contact form on their website. It was hand coded by developers before me and I was able to plug this in, make a few changes, and make it work.

    For some reason I only needed one closing }, as two would cause a syntax error. I mean minutes after I pushed save on the code, I already got a SPAM hit in the spam.txt.

    Really, really, nice and it helped me learn. I look like the hero, thanks!

    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply