A contact form with a captcha can be made up using two extra php files like so:
the html: add this to your contact page
<form method="post" action="sendmail.php">
<label for="name">Your Name</label><input id="name" name="name" type="text" ></input><br />
<label for="address">Address</label><textarea id="address" name="address" rows="2" cols="1" ></textarea><br />
<label for="country">Country</label><input id="country" name="country" type="text" ></input><br />
<label for="email">Your Email</label><input id="email" name="email" type="text"></input><br />
<label for="phone">Phone No.</label><input id="phone" name="phone" type="text"></input><br />
<label for="contact">Contact me by</label><select id="contact" name="contact"><option value="Email">Email</option><option value="Phone">Phone</option></select><br />
<label for="subject">Subject</label><input id="subject" name="subject" type="text"></input><br />
<label for="message">Message</label><textarea id="message" name="message" rows="5" cols="1"></textarea><br />
<div id="cap"><img src="captcha_image.php" alt="Cap" /></div>
<label for="captcha">Enter Code</label><input id="captcha" name="captcha" type="text"></input><br />
<div id="button"><input name="Send" type="submit" id="Send" value="Send"></input></div>
</form>
the mail php: create a file called 'sendmail.php' in your root and insert this code making the changes as needed.
<?
session_start();
if ($_POST["captcha"] == $_SESSION["pass"])
{
$mailuser = "enquiries@yourwebsite.com "; //put your email address here
$header = "Return-Path: ".$mailuser."\r\n";
$header .= "From: Website Enquiry <".$mailuser.">\r\n";
$header .= "Content-Type: text/html;";
$mail_body = '
Your Name: '. $_POST[name] . '<br>
Address: '. $_POST[address] . '<br>
Country: '. $_POST[country] . '<br>
Your Email: '. $_POST . '<br>
Phone: '. $_POST[phone] . '<br>
Contact: '. $_POST[contact] . '<br>
Subject: '. $_POST[subject] . '<br>
Message: '. $_POST[message] . '<br>'
;
mail ($mailuser, 'Form sent', $mail_body, $header);
//redirect to desired page
header("Location: http://yourwebsite.com/thanks.html"); //redirect to a page saying thanks for the message
} else {
header("Location: http://yourwebsite.com/wrongcode.html"); //redirect to a page saying wrong code entered
}
?>
[B]The captcha image generator: create a file called 'captcha_image.php' in your root and add this code:[/B]
<?
session_start();
header("Content-Type: image/jpeg");
die(create_image());
function create_image()
{
$md5 = md5(rand(0,9999));
$pass = substr($md5, 10, 5);
$_SESSION["pass"] = $pass;
$image = ImageCreatetruecolor(100, 21);
$clr_white = ImageColorAllocate($image, 255, 255, 255);
$clr_black = ImageColorAllocate($image, 150, 150, 150);
$clr_grey = ImageColorAllocate($image, 220, 220, 220);
imagefill($image, 0, 0, $clr_black);
imagefontheight(18);
imagefontwidth(18);
imagestring($image, 5, 30, 3, $pass, $clr_white);
imageline($image, 4, 1, 40, 20, $clr_grey);
imageline($image, 60, 1, 96, 20, $clr_grey);
return imagejpeg($image);
imagedestroy($image);
}
?>
You now have a securish lol contact form on your website, mailto: gets picked up very easy by spambots and your inbox will be filled with thousands of spam mails. Happens very quickly if your site becomes popular and people start linking to it