This script will collect information from a form and send it automatically. If you use form method="get" then just change the HTTP_POST_VARS to HTTP_GET_VARS. Also set the variables in the documentation at the top of the code under the variables title.
<?
/*
Author: Ryan Mortensen
Description: This page will grab all posted form data and send it in an email to whoever is specified:
Variables:
$emailaddress_fieldname = The field name for email address on the previous screen and will test to see if it is valid and if it is it will be place in the from address for the email.
$default_from_address = The from address of the email if the email address sent in the form is not valid.
$from = the from address listed in the contact email.
$required_list = add fieldnames to this comma-delimited list of form fieldnames that are required.
$submit_to = The email recipient's email address
$subject = The subject of the email
$success_page = The thank you page that this page will redirect to if the email is successfully sent.
*/
function valid_email($address)
{
// check an email address is possibly valid
if (eregi("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$", $address)){
return true;
} else {
return false;
}
}
$emailaddress_fieldname = "emailaddress";
$default_from_address = "support@yourdomain.com";
if( valid_email($email_address)){
$from = $email_address;
} else {
$from = $default_from_address;
}
$required_list = "tester,lastname,emailaddress,phone,city,state,product,campaign";
$submit_to = "sales@yourdomain.com";
$subject = "Website Contact Request";
$success_page = "http://www.yourdomain.com/demo/thankyou.php";
$email_address = $_POST["emailaddress"];
$error_list = "";
$form_fields = array_keys($HTTP_POST_VARS);
for ($i = 0; $i < sizeof($form_fields); $i++) {
$thisField = $form_fields[$i];
$thisValue = $HTTP_POST_VARS[$thisField];
$pos = strpos($required_list, $thisField);
if ($pos == true) {
if( trim($thisValue) == ""){
$error_list .= "<BR><font style='color:gray;font-weight:bold'>" . $thisField . "</font> is a required field.";
}
}
if(trim($thisField) <> "Submit"){
$email_content = $email_content . "<br>".$thisField." : ".$thisValue;
}
}
?>
<?
if (trim($error_list) <> ""){
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>[title tag here]</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php require("shared/header.htm"); ?>
<h2 class="pagetitle" id="post-2">Contact Us</h2>
<?
echo "<div style='color:red'>Please fix the problems below to submit this form.";
$error_list = "<div style='color:black;margin-left:20px'>".$error_list."</div>";
echo $error_list . "</div>" . "<br><hr color='silver'><br>Please <a href='javascript:history.go(-1)'>go back</a> and fix these errors to submit this form.";
?>
<?php require("shared/footer2.html"); ?>
</body>
</html>
<?
} else {
$email_content = "<html> <title><head></head></title><body bgcolor='#ffffff'>" . $email_content . "</body> </html> ";
$headers = "From: $from\\r\\n";
$headers .= "Content-type: text/html\\r\\n";
if (mail($submit_to, $subject, $email_content, $headers)) {
if( trim($success_page) <> ""){
header("Location: $success_page");}
echo("<p>Message successfully sent!</p>");
} else {
?>
<html>
<head>
<title>[title tag here]</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php require("shared/header.htm"); ?>
<h2 class="pagetitle" id="post-2">Contact Us</h2>
<?
echo("<p>Message delivery failed. Please contact us at 1-800-123-4567</p>");
?>
<?php require("shared/footer2.html"); ?>
</body>
</html>
<?
}
}
?>