Return to Snippet

Revision: 21213
at December 7, 2009 04:05 by hellofromdan

Initial Code
// where you want the emails to go to
// separate multiple emails with a comma.

// this will be the first part of the subject line of mail
// sent from this script (identifies mail from this page)

// emails sent from this page may appear to come from this
// email address. change YOURDOMAIN.COM is to be the same
// as your website's domain name

// emails sent from here may come from this name.
// Change this to be the name of your website.

// If your host blocks messages with To: fields coming
// from other domains, then you may change this to true.
// If the script works fine as it is, just leave it as false.
// If this variable is false, messages will appear to come
// directly from the email address of the person who
// filled out the form, rather than appearing to come from
// the $contact_from_email above.

// The color the errors will come out as when they
// are displayed on the screen, use either a name or code
// such as #0000FF

// Use a security image only if you have a problem with automated
// bots submitting SPAM to this form. You will need the
// securityimage directory, which is located in the zip
// file on
// change to true to use security image
// false to not use security image.
// The downside to using a security image is that visually impaired
// people will not be able to use the form.


function previous_request_value($str)
if (isset($_REQUEST[$str]) )
return $_REQUEST[$str];
return '';

function cndstrips($str)
if (get_magic_quotes_gpc())
return stripslashes($str);
return $str;

$security_code=str_replace(' ','',cndstrips(trim(previous_request_value('security_code'))));


function validate_email($email) {
return preg_match('/^[A-Za-z0-9_\-\.][email protected][A-Za-z0-9_\-\.]+\.[A-Za-z0-9_\-\.]+$/', $email) == 0;


if (validate_email($visitor_email) ) {
$errors.="<br/><br/>Please enter a valid email address in the form of [email protected]";

if ($use_security_image && (strtolower($security_code) != strtolower($_SESSION['contact_form_security_code']) || $_SESSION['contact_form_security_code']=='') ) {
$errors.="<br/><br/>The verification code for the image presented was incorrect. Please enter a correct verification code.";

if ($message_body == '')
$errors.="<br/><br/>Please enter a message";

if ($message_subject == '')
$errors.="<br/><br/>Please enter a message subject";

if ($visitor_name == '')
$errors.="<br/><br/>Please enter your name";

if ( !$errors ) {
$httpagent = $_SERVER["HTTP_USER_AGENT"];
$time = date("D, F j, Y H:i O");

if ($visitor_name)
$visitor_name_and_email="$visitor_name <$visitor_email>";

if ($contact_from_name)
$contact_from_email="$contact_from_name <$contact_from_email>";

$message = "


Browser Info: $ip $httpagent

if ($send_from_internal_address) {
$message= "
From: $visitor_name_and_email
Date: $time
Subject: $message_subject

if ($send_from_internal_address) {
mail($contact_to_email, $contact_subject." $message_subject", $message, "From: $contact_from_email
Reply-To: $visitor_name_and_email");
else {
mail($contact_to_email, $contact_subject." $message_subject", $message, "From: $visitor_name_and_email");

echo "Your message:";
echo "<div style='border: 1px solid white; margin: 10px 10px 10px 10px; padding: 10px 10px 10px 10px;'>From: ".htmlentities($visitor_name_and_email)."<br />Re: ".htmlentities($message_subject)."<br />".htmlentities($message_body)."</div>";
echo "Has been sent. Thank you for contacting us.";

if (!$message_sent) {
$this_file = substr(strrchr($_SERVER['PHP_SELF'], "/"), 1);


<form name="ContactForm" id="ContactForm" method="post" action="<?php echo $this_file ?>">
<br />We are happy to hear from you. Please enter the requested information <?php if (!$message_body) echo "and message" ?> below, then click the Send button.

if ($errors) {
echo "<span style='color:$error_color'>$errors</span>";

<br /><br/><label for="visitor_name">Your name</label><br />
<input name="visitor_name" type="text" id="visitor_name" value="<?php echo htmlentities($visitor_name) ?>" size="25" />
<br /><br />

<label for="visitor_name">Your Email Address</label><br />
<input name="visitor_email" type="text" id="visitor_email" value="<?php echo htmlentities($visitor_email) ?>" size="25"/>
<br /><br />

<label for="message_subject">Subject</label><br />
<input name="message_subject" type="text" id="message_subject" value="<?php echo htmlentities($message_subject) ?>" size="25"/>
<br /><br />

if ($use_security_image) {
<div id="security"><img src="securityimage/security-image.php?width=200" width="200" height="40" alt="Verification Image" /></div>

Verification Image (please enter the text in the image above) <br />
<input name="security_code" type="text" id="security_code" size="25"/>
<br /><br />


<label for="message_body">Message</label><br />
<textarea name="message_body" cols="30" rows="6" id="message_body" ><?php echo htmlentities($message_body) ?></textarea>
<br /><br />

<input type="submit" name="Submit" value="Send" />


Initial URL

Initial Description
Version 1.5

Initial Title
PHP Contact Script

Initial Tags

Initial Language