PHP Form Mailer (Easy)

{DirectPHP ignore}
For PHP Form Mailer email form results - create 2 files - contact.html and sendmail.php .  You can use an existing form page in place of the contact.html form page -- it will need to call the sendmail.php page in its action.  You can add additional fields and you can remove unwanted fields.  Make sure the field names are located in both files and spelled exactly the same.  The form page can be customized using a wysiwig web page editor or by hand coding, and the script page can be changed by hand coding.

There is no need to specify the smtp server -- the script sends to it automatically.

You need to change the following 3 variables in the sendmail.php file, they are:

$valid_ref1 , $valid_ref2 , and $replyemail

contact.html sample form page (can be named anythingyouwant.html)


contact.html:
<html>
<head>
<title>Contact Us</title>
<style>
.style1 {
font-size: x-small;
}
.style2 {
font-size: small;
}
.style3 {
font-size: medium;
}
.style4 {
color: #0000FF;
}
</style>
</head>
<body>

<form name="phpformmailer" action="sendmail.php" align="center" method="post">
<div align="center"><center><table width="742" cellspacing="6">
<tr>
<td width="162" class="style3"><strong>Contact Us<br>
&nbsp;</strong></td>
</tr>
<tr>
<td align="right" width="162" class="style2">Your name:</td>
<td width="556"><font face="Arial"><input class="inputc" size="50" name="name">
</font></td>
</tr>
<tr>
<td align="right" width="162"><font color="#000080"><span class="style2">*</span></font><span class="style2"> Your email
address:</span></td>
<td align="left" width="556"><font face="Arial"><input class="inputc" size="50"
name="email">
</font></td>
</tr>
<tr align="middle">
<td align="right" width="162"><font color="#000080"><span class="style2">*</span></font><span class="style2"> Confirm email
address:</span></td>
<td width="556" align="left"><font face="Arial"><input class="inputc" size="50"
name="email2">
</font></td>
</tr>
<tr>
<td align="right" width="162">&nbsp;
<p><font color="#000080"><span class="style2">*</span></font><span class="style2"> Your
question or comment:</span></td>
<td width="556"><textarea style="FONT-SIZE: 10pt" name="themessage" rows="7" cols="60"></textarea></td>
</tr>
<tr>
<td width="162"></td>
<td width="556"><p>
<input type="button" class="button"
value="Send Message" name="B1" ONCLICK="javascript:validateForm()">
<span class="style1">You must fill in
the fields marked with a <span class="style4">*</span></span></p>
</td>
</tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</center></div>
<input type="hidden" name="thesubject" value="Web Site Feedback">
</form>

<script language="JavaScript"><!--
function validateForm()
{
var okSoFar=true
with (document.phpformmailer)
{
var foundAt = email.value.indexOf("@",0)
if (foundAt < 1 && okSoFar)
{
okSoFar = false
alert ("Please enter a valid email address.")
email.focus()
}
var e1 = email.value
var e2 = email2.value
if (!(e1==e2) && okSoFar)
{
okSoFar = false
alert ("Email addresses you entered do not match. Please re-enter.")
email.focus()
}
if (thesubject.value=="" && okSoFar)
{
okSoFar=false
alert("Please enter the subject.")
thesubject.focus()
}
if (themessage.value=="" && okSoFar)
{
okSoFar=false
alert("Please enter the details for your enquiry.")
themessage.focus()
}
if (okSoFar==true) submit();
}
}
// --></script>
</body>
</html>

sendmail.php:
<?php
/* PHP Form Mailer - phpFormMailer v2.2, last updated 23rd Jan 2008
(easy to use and more secure than many cgi form mailers) FREE from:
www.thedemosite.co.uk/phpformmailer
Should work fine on most Unix/Linux platforms
*/

// ------- three variables you MUST change below -------------------------------------------------------
$replyemail="Your--email";//change to your email address
$valid_ref1="http://Your--domain/contact.html";// change "Your--domain" to your domain
$valid_ref2="http://www.Your--domain/contact.html";// change "www.Your--domain" to your domain
// -------- No changes required below here -------------------------------------------------------------
// email variable not set - load $valid_ref1 page
if (!isset($_POST['email']))
{
echo 
"";
exit;
}

$ref_page=$_SERVER["HTTP_REFERER"];
$valid_referrer=0;
if(
$ref_page==$valid_ref1$valid_referrer=1;
elseif(
$ref_page==$valid_ref2$valid_referrer=1;
if(!
$valid_referrer)
{
echo 
"";
exit;
}

//check user input for possible header injection attempts!
function is_forbidden($str,$check_all_patterns true)
{
$patterns[0] = 'content-type:';
$patterns[1] = 'mime-version';
$patterns[2] = 'multipart/mixed';
$patterns[3] = 'Content-Transfer-Encoding';
$patterns[4] = 'to:';
$patterns[5] = 'cc:';
$patterns[6] = 'bcc:';
$forbidden 0;
for (
$i=0$i<count($patterns); $i++)
{
$forbidden eregi($patterns[$i], strtolower($str));
if (
$forbidden) break;
}
//check for line breaks if checking all patterns
if ($check_all_patterns AND !$forbidden$forbidden preg_match("/(%0a|%0d|\\n+|\\r+)/i"$str);
if (
$forbidden)
{
echo 
"

STOP! Message not sent.



The text you entered is forbidden, it includes one or more of the following:

"
;
foreach (
$patterns as $key => $value) echo $value."\n";
echo 
"\\n\n\\r
Click back on your browser, remove the above characters and try again.

"
;
exit();
}
else return 
$str;
}

$name is_forbidden($_POST["name"]);
$email is_forbidden($_POST["email"]);
$thesubject is_forbidden($_POST["thesubject"]);
$themessage is_forbidden($_POST["themessage"], false);

$success_sent_msg=' 

 


Message sent!

 


We have also emailed you a copy of your message.

 

';

$themessage "Name: $name \nMessage: $themessage";
mail("$replyemail",
"$thesubject",
"$themessage",
"From: $email\nReply-To: $email");
mail("$email",
"Receipt: $thesubject",
"$replymessage",
"From: $replyemail\nReply-To: $replyemail");
echo 
$success_sent_msg;
/*
PHP Form Mailer - phpFormMailer (easy to use and more secure than many cgi form mailers)
FREE from:

www/thedemosite.co.uk/phpformmailer */
?>