Monday, June 22, 2009

PHP - Customer feedback or simple emailer - A PHP script that enables your visitors to send you emails.

readme.txt

INSTALLATION AND CONFIGURATION

** One major change in version 3.0. You no longer link to emailer.php. You link to whatever form you want to process.
Now, the action of that form has to direct to emailer.php as shown in the sample form.

1- Edit emailer_form.php (or any other form for that matter) by building the form you desire. See reference below for hidden field options.
2- Be sure the action of the form is aimed at the emailer.php file
3- Build the "Thanks" and/or "Error" page(s) if you choose the custom option
4- Edit the top few variable at the top of emailer.php to suit your needs, the available variables are as follows:

<?php
// recipient configuration - this is the array of recipients for the form, the number in the brackets
// must match the number in the "recipient_group" hidden field explained below
$tomail[0]="";
$cc_tomail[0]="";
$bcc_tomail[0]="";
$tomail[1]="";
$cc_tomail[1]="";
$bcc_tomail[1]="";
$tomail[2]="";
$cc_tomail[2]="";
$bcc_tomail[2]="";
// General Variables
// whether or not to check the referrer, 1 for yes, 0 for no
$check_referrer=1;
// domains allowed to use the emailer, with and without www
$referring_domains="http://domain.com/,http://www.domain.com/";
// Default Error and Success Page Variables
$error_page_title="Error - Missed Fields";
$error_page_text="Please use your browser's back button to return to the form and complete the required fields.";
$thanks_page_title="Message Sent";
$thanks_page_text="Thank you for your inquiry";
?>

HIDDEN FIELD OPTIONS
subject - The subject of the email to be sent
reply_to_field - The name of the field that should be put in the "from" field of the email
required_fields - a comma separated list of fieldname to be validated for content
required_email_fields - a comma delimited list of fields to be validated for valid email address syntax
recipient_group - the number that refers to the number in the brackets of the list of recipients to recieve this email
error_page - the filename (and path if necessary) to a custom error page, if missing or blank default text in variable above will be used
thanks_page - the filename (and path if necessary) to a custom thanks page, if missing or blank default text in variable above will be used
send_copy - whether or not to send a copy of the mail to the sender
copy_subject - subject of sent copy
copy_tomail_field - field of email address to the sender for the sent copy
mail_type - layout of email options are "vert_table" or "horz_table", anything else will result in plain text email
mail_priority - 1 is high, 3 is normal
return_ip - do you want the IP of the sender returned with the email if available? 1 is yes, 0 is no

NOTES:
> To call the form link to the emailer.php file NOT the emailer_form.php!
> DO NOT use spaces in the field names of your form. A good option is to use an underscore ("_") instead.
> When making the form, always include the <?php echo $fieldname; ?> as the value of each field. This will make the field populate with the previously entered information should someone not fill out all required fields and see the default error page.
> the recipients_group hidden field may seem confusing, but it is done this way to prevent spambots from harvesting actual email addresses from hidden fields the way many scripts do



header.php

<?php
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Feedback Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<table style="width:600px;border-style:none;" cellpadding="0" cellspacing="0">
<tr>
<td align="left" valign="top">

emailer_form.php

<?php
?>
<html>
<head>
<title>dB Masters FormMailer 3.0</title>
</head>
<body>
<form id="form" method="post" action="emailer.php">
<p>Your Name<br /><input type="text" name="Name" value="<?php echo $Name; ?>" /></p>
<p>Your Email<br /><input type="text" name="Email" value="<?php echo $Email; ?>" /></p>
<p>Comments and/or Questions<br /><text area name="Comments" rows="5" cols="40"><?php echo $Comments; ?></text area></p>
<p>
<input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Reset" value="Clear Form" />
<input type="hidden" name="subject" value="Subject of Email" />
<input type="hidden" name="reply_to_field" value="Email" />
<input type="hidden" name="required_fields" value="Name,Comments" />
<input type="hidden" name="required_email_fields" value="Email" />
<input type="hidden" name="recipient_group" value="0" />
<input type="hidden" name="error_page" value="" />
<input type="hidden" name="thanks_page" value="" />
<input type="hidden" name="send_copy" value="no" />
<input type="hidden" name="copy_subject" value="Subject of Copy Email" />
<input type="hidden" name="copy_tomail_field" value="Email" />
<input type="hidden" name="mail_type" value="vert_table" />
<input type="hidden" name="mail_priority" value="1" />
<input type="hidden" name="return_ip" value="1" />
</p>
</form>
</body>
</html>

footer.php

<?php
?>
</td>
</tr>
<tr>
<td class="bodysm">
</td>
</tr>
</table>
</body>
</html>


emailer.php

<?php
// recipient configuration
$tomail[0]="you@domain.com";
$cc_tomail[0]="";
$bcc_tomail[0]="";
$tomail[1]="";
$cc_tomail[1]="";
$bcc_tomail[1]="";
$tomail[2]="";
$cc_tomail[2]="";
$bcc_tomail[2]="";
// General Variables
$check_referrer=1;
$referring_domains="http://domain.com/,http://www.domain.com/";
// Default Error and Success Page Variables
$error_page_title="Error - Missed Fields";
$error_page_text="Please use your browser's back button to return to the form and complete the required fields.";
$thanks_page_title="Message Sent";
$thanks_page_text="Thank you for your inquiry";

/////////////////////////////////////////////////////////////////////////
// Don't muck around past this line unless you know what you are doing //
/////////////////////////////////////////////////////////////////////////
ob_start();
$required_fields=$_POST["required_fields"];
$required_email_fields=$_POST["required_email_fields"];
$recipients=$_POST["recipient_group"];
$error_page=$_POST["error_page"];
$thanks_page=$_POST["thanks_page"];
$send_copy=$_POST["send_copy"];
$copy_subject=$_POST["copy_subject"];
$copy_tomail_field=$_POST["copy_tomail_field"];
$mail_type=$_POST["mail_type"];
$mail_priority=$_POST["mail_priority"];
$return_ip=$_POST["return_ip"];
if(
$_POST["Submit"]=="Submit")
{
if(
$check_referrer==1)
{
$ref_check=preg_split('/,/',$referring_domains);
$ref_run=sizeof($ref_check);
$referer=$_SERVER['HTTP_REFERER'];
$domain_chk="no";
for(
$i=0;$i<$ref_run;$i++)
{
$cur_domain=$ref_check[$i];
if(
stristr($referer,$cur_domain)){$domain_chk="yes";}
}
}
else
{
$domain_chk="yes";
}
if(
$domain_chk=="yes")
{
$mail="yes";
$req_check=preg_split('/,/',$required_fields);
$req_run=sizeof($req_check);
$error_message="";
for(
$i=0;$i<$req_run;$i++)
{
$cur_field_name=$req_check[$i];
$cur_field=$_POST[$cur_field_name];
if(
$cur_field=="")
{
$error_message=$error_message."You are missing the ".$req_check[$i]." field<br />";
$mail="no";
}
}
$email_check=preg_split('/,/',$required_email_fields);
$email_run=sizeof($email_check);
for(
$i=0;$i<$email_run;$i++)
{
$cur_email_name=$email_check[$i];
$cur_email=$_POST[$cur_email_name];
if(
$cur_email=="" || !eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$",$cur_email))
{
$error_message=$error_message."You are missing the ".$email_check[$i]." field or the email is not a valid email address.<br />";
$mail="no";
}
}
if(
$mail=="yes")
{
if(
getenv(HTTP_X_FORWARDED_FOR))
{
$user_ip=getenv("HTTP_X_FORWARDED_FOR");}
else
{
$user_ip=getenv("REMOTE_ADDR");}
if(
$mail_type=="vert_table")
{
$message="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">
<html>
<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>
<body>
<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" width=\"600\">\n"
;
foreach(
$_POST as $key=>$value)
{
$value=stripslashes($value);
$value=preg_replace("/(http:\/\/+.[^\s]+)/i",'<a href="\\1">\\1</a>', $value);
$value=nl2br($value);
if(
$key != "Submit" && $key != "subject" && $key != "required_fields" && $key != "required_email_fields" && $key != "recipient_group" && $key != "error_page" && $key != "thanks_page" && $key != "send_copy" && $key != "copy_subject" && $key != "copy_tomail_field" && $key != "mail_type" && $key != "mail_priority" && $key != "return_ip")
{
$message=$message."<tr>\n<td align=\"left\" valign=\"top\" style=\"white-space:nowrap;\"><b>".$key."</b></td>\n<td align=\"left\" valign=\"top\" width=\"100%\">".$value."</td></tr>";
}
}
if(
$return_ip==1)
{
$message=$message."<tr>\n<td align=\"left\" valign=\"top\" style=\"white-space:nowrap;\"><b>Sender IP</b></td>\n<td align=\"left\" valign=\"top\" width=\"100%\">".$user_ip."</td></tr>";
}
$message=$message."\n</table></body></html>";
}
else if(
$mail_type=="horz_table")
{
$message="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">
<html>
<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head>
<body>
<table cellpadding=\"2\" cellspacing=\"0\" border=\"1\">\n
<tr>"
;
foreach(
$_POST as $key=>$value)
{
if(
$key != "Submit" && $key != "subject" && $key != "required_fields" && $key != "required_email_fields" && $key != "recipient_group" && $key != "error_page" && $key != "thanks_page" && $key != "send_copy" && $key != "copy_subject" && $key != "copy_tomail_field" && $key != "mail_type" && $key != "mail_priority" && $key != "return_ip")
{
$message=$message."\n<td align=\"left\" valign=\"top\" style=\"white-space:nowrap;\"><b>".$key."</b></td>";
}
}
if(
$return_ip==1)
{
$message=$message."<td align=\"left\" valign=\"top\" style=\"white-space:nowrap;\"><b>Sender IP</b></td>";
}
$message=$message."</tr>\n<tr>\n";
foreach(
$_POST as $key=>$value)
{
$value=stripslashes($value);
$value=preg_replace("/(http:\/\/+.[^\s]+)/i",'<a href="\\1">\\1</a>', $value);
$value=nl2br($value);
if(
$key != "Submit" && $key != "subject" && $key != "required_fields" && $key != "required_email_fields" && $key != "recipient_group" && $key != "error_page" && $key != "thanks_page" && $key != "send_copy" && $key != "copy_subject" && $key != "copy_tomail_field" && $key != "mail_type" && $key != "mail_priority" && $key != "return_ip")
{
$message=$message."\n<td align=\"left\" valign=\"top\" style=\"white-space:nowrap;\">".$value."</td>";
}
}
if(
$return_ip==1)
{
$message=$message."<td align=\"left\" valign=\"top\" style=\"white-space:nowrap;\">".$user_ip."</td>";
}
$message=$message."\n</tr>\n</table></body></html>";
}
else
{
$message="Form Results";
foreach(
$_POST as $key=>$value)
{
$value=stripslashes($value);
$value=nl2br($value);
if(
$key != "Submit" && $key != "subject" && $key != "required_fields" && $key != "required_email_fields" && $key != "recipient_group" && $key != "error_page" && $key != "thanks_page" && $key != "send_copy" && $key != "copy_subject" && $key != "copy_tomail_field" && $key != "mail_type" && $key != "mail_priority" && $key != "return_ip")
{
$message=$message."\n".$key.": ".$value;
}
}
if(
$return_ip==1)
{
$message=$message."Sender IP: ".$user_ip;
}
}
$extra="From: ".$_POST[$reply_to_field]."\n";
$extra.="X-Priority: $mail_priority\n";
$cc_tomail=$cc_tomail[$recipients];
$bcc_tomail=$bcc_tomail[$recipients];
if(
$cc_tomail!="")
{
$extra.="Cc: $cc_tomail;\n";
}
if(
$bcc_tomail!="")
{
$extra.="Bcc: $bcc_tomail[$recipients]\n";
}
if(
$mail_type=="horz_table" || $mail_type=="vert_table")
{
$extra.="MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1\n";
}
$subject=$_POST["subject"];
$tomail=$tomail[$recipients];
mail ("$tomail", "$subject", "$message", "$extra");
if(
$send_copy=="yes")
{
$copy_extra="From: $Name<$Email>\nX-Priority: $mail_priority\n";
if(
$mail_type=="horz_table" || $mail_type=="vert_table")
{
$copy_extra.="MIME-Version: 1.0\nContent-type: text/html; charset=iso-8859-1\n";
}
$copy_address=$_POST[$copy_tomail_field];
mail ("$copy_address", "$copy_subject", "$message", "$copy_extra");
}
if(
$thanks_page=="")
{
echo
"<p>$thanks_page_title</p>";
echo
"<p>$thanks_page_text</p>";
}
else
{
ob_end_clean();
$redirect="Location: ".$thanks_page;
header($redirect);
}
}
else
{
if(
$error_page=="")
{
echo
"<p>$error_page_title</p>";
echo
$error_message;
echo
"<p>$error_page_text</p>";
}
else
{
ob_end_clean();
$redirect="Location: ".$error_page;
header($redirect);
}
}
}
else
{
echo
"<p>Sorry, mailing request came from an unauthorized domain.</p>";
}
}
else
{
echo
"<p>Error</p>";
echo
"<p>No form data has been sent to the script</p>";
}
ob_end_flush();
?>

No comments: