<?php
/*
** Ok. This script may require some editing at different points along it.
** It's been made very easy to edit when you find the right area. It's
** commented very well at important sections, so you shouldn't have
** any trouble getting things the way you want them. You can support
** any number of form elemets, and format them any way you like in a very
** easy manner. You can also format the 'thank you' page easily as well.
** Look through the script and find what you want to change and/or edit.
** Recommended things to look for are:
** $recipient_email //Email of person getting this email
** $redirect_page //The URL to redirect to after the thanks is displayed
** $redirect_wait //How long to wait on the thanks page
** $thanks_text //Text displayed to user after email successfully sent
** $require_fields //Form fields required before email will be sent
** $subject //Subject of email being sent
** $newsletter //This is just interesting
** $message //Text of the email being sent
*/
/*
** This function used from http://snipplr.com/view/25/format-phone-number/
*/
function format_phone($phone)
{
$phone = preg_replace("/[^0-9]/", "", $phone);
if(strlen($phone) == 7)
return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
elseif(strlen($phone) == 10)
return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
else
return $phone;
}
/*
**This makes all the form fields into variables in the form of
** $f_<form field name>.
** Example:
** If I had a field on my form as:
** <input type="text" name="email" />
** I could access it in this script with:
** $f_email
*/
import_request_variables("gp","f_");
//Your email address
$recipient_email = "name@example.com";
/*
** This is the page your user will be sent to after they send an email.
** Uncomment or edit this option as you see fit. If you comment it out
** the user will not be given a return link or automatically redirected.
*/
//$redirect_page = "/"; //Return the user to your homepage.
//$redirect_page = "http://url.com/path/to/page.html"; //Send user to a specific webpage.
//$redirect_page="http://google.com";
/*
** Time (in seconds) to wait before redirect. If $thanks_page is commented
** out, this does nothing. Don't comment this out.
*/
$redirect_wait = 5;
/*
** The text you put between <<<REDIR and REDIR; will be shown to the user
** as a thank you page. If you have defined $thanks_page above, a link
** will be automatically appended to the end of this.
*/
$thanks_text = <<<REDIR
<h2>Thanks</h2>
Thanks for sending your email to us!
REDIR;
/*
** This array stores a list of required fields. If you put a field name here
** it just means that it must be included in the form and not be blank.
** This does no error or validity checking. That must be handled on an
** individual field basis further on in the script.
**
** This is NOT handled by the variable import at the begginning of the file.
** These names should be exactly as they are in your html file.
*/
$require_fields = array("email","first","last");
$errors = array();
/*
** Make sure all required fields are filled in. Set the required
&& fields at the top of this script.
*/
foreach($require_fields as $value)
{
if(!isset($_REQUEST[$value]) || $_REQUEST[$value] == "")
$errors[] = "The field '<strong>" . $value . "</strong>' is required!";
}
/*
** Validate the email address
*/
if(isset($f_email) && $f_email != "")
{
$emailregex = "/^([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})(\]?)$/";
if(!$val=preg_match($emailregex,$f_email))
$errors[] = "Email address is invalid.";
}
/*
** Format a message to email
*/
//Subject of the email to send
$subject = "Mail from my website";
//Let's make $f_newsletter look a bit more friendly in the email.
$newsletter = $f_newsletter?"Yes":"No";
//Let's make $f_phone a little prettier
$phone = format_phone($f_phone);
/*
**Format the email between the <<<EMAIL and EMAIL;
**It should be emailed exactly as shown between those tags. If you want
**a newline, press return. You can use $f_<form element name> to get the
**strings of your form elements.
*/
$message = <<<EMAIL
<p><strong>Name:</strong> $f_title $f_first $f_last $f_suffix</p>
<p><strong>Email:</strong> $f_email</p>
<p><strong>Phone:</strong> $phone</p>
<p><strong>Address:</strong><br />
<div style="margin-left:5em;">
$f_address1<br />
$f_address2<br />
$f_city, $f_state $f_zip
</div></p>
<p><strong>Newsletter:</strong> $newsletter</p>
<p><strong>Comments:</strong> $f_comments</p>
EMAIL;
$headers = <<<HEAD
MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
From: $f_title $f_first $f_last $f_suffix <$f_email>
Reply-To: $f_email
HEAD;
if(!count($errors))
{
if($result = mail($recipient_email,$subject,$message,$headers))
{
echo <<<HTMLS
<html>
<head>
HTMLS;
if(isset($redirect_page) && $redirect_page != "")
echo "<meta http-equiv=\"refresh\" content=\"".$redirect_wait.";".$redirect_page."\">";
echo <<<HTMLM
</head>
<body>
$thanks_text
HTMLM;
if(isset($redirect_page) && $redirect_page != "")
echo "<p>Click <a href=\"".$redirect_page."\">here</a> to continue</p>";
echo <<<HTMLF
</body>
</html>
HTMLF;
}
else
{
$errors[] = "Mail failed to send! Please contact the webmaster if this happens repeatedly.";
}
}
/*
** Show all errors
*/
if(count($errors))
{
echo "<h2>Error</h2>";
echo "<p>The following errors exist. Please press the browser's back button to correct them.</p>";
foreach($errors as $value)
{
echo $value . "<br />";
}
}
?>