Location: PHPKode > scripts > PHP Contact Form with Spam Protection > php-contact-form-with-spam-protection/contact-form.php
<?php

/*******************************************************************************
*  Title: PHP Contact Form with Spam Protection
*  Version: 1.1 @ April 1, 2009
*  Author: Vishal P. Rao
*  Website: http://www.work-at-home-forum.com
********************************************************************************
*  COPYRIGHT NOTICE
*  Copyright 2009 Vishal P. Rao. All Rights Reserved.
*
*  This script may be used and modified free of charge by anyone
*  AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
*  By using this code you agree to indemnify Vishal P. Rao or 
*  www.work-at-home-forum.com from any liability that might arise from 
*  it's use.
*
*  Selling the code for this program, in part or full, without prior
*  written consent is expressly forbidden.
*
*  Obtain permission before redistributing this software over the Internet
*  or in any other medium. In all cases copyright and header must remain
*  intact. This Copyright is in full effect in any country that has
*  International Trade Agreements with the India
*
*  Removing any of the copyright notices without purchasing a license
*  is illegal! 
*******************************************************************************/

/*******************************************************************************
 *	Script configuration - Refer README.txt
*******************************************************************************/

/* Email address where the messages should be delivered */
$to = 'hide@address.com';

/* This will be appended to the subject of contact form message */
$subject_prefix = 'My Website Contact';

/* Form width in px or % value */
$form_width = '70%';

/* Form background color */
$form_background = '#F7F8F7';

/* Form border color */
$form_border = '#CCCCCC';

/* Form border style. Examples - dotted, dashed, solid, double */
$form_border_style = 'solid';

/* Empty/Invalid fields will be highlighted in this color */
$field_error_color = '#FF0000';

/* Thank you message to be displayed after the form is submitted. Can include HTML tags. Write your message between <!-- Start message --> and <!-- End message --> */
$thank_you_message = <<<EOD
<!-- Start message -->
<p>We have received your message. If required, we'll get back to you as soon as possible.</p><br /><br /><br /><br /><br /><br /><br /><br />
<!-- End message -->
EOD;

/* URL to be redirected to after the form is submitted. If this is specified, then the above message will not be shown and user will be redirected to this page after the form is submitted */
/* Example: $thank_you_url = 'http://www.yourwebsite.com/thank_you.html'; */

$thank_you_url = '';

/*******************************************************************************
 *	Do not change anything below, unless of course you know very well 
 *	what you are doing :)
*******************************************************************************/

$label_name = 'Name';
$label_email = 'Email';
$label_subject = 'Subject';
$label_message = 'Message';
$label_code = 'Code';
$name_value = '';
$email_value = '';
$subject_value = '';
$message_value = '';
$label_name_empty_style = '';
$label_email_empty_style = '';
$label_email_invalid = '';
$label_subject_empty_style = '';
$label_message_empty_style = '';
$label_code_empty_style = '';
$label_code_invalid = '';

if (!isset($_POST['submit'])) {

  showForm();

} else { //form submitted

  $error = 0;
  
  if(!empty($_POST['name'])) {
  	$name_value = clean_var($_POST['name']);
  	if (function_exists('htmlspecialchars')) $name_value = htmlspecialchars($name_value, ENT_QUOTES);
  }
  else {
    $error = 1;
    $label_name_empty_style = 'color:#FF0000;';
  }
  
  if(!empty($_POST['email'])) {
  	$email_value = clean_var($_POST['email']);
  	if (!ereg("^([a-zA-Z0-9_\.-]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", $_POST['email'])) {
  	  $error = 1;
  	  $label_email_empty_style = 'color:#FF0000;';
  	  $label_email_invalid = '<strong><span style="color:#FF0000;">Invalid email</span></strong>';
	  }
  }
  else {
    $error = 1;
    $label_email_empty_style = 'color:#FF0000;';
  }
  
  if(!empty($_POST['subject'])) {
  	$subject_value = clean_var($_POST['subject']);
  	if (function_exists('htmlspecialchars')) $subject_value = htmlspecialchars($subject_value, ENT_QUOTES);  	
  }
  else {
  	$error = 1;
    $label_subject_empty_style = 'color:#FF0000;';
  }  

  if(!empty($_POST['message'])) {
  	$message_value = clean_var($_POST['message']);
  	if (function_exists('htmlspecialchars')) $message_value = htmlspecialchars($message_value, ENT_QUOTES);
  }
  else {
    $error = 1;
    $label_message_empty_style = 'color:#FF0000;';
  }    

  if(empty($_POST['captcha_code'])) {
    $error = 1;
    $label_code_empty_style = 'color:#FF0000;';
  } else {
  	include_once "securimage.php";
		$securimage = new Securimage();
    $valid = $securimage->check($_POST['captcha_code']);

    if(!$valid) {
      $error = 1;
      $label_code_empty_style = 'color:#FF0000;';   
      $label_code_invalid = '<strong><span style="color:#FF0000;">Incorrect code</span></strong>';
    }
  }

  if ($error == 1) {
    $error_message = '<span style="font-weight:bold;font-size:90%;">Please correct/enter field(s) in red.</span>';

    showForm();

  } else {
  	
  	if (function_exists('htmlspecialchars_decode')) $name_value = htmlspecialchars_decode($name_value, ENT_QUOTES);
  	if (function_exists('htmlspecialchars_decode')) $subject_value = htmlspecialchars_decode($subject_value, ENT_QUOTES);
  	if (function_exists('htmlspecialchars_decode')) $message_value = htmlspecialchars_decode($message_value, ENT_QUOTES);  	
  	
    $message = "$name_value\n$email_value\n\nMessage:\n$message_value";
    
    
    mail($to,"$subject_prefix - $subject_value",$message,"From: $name_value <$email_value>");
    
    if (!$thank_you_url) {
    
      include 'form-header.php';
      echo $GLOBALS['thank_you_message'];
      echo "\n";
      include 'form-footer.php';
	  }
	  else {
	  	header("Location: $thank_you_url");
	  }
       	
  }

} //else submitted



function showForm()

{
include 'form-header.php';
echo $GLOBALS['error_message'];  
  
echo <<<EOD
<div style="width:{$GLOBALS['form_width']};vertical-align:top;text-align:left;background-color:{$GLOBALS['form_background']};border: 1px {$GLOBALS['form_border']} {$GLOBALS['form_border_style']};overflow:visible;" id="formContainer">
<form method="post" class="contactForm">
<fieldset style="border:none;">
<p><label for="name" style="font-weight:bold;{$GLOBALS['label_name_empty_style']};width:25%;float:left;display:block;">{$GLOBALS['label_name']}</label> <input type="text" name="name" value="{$GLOBALS['name_value']}" /></p>
<p><label for="email" style="font-weight:bold;{$GLOBALS['label_email_empty_style']}width:25%;float:left;display:block;">{$GLOBALS['label_email']}</label> <input type="text" name="email" value="{$GLOBALS['email_value']}" /> {$GLOBALS['label_email_invalid']}</p>
<p><label for="subject" style="font-weight:bold;{$GLOBALS['label_subject_empty_style']}width:25%;float:left;display:block;">{$GLOBALS['label_subject']}</label> <input type="text" name="subject" value="{$GLOBALS['subject_value']}" /></p>
<p><label for="message" style="font-weight:bold;{$GLOBALS['label_message_empty_style']}width:25%;float:left;display:block;">{$GLOBALS['label_message']}</label> <textarea name="message" cols="40" rows="6">{$GLOBALS['message_value']}</textarea></p>
<p><label for="" style="font-weight:bold;width:25%;float:left;display:block;">&nbsp;</label> <img id="captcha" src="securimage_show.php" alt="CAPTCHA Image" /></p>
<p><label for="code" style="font-weight:bold;{$GLOBALS['label_code_empty_style']}width:25%;float:left;display:block;">{$GLOBALS['label_code']}</label> <input type="text" name="captcha_code" size="10" maxlength="5" /> {$GLOBALS['label_code_invalid']}</p>
<div style="margin-left:25%;display:block;">(Please enter the text in the image above. Text is not case sensitive.)<br />
<a href="#" onclick="document.getElementById('captcha').src = 'securimage_show.php?' + Math.random(); return false">Click here if you cannot recognize the code.</a>
</div>
<p><span style="font-weight:bold;font-size:90%;">All fields are required.</span></p>
<input type="submit" name="submit" value="Submit" style="border:1px solid #999;background:#E4E4E4;margin-top:5px;" />
</fieldset>
</form>
</div>
<div style="width:{$GLOBALS['form_width']};text-align:right;font-size:80%;"><a href="http://www.work-at-home-forum.com/8_12714_0.html" title="PHP Contact Form">PHP Contact Form</a></div> 
</div>
EOD;

include 'form-footer.php';
}

function clean_var($variable) {
    $variable = strip_tags(stripslashes(trim(rtrim($variable))));
  return $variable;
}

?>
Return current item: PHP Contact Form with Spam Protection