Ive knocked together a bit of php to prevent our website email form from sending us spam. We dont have a huge problem with receiving spam but were getting a few regular ones which this bit of code has prevented.
It uses the mail() but the users entries must first pass some validation checks (not shown here) followed by the spam mail checks shown below before it executes the mail().
We cant use a default spam filter as the email facility on our site allows our patients to request repeat prescriptions and will ligitamately contains phrases like Viagra and other commonly spam associated phrases.
This method is working absolutely fine at the moment but as i continue to add more and more phrases to block it will become rather messy with an endless list of elseif string interogations. What would be a better way to execute this, some sort of array perhaps (containing the blcoked phrases)?
<html>
<body leftmargin="0" topmargin="0">
<?php
/* BLOCKED MAIL SENDER NAMES */
if(strstr($_POST['visitor'],"add a name to catch here") ){
$isspamfree = false;
}
/* BLOCKED EMAIL ADDRESSES */
if (strstr($_POST['visitormail'],"uk-car-ads") || strstr($_POST['visitormail'],"anotheremailtoblockhere")) {
$isspamfree = false;
}
/* MESSAGE CONTENT SPAM FILTER */
elseif(strstr($_POST['notes'],"gaur eskimoic") || strstr($_POST['notes'],"Central Contracts") || strstr($_POST['notes'],"click on") || strstr($_POST['notes'],"nsubscribe") || strstr($_POST['notes'],"[url=http") || strstr($_POST['notes'],"<a href") ) {
$isspamfree = false;
}
if($isspamfree == false) {
echo '
<!-- DISPLAY THE FOLLOWING ONLY IF SPAM IS DETECTED -->
<table width="780" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="144" valign="top" background="RBtopslogan.jpg">
<table width="99%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<div align="right">
<font color="#666666" size="1" face="Arial, Helvetica, sans-serif">>>
www.winyatesHC.co.uk</font></div>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="23" background="topunder.jpg">
<table width="75%" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td><font face="Arial, Helvetica, sans-serif" color="#ffffff" size="1">
>> SPAM DETECTED!</font></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
<table width="780" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr valign="top">
<td width="174" background="leftback.jpg">
</td>
<td width="606" background="back.jpg">
<table width="98%" height="100%" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td valign="top">
<p align="justify"><strong><span lang="en-gb">
<font face="Arial, Helvetica, sans-serif" size="2" color="#993333">Prescription
request - not sent.</font></span></strong></p>
<p align="justify"><strong><span lang="en-gb">
<font face="Arial, Helvetica, sans-serif" size="2" color="#993333">
THE EMAIL YOU ARE ATTEMPTING TO SEND APPEARS TO BE SPAM:<br/><br/>
</font></span></strong></p>
<p align="center"> </p>
<p align="center">
<font face="Arial, Helvetica, sans-serif" size="2" color="#993333">
<b>Summary</b><br/><br/>
</font><br />
</p>
<p align="center"><b>If you believe this request is not a spam email</b></p>
<p align="center"><b/>please contact the surgery in person or by phone.</b></p>
<br /><br/>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal" align="center" style="text-align: left"> </p>
<p></font></td>
</tr>
<tr>
<td align="center">
<!--RiteCounter-->
<script type="text/javascript" src="http://www.ritecounter.com/c/10/9702.js"></script>
<noscript>
<a href="http://www.ritecounter.com"><img src="http://www.ritecounter.com/scripts/htmlc.php?id=9702" alt="javascript hit counter" border=0></a></noscript>
<!--END RiteCounter-->
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="29" background="down.jpg">
<table width="78%" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td>
<div align="center">
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>';
}
?>
</body>
</html>
this page is displayed informing them their email has not been sent and to contact the surgery in person. I have deliberately NOT given specifics as to an email is flagged as spam to prevent it being easily by-passed in future.