<?php
error_reporting(0); /* Turn off all reporting */
session_start();
/*
IN THIS FILE:
In the "define" fields below, replace my values in the second field with yours.
Upload this file to the appropriate directory.

IN YOUR FORM FILE:
The action field of the form tag should be the appropriate directory and "processemailform.php"
Example:
<form id="form1" name="form1" method="post" action="/includes/processemailform.php">

NOTE: submit button should be named "submit" or "Submit"; value can be anything
NOTE: Email form tag should be named "email" or "Email"; value will be entered by submitter
*/

/*PSEUDO CAPTCHA: enter the answer to your "pseudo-captcha" question; if you are NOT using the
pseudo-captcha option, leave the defined field empty.
NOTE: the form field name must be "pseudo-captcha".
Example:
Is water wet or dry? <input name="pseudo-captcha" type="text" id="web" size="70" />
*/
define("PSEUDO_CAPTCHA_RESPONSE", "wet");

/*SPAM PREVENTION: enter 1 if you want to prevent users from entering 'http://' in any form field
NOTE: The person submitting cannot put "http://" in any form field, even for a website's URL;
prefix that form field with the http:// for them, and put a note on your form page that if
they put http:// in any form field, their form will be rejected as spam; I usually put this
next to the "required fields" notice, or immediately above the form field

Example:
Your website: (do not enter "http://" or your form will be rejected as spam)
<br />http://<input name="website" type="text" id="web" size="70" />
*/
define("PREVENT_HTTP", "1");

/*REQUIRED FIELDS: enter required fields (no spaces in field names!), delimited by a comma,
or leave blank for none; if you are requiring a field that you want as two or
more words in the email, use the underscore "_" to separate the words on your form.
Example: "Your Name" as text is "Name_of_person_submitting_form" as the form field.
It will show up in the email as "Name of person submitting form:".
NOTE: do not use cache-control header tags on the contact form file, or the person submitting
might not be able to get back to their form with their existing values intact. This
is not the most graceful way to check for required fields, but this approach will work
with an html contact file and does not require javascript (which can be bypassed VERY
easily in Opera browsers).
UPDATE: Another thing to note is that this script works best with .htm or .html contact files.
For some reason that I haven't figured out yet, PHP will remove the entered values,
even though I am not including cache-control header tags.
*/
define("REQUIRED_FIELDS_COMMA_DELIMITED", "Name_of_person_submitting_form,Email,Country");

//enter 1 if you prefer html email; enter 0 if you prefer text email
define("HTML_EMAIL", "1");

//enter 1 if you are using a reset button on your form; enter 0 if you are not
define("USING_RESET_BUTTON", "0");

//put the "from" email address here; many servers require that this domain be the website domain
define("FROM", "info@website.com");

//put the receipient(s) email address here; separate multiple emails with a comma
define("SEND_TO_EMAIL", "info@website.com");

//enter 1 if person submitting should receive copy; enter 0 if not
define("CC_EMAIL", "1");

//put the website name here
define("WEBSITE", "Website");

//change subject line to your preference
define("SUBJECT_LINE", "Sample Contact Form Submission");

//enter the FULL name of the file that you want to return visitors to after they submit
define("REDIRECT_PAGE_NAME", "http://www.website.com/thankyou.htm");

/* IF YOU ARE HAVING PROBLEMS EMAILING,
uncomment the ini_set lines and specify your SMTP Server, Port and Valid From Address
*/
//ini_set("smtp","mail.website.com");
//ini_set("smtp_port","25");
//ini_set("sendmail_from","from@website.com");

/*
Problems? Questions? Suggestions?
Contact me at jenny - at - whitewavedesigns.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DO NOT CHANGE ANYTHING BELOW THIS LINE */

//check pseudo-captcha
if (PSEUDO_CAPTCHA_RESPONSE && PSEUDO_CAPTCHA_RESPONSE <> $_POST['pseudo-captcha']) { echo 'Your pseudo-captcha response '.$_POST['pseudo-captcha'].' is invalid! Please use your browser\'s back button, correct and submit again.<br />'; exit; }

//check required fields:
$requiredAR = explode(",", REQUIRED_FIELDS_COMMA_DELIMITED);
if ($requiredAR[0]) //make sure there is really a required field
{
foreach ($requiredAR as $required)
{
if (!$_POST[trim($required)]) $requiredMsg = $requiredMsg.$required.' is a required field! Please use your browser\'s back button, complete the form, and submit again.<br />';
if (strtolower($required) == 'email') //validate email
{
$email = $_POST['Email'];
if (!$email) $email = $_POST['email'];
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $requiredMsg = $requiredMsg.$required.' is invalid! Please use your browser\'s back button, correct and submit again.<br />';
}
}
}
if ($requiredMsg) { echo $requiredMsg; exit; }

if ($_POST['Email']) define("CC_EMAIL_ADDRESS", $_POST['Email']);
else define("CC_EMAIL_ADDRESS", $_POST['email']);

if (noHeaders(CC_EMAIL_ADDRESS))
{ emailPostForm($_POST); header("Location:".REDIRECT_PAGE_NAME); }
else //spam attempt; redirect somewhere else
{ header("Location:http://www.google.com/"); }

function emailPostForm($postformAR)
{
$sendto = SEND_TO_EMAIL;
$website = WEBSITE;
$subject = SUBJECT_LINE;
$headers = '';
if (HTML_EMAIL)
{
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
}
$headers.= 'From: '.$website.' Website <'.FROM.'>';
if (CC_EMAIL) $headers.= "\r\nCC:".CC_EMAIL_ADDRESS."";

$space = '';
$trtd_open = '<tr><td width="100%" valign="top" align="left" class="tdfont">';
$trtd_close = '</td></tr>';

//get key and field values from postformAR
foreach ($postformAR as $key => $value)
{
$captions[] = str_replace('_', ' ', $key);
if (PREVENT_HTTP)
{
if (noHTTP($value))
{ $value = eregi_replace(chr(13),"<br>",$value); $values[] = trim(stripslashes($value)); }
else //spam attempt; redirect somewhere else
{ header("Location:http://www.google.com/"); exit; }
}
else $values[] = trim(stripslashes($value));
}

//build email message content
$message = '';
//build message content for php thankyou page
$thankyou_message_content = '';

if (HTML_EMAIL)
{
$message = $message.'<html><head><title>'.SUBJECT_LINE.'</title>';
$message = $message.'<STYLE>td {font-size:11px; font-family: Verdana}</STYLE></head>';
$message = $message.'<body>';
$message = $message.'<table style="border-collapse:collapse" width="100%" border="1" cellpadding="3" bordercolor="#FFFFFF">';
}

$thankyou_message_content = '<table style="border-collapse:collapse" width="100%" border="1" cellpadding="3" bordercolor="#FFFFFF">';

$num = (count($captions));
if (USING_RESET_BUTTON) $num = $num-1;
for($i = 0;$i < $num; $i++)
{
if (isSeparator($captions[$i]))
{
$thankyou_message_content = $thankyou_message_content.$trtd_open."-----".$trtd_close;
if (HTML_EMAIL) $message = $message.$trtd_open."-----".$trtd_close;
else $message = $message."\n";
}
elseif ( ($captions[$i] <> "submit") && ($captions[$i] <> "Submit") )
{
$thankyou_message_content = $thankyou_message_content.$trtd_open.stripslashes($captions[$i]).': <strong>'.$values[$i].'</strong>'.$trtd_close;
if (HTML_EMAIL) $message = $message.$trtd_open.stripslashes($captions[$i]).': <strong>'.$values[$i].'</strong>'.$trtd_close;
else $message = $message.stripslashes($captions[$i]).': '.$values[$i]."\n";
}
}

$thankyou_message_content = $thankyou_message_content.'</table>';
if (HTML_EMAIL)
{
$message = $message.'</table></body></html>';
$message = wordwrap($message, 72);
}

mail($sendto,$subject,$message,$headers);

//added for displaying the email message on a php thankyou page
$_SESSION['message'] = $thankyou_message_content;
}

function noHeaders($email)
{
$email = strtolower($email);
if ( (stristr($email, "\r")) || (stristr($email, "\n")) || (stristr($email, 'bcc:')) || (stristr($email, 'cc:')) ) return false;
else return true;
}

function noHTTP($message)
{
$message = strtolower($message);
if ( (stristr($message, 'http://')) || (stristr($message, 'https://')) || (stristr($message, '[url=')) || (stristr($message, '[/url]')) ) return false;
else return true;
}

function isSeparator($caption)
{
$separator = stristr($caption, 'separator');
if ($separator)
{return true; }
else
{return false; }
}

?>