[Jan.07.2009]
 
                                  Email us
Zenguy's Email Script
Today we are going to create an email script to help stop those rotten email bots.
I have remarks in the code to help explain. :)
This code reflects a style sheet that's included with a download at the end of the tutorial.

1st we need to connect to the database.
This file needs to be in the includes folder.
db.php
<?
// You need this to connect to your Database.
// Edit all CAPITOLS below.
$conn = mysql_connect("LOCALHOST","USER","PASSWORD");
mysql_select_db(DATABASE) or die(mysql_error());
?>



Here's a form to send the info.
contact.php
<?PHP
session_start();
?>
<!-- following HTML is a form to submit the info -->
<!-- I am using sessions to save the info in case of a missed field -->
<head>
<link href="includes/style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<!-- Just a spacer table below -->
<table width="480" height="50" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>&nbsp;</td>
</tr>
</table>
<!-- Next a table to hold our form -->
<table width="480" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<!-- Once the form is submited go to contact2.php -->
<form action="contact2.php" method="POST">
<tr>
<!-- Contact_clear.php will clear all sessions of this form (except the ID field)-->
<td height="25" colspan="3" align="center">
<a href="contact_clear.php" class="link_12_black_gray_hover">Clear table</a>
<!-- Set the value below to your admin ID in your user database -->
<!-- (or whatever the user's ID is, that you want the emails sent to -->
<!-- The second input (email) is to trick the bots -->
<!-- They automatically fill in fields named email -->
<!-- The real email field has random name to it (oo456oo)-->
<input type="hidden" name="di555di" value="1"/><input type="text" name="email" style="display: none;" value="1">
</td>
</tr>
<tr>
<!-- Random field names are to help stop bots (name="tt765tt" instead of name="name")-->
<td width="141" align="right" >Your name: </td>
<td colspan="2">
<input type="text" name="tt765tt" size="20" class="txtbox" value="<?PHP echo $_SESSION['s_tt765tt'];?>"/>*
</td>
</tr>
<tr>
<td align="right" >Your email: </td>
<td colspan="2">
<input type="text" name="oo456oo" size="20" class="txtbox" value="<?PHP echo $_SESSION['s_oo456oo'];?>"/>*
</td>
</tr>
<tr>
<td align="right" >Subject: </td>
<td colspan="2">
<input type="text" name="ww908ww" size="20" class="txtbox" value="<?PHP echo $_SESSION['s_ww908ww'];?>"/>*
</td>
</tr>
<tr>
<td align="right" valign="top" >Comments: </td>
<td width="199" valign="top">
<textarea name="mm657mm" rows="8" cols="30" class="txtbox" value=""><?PHP echo $_SESSION['s_mm657mm'];?></textarea>
</td>
<td width="140" valign="top">*
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td colspan="2">
<input type="submit" class="txtbox" name="submit" value="Send Email" />
</td>
</tr>
</form>
</table>



Next we need to make the action to our form.
This is the meat of the code.

contact2.php
<?php
session_start();
// If there is a post with a field named email...it's a bot,
// or possibly it's an auto-form filler. (Google toolbar)
if ($_POST['email'])
{
// This is the function to validate the email
include 'contact3.php';
if(validateEmail($_POST['oo456oo']))
{
// Was there a post? Then proceed,
// If no post user should not be here, send an error message
// (find it at bottom of code, ERROR #1)
if ($_POST['submit'])
{
// Include your db set up
include 'includes/db.php';
// Set your users info from the previous form
$id = $_POST['di555di'];
$name = $_POST['tt765tt'];
$email = $_POST['oo456oo'];
$subject = $_POST['ww908ww'];
$comments = $_POST['mm657mm'];
// If certain fields are empty send an error message
// (find it at bottom of code, ERROR #2)
if ($name !== "" && $email !== "" && $subject !== "" && $comments !== "")
{
// $set is the admin's ID, or whoever you want it to be
$set = $id;
// Get the email from the DB
$result = mysql_query("SELECT * FROM `users` WHERE `id` = '".$set."' LIMIT 1")or die(mysql_error());
$r=mysql_fetch_array($result);
// For this code the email will be sent to user ID # 1 (It's set in the previous page)
$db_email = $r['email'];
// Clean the posted info
// From visitor who posted
include 'includes/clean.php';
$id = clean($_POST['di555di']);
$name = clean($_POST['tt765tt']);
$email = clean($_POST['oo456oo']);
$subject = clean($_POST['ww908ww']);
$comments = clean($_POST['mm657mm']);
// Set date and time
$today = date("m.d.Y");
$time = date("H:i:s");
?>
<!-- The following table is just a link back to your home page -->
<head>
<link href="includes/style.css" rel="stylesheet" type="text/css">
</head>
<!-- Just a spacer table below -->
<table width="480" height="50" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>&nbsp;</td>
</tr>
</table>
<table width="480" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="center">
<td height="40">
<!-- Change below, to a page you want the user to go after the email has been sent -->
<a href="index.php" class="link_12_black_gray_hover">Back</a>
</td>
</tr>
</table>
<?php
// Make a nice looking layout for the email
@$message =
"Message sent: $today at $time:\n\n".
"From: $name\n".
"Email: $email\n".
"About: $subject\n\n".
"--------------------------- COMMENTS ---------------------------\n\n".
$comments.
"\n\n\n-------------------------------------------------------------------------\n\n".
$site;
// Send the email
mail($db_email, $subject, $message, "From: $name <$email>");
?>
<!-- Everything worked!! -->
<table width="480" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
Thank you for your inquiry.
<br />
Your email has been sent.
</td>
</tr>
</table>
<?php
// If certain fields are empty send an error message
// ERROR #2
} else {
// Save any info that was entered...just to be nice :)
// I have the ID session incase you set this up for a usersystem
$id = $_POST['di555di'];
$name = $_POST['tt765tt'];
$email = $_POST['oo456oo'];
$subject = $_POST['ww908ww'];
$comments = $_POST['mm657mm'];
$_SESSION['s_mm657mm'] = $comments;
$_SESSION['s_ww908ww'] = $subject;
$_SESSION['s_oo456oo'] = $email;
$_SESSION['s_tt765tt'] = $name;
$_SESSION['s_di555di'] = $id;
// Close session to save data
session_write_close();
// Send a nice message
echo "<link href=\"includes/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<center>You forgot a mandatory field.<br>";
echo "Click back on your browser control or click ";
echo "<a href=\"contact.php\" class=\"link_12_black_gray_hover\">here</a></center>";
}
// ERROR # 1
// User has come to this page wrongfully
} else {
echo "<link href=\"includes/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<center><p>Error!</p>";
//************** Edit below if need be**************//
echo "<a href=\"index.php\" class=\"link_12_black_gray_hover\">Back</a></center>";
}
} else {
echo "<link href=\"includes/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<center><p>&nbsp;</p>";
echo "Not a valid email address.<br />";
echo "<a href=\"contact.php\" class=\"link_12_black_gray_hover\" title=\"Back\">Back</a></center>";
}
} else {
echo "<link href=\"includes/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<center><p>&nbsp;</p>";
echo "We're sorry, but you may have used an auto email form.<br />";
echo "Our need to protect our email is very relevant due to email bots.<br / >";
echo "Please go back, then clear the table with the link provided. Then refill the form.<br / >";
echo "Sorry for your trouble, we are looking forward to your email.<br / >";
echo "<a href=\"contact\" class=\"link_12_black_gray_hover\" title=\"Back\">Back</a></center>";
}
?>



Now we have the email validation script

contact3.php
<?PHP
session_start();
session_register("s_tt765tt"); // Name field // These must be in quotes " not '
$s_tt765tt = $_POST['tt765tt'];
session_register("s_ww908ww"); // Subject field
$s_ww908ww = $_POST['ww908ww'];
session_register("s_oo456oo"); // Email field
$s_oo456oo = $_POST['oo456oo'];
session_register("s_mm657mm"); // Message field
$s_mm657mm = $_POST['mm657mm'];
function validateEmail ( $s_oo456oo )
{
// Create the syntactical validation regular expression
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
// Presume that the email is invalid
$valid = 0;
// Validate the syntax
if (eregi($regexp, $s_oo456oo))
{
list($username,$domaintld) = split("@",$s_oo456oo);
if (getmxrr($domaintld,$mxrecords))
$valid = 1;
} else {
$valid = 0;
}
return $valid;
}
?>



Lastly, the form has a link to clear the table.
The following will do so.

contact_clear.php
<?php
session_start();
// Clear the tables contents
unset($_SESSION['s_tt765tt']);
unset($_SESSION['s_oo456oo']);
unset($_SESSION['s_ww908ww']);
unset($_SESSION['s_mm657mm']);
echo "<link href=\"includes/style.css\" rel=\"stylesheet\" type=\"text/css\" />";
echo "<meta http-equiv=\"Refresh\" content=\"1; URL=contact.php\"/>";
session_write_close();
?>

Thank you for checking out my tutorial.
Here's a link to the source files.  Linky
Zentyx.com © 2005 - 2009