Source of: emailForm.php
<?php
// put this code so it is in the body section
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
// Variables that you may need to change for this to work are contained in this block
// your form needs to have a text box with the name
// txtEmail
// to work (get or post are ok)
$debug = false; // this variable helps me find a mistake
//the page that has the form on it
$yourURL = "http://www.uvm.edu/~rerickso/myForm.html";
$yourName = "Robert M. Erickson";
// just sets these variable to the current date and time
$todaysDate=strftime("%x");
$currentTime=strftime("%X");
/* subject line for the email message */
$subject = "Web Order: " . $todaysDate ;
// be sure to change Your Site and yoursite to something meaningful
$mailFrom = "Your site <noreply@yoursite.com>";
$cc = ""; // if you needed to Carbon Copy someone (person who fills out form will see this) ex:
// $cc = "webmaster@yoursite.com";
$bcc = ""; // if you need to Blind Carbon Copy (person who fills out form will NOT see this) ex:
// $bcc = "youremail@yoursite.com";
//build your message here.
$message = '<p>This is your confirmation on your order placed on ' . $todaysDate;
$message .= '. please print and keep a copy for your records.</p>';
/* after this the forms information will appended to the message. if you need to customize the
message more you would need to move these two line down in the code. Look for the comment below */
// $$$$$$$$$$$$ build message Here
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//************************************************************
// is the refeering web page the one we want or is someone trying
// to hack in. this is not 100% reliable */
$fromPage = getenv("http_referer");
if ($debug) print "<p>From: " . $fromPage . "should match yourUrl: " . $yourURL;
if($fromPage != $yourURL){
die("<p>Sorry you cannot access this page. Security breach detected and reported</p>");
}
/*
this function just converts all input to html entites to remove any potentially
malicious coding
*/
function clean($elem)
{
if(!is_array($elem))
$elem = htmlentities($elem,ENT_QUOTES,"UTF-8");
else
foreach ($elem as $key => $value)
$elem[$key] = clean($value);
return $elem;
}
// be sure to clean out any code that was submitted
if(isset($_GET)) $_CLEAN['GET'] = clean($_GET);
if(isset($_POST)) $_CLEAN['POST'] = clean($_POST);
/*
we send the forms information to the persons email address they supplied.
the input name must me txtEmail in order for this to work.
*/
if(isset($_CLEAN['GET']['txtEmail'])){
$to = $_CLEAN['GET']['txtEmail'];
} else if(isset($_CLEAN['POST']['txtEmail'])){
$to = $_CLEAN['POST']['txtEmail'];
} else {
die("You are missing the text box for the email address");
}
/* message */
$messageTop = '<html><head><title>' . $subject . '</title></head><body>';
// $$$$$$$$$$$$ build message Here
/* here you can customize the message if you need to */
/* ########################################################################### */
// This block simply adds the items filled in on the form to the email message
if(!empty($_GET)) {
foreach ($_CLEAN['GET'] as $key => $value){
$message .= "<p>" . $key . " = " . $value . "</p>";
}
}
if(!empty($_POST)) {
foreach ($_CLEAN['POST'] as $key => $value){
$message .= "<p>" . $key . " = " . $value . "</p>";
}
}
/* ########################################################################### */
/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
/* additional headers */
$headers .= "From: " . $mailFrom . "\r\n";
if ($cc!="") $headers .= "CC: " . $cc . "\r\n";
if ($bcc!="") $headers .= "Bcc: " . $bcc . "\r\n";
$mailMessage = $messageTop . $message;
/* this line actually sends the email */
if($_POST['txtEmail']!="" || $_GET['txtEmail']!="") {
$blnMail=mail($to, $subject, $mailMessage, $headers);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php print $subject; ?> Print for your records</title>
<meta charset="utf-8">
<meta name="author" content="<?php print $yourName; ?>">
<meta name="description" content="Display Order">
<link rel="stylesheet"
href="style.css"
type="text/css"
media="screen">
</head>
<body>
<h1>Your Request has <?
if ($blnMail==False) {
echo "not ";
}
echo "been processed</h1>";
print "<p>A copy of this message has ";
if ($blnMail==False) {
echo "NOT ";
}
print "been sent</p>";
print "<p>To: " . $to . "</p>";
print "<p>Subject: " . $subject . "</p>";
if ($debug) print "<p>Headers: " . $headers . "</p>";
print "<p>Mail Message:</p>";
echo $message;
?>
</body>
</html>