Not sure if there is ANY perfect way to prevent against some users, but after using contact forms for years, I find that tweaked version of this works good. It's a self contained php form, which you can use as a javascript pop-up from any web page.
Read through the source code carefully and personalise it.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Contact Us</title>
<!-- METADATA -->
<script type="text/JavaScript">
<!--
//pass min and max -measured against window width
function P7_MinMaxW(a,b){
var nw="auto",w=document.documentElement.clientWidth;
if(w>=b){nw=b+"px";}if(w<=a){nw=a+"px";}return nw;
}
//-->
</script>
<!--[if lte IE 6]>
<style type="text/css">
#pagewrapper {width:expression(P7_MinMaxW(750,750));}
#container {height: 1%;}
</style>
<![endif]-->
<style type="text/css">
<!--
.NewLine {
padding: 10px;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.LabelField {
font-size: 12px;
float: left;
width: 100px;
text-align: right;
}
.txtNorm {
font-size: 12px;
}
.txtTiny {
font-size: 10px;
}
#EnquiryDetails {
font-family: Verdana, Arial, Helvetica, sans-serif;
}
.txtTitle {
font-size: 18px;
color: #999999;
}
.EntryField {
margin-left: 110px;
width: 350px;
}
#centre_content {
width: 475px;
border: thin solid #CCCCCC;
padding: 10px;
background-image: background.jpg);
}
.txtSmallNote {
font-size: 10px;
}
.radioButtons {
font-size: 9px;
}
-->
</style>
</head>
<body>
<div id="centre_content">
<?php
// where to send e-mail to
$to = 'YOUREMAIL@SOMETHING.COM'; //i.e. bloggs@whatever.com
//$to2 = 'YOUREMAIL2@SOMETHING.COM'; // uncomment if you want other recips (but see further down too)
//$to3 = 'YOUREMAIL3@SOMETHING.COM'; // uncomment if you want other recips (but see further down too)
// This will remove HTML tags, javascript sections
// and white space. It will also convert some
// common HTML entities to their text equivalent.
$search = array ('@<script[^>]*?>.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@([\r\n])[\s]+@', // Strip out white space
'@&(quot|#34);@i', // Replace HTML entities
'@&(amp|#38);@i',
'@&(lt|#60);@i',
'@&(gt|#62);@i',
'@&(nbsp|#160);@i',
'@&(iexcl|#161);@i',
'@&(cent|#162);@i',
'@&(pound|#163);@i',
'@&(copy|#169);@i',
'@(\d+);@e'); // evaluate as php
$replace = array ('',
'',
'\1',
'"',
'&',
'<',
'>',
' ',
chr(161),
chr(162),
chr(163),
chr(169),
'chr(\1)');
// declare and sanitise values
$FirstName = preg_replace($search, $replace, $_POST['FirstName']);
$LastName = preg_replace($search, $replace, $_POST['LastName']);
$PrefContact = preg_replace($search, $replace, $_POST['PrefContact']);
$PhoneNumber = preg_replace($search, $replace, $_POST['PhoneNumber']);
$EmailAddress = preg_replace($search, $replace, $_POST['EmailAddress']);
$EnquiryDetails = preg_replace($search, $replace, $_POST['EnquiryDetails']);
$mydate = date ( 'l, F d Y g:i A',time()+240 );
// e-mail subject (variable param)
$subject = "Generic Contact Form (from: $FirstName $LastName)";
// e-mail message
$message = "Generic Contact Form Submission:\r\n"
."----------------------------------------------------------------\r\n"
."Name: $FirstName $LastName\r\n"
."This person would prefer to be contacted by: $PrefContact\r\n"
."Phone: $PhoneNumber\r\n"
."Email: $EmailAddress\r\n\r\n"
."Referrer: $Referrer\r\n\r\n"
."Message: \r\n$EnquiryDetails\r\n"
."----------------------------------------------------------------\r\n"
."Submitted: $mydate\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n"
."Form Address: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}\r\n"
."----------------------------------------------------------------\r\n";
$headers = "From: $FirstName $LastName <$EmailAddress>\n"
."Reply-To: $EmailAddress\n"
."X-Mailer: PHP/".phpversion();
// check for validation, then send the e-mail
if (!empty($FirstName)){
if ($FirstName==$LastName){echo "No Spam Please!"; die;}
}
if(empty($FirstName) || empty($LastName) || empty($PhoneNumber) || empty($EmailAddress) || empty($EnquiryDetails)) {
?>
<div class='txtTitle'>Contact Us</div>
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
<div class="NewLine">
<div class="LabelField">Title:</div>
<div class="EntryField">
<select name="select">
<option value="Not Specified">select..</option>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
</select>
</div>
</div>
<div class="NewLine">
<div class="LabelField">First Name:</div>
<div class="EntryField"><input name="FirstName" type="text" id="FirstName" value="<?php echo $_POST['FirstName'];?>" size="20" />
</div>
</div>
<div class="NewLine">
<div class="LabelField">Last Name:</div>
<div class="EntryField"><input name="LastName" type="text" id="LastName" value="<?php echo $_POST['LastName'];?>" size="20" />
</div>
</div>
<div class="NewLine">
<div class="LabelField">Phone Number:</div>
<div class="EntryField"><input name="PhoneNumber" type="text" id="PhoneNumber" value="<?php echo $_POST['PhoneNumber'];?>" size="20" />
<span class="txtSmallNote">←</span><input name="PrefContact" type="radio" value="Phone" checked="checked" class="radioButtons" /> <span class="txtSmallNote">(preferred contact method)</span> </div>
</div>
<div class="NewLine">
<div class="LabelField">Email Address:</div>
<div class="EntryField"><input name="EmailAddress" type="text" id="EmailAddress" value="<?php echo $_POST['EmailAddress'];?>" size="20" />
<span class="txtSmallNote">←</span><input name="PrefContact" type="radio" value="Email" />
</div>
</div>
<div class="NewLine">
<div class="LabelField">Enquiry Details:</div>
<div class="EntryField">
<textarea name="EnquiryDetails" cols="30" rows="5" id="EnquiryDetails"><?php echo $_POST['EnquiryDetails'];?></textarea>
</div>
</div>
<div class="NewLine">
<div class="LabelField"> </div>
<div class="EntryField">
<input name="Referrer" id="Referrer" type="hidden" value="<?php echo($_COOKIE['ref']); ?>" />
<input name="submit" type="submit" class="submit" value="Submit" />
</div>
</div>
</form>
<div class="txtTiny" align="right">Your IP: <?php echo $_SERVER[REMOTE_ADDR]?></div>
<?php
} elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $EmailAddress)) {
echo "<p>ERROR: Please enter a valid e-mail address.</p>";
} else {
mail( $to, $subject, $message, $headers );
// mail( $to2, $subject, $message, $headers ); //uncomment if you want more than one recip.
// mail( $to3, $subject, $message, $headers ); //uncomment if you want more than one recip.
?>
<div class='txtTitle'>Message Sent!</div>
<p>
<div class="txtNorm">
Thank you <?php echo $FirstName. " " . $LastName; ?> for your enquiry.
<br /><br />
We will get back to you shortly.</div>
<?php
}
?>
<div class="txtTiny" align="right">©<?php echo date("Y"); ?> - Powered by <a href="http://www.YOURDOMAIN.co.uk" target="_blank">YOUR DOMAIN</a></div>
</div>
<!-- end centre content -->
</body>
</html>
P.s. There is a bit in there about cookie refs. It can track who sent your user to the form. But more on that if you need it.
Hope this helps.
Mike