Easy Contact Form
I could not find a decent contact form tutorial that achieves what this one does, This tutorial makes the subject of your email what the user inputs (Not one that you specify in the mailer script) and makes the sender of the email in your inbox actually what the user's inputs as their email. I will first show you the code to achieve this form, Then I will explain it. Put this in your contact page, Where you want the form to appear.
<table border="0" cellpadding="1" cellspacing="1">
<form method="post" action="emailerprocess.php">
<tr>
<td><label for="Name" id="Name">Name:</label></td>
<td><input type="text" name="Name" /></td>
</tr>
<td><label for="Email" id="Email">Email Address:</label></td>
<td><input type="text" name="Email" /></td>
</tr>
<tr>
<td><label for="Subject" id="Subject">Subject:</label></td>
<td><input type="text" name="Subject" /></td>
</tr>
<tr>
<td><label for="Message" id="Message">Message:</label></td>
<td><textarea name="Message" rows="20" cols="20"></textarea></td>
</tr>
</table>
(PS: This is the code from my contact form minus all the CSS styling, If you would like to see how I do that, A tutorial is coming soon.)
Now we will make a new file named emailerprocess.php and put the following in it:
<form method="post" action="emailerprocess.php">
<tr>
<td><label for="Name" id="Name">Name:</label></td>
<td><input type="text" name="Name" /></td>
</tr>
<td><label for="Email" id="Email">Email Address:</label></td>
<td><input type="text" name="Email" /></td>
</tr>
<tr>
<td><label for="Subject" id="Subject">Subject:</label></td>
<td><input type="text" name="Subject" /></td>
</tr>
<tr>
<td><label for="Message" id="Message">Message:</label></td>
<td><textarea name="Message" rows="20" cols="20"></textarea></td>
</tr>
</table>
<input value="Submit" type="submit">
</form>
<?php
$EmailTo = "you@yourdomain.com";
$Subject = Trim(stripslashes($_POST['Subject']));
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$EmailFrom = "$Email";
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailerror.php\">";
exit;
}
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailsuccess.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailerror.php\">";
}
?>
Now you will need another page named emailsuccess.php ... All this needs to be is a page telling the user the email was sent, thanks for emailing, I will reply soon, All that sorta stuff. So I will leave the code of this page up to you, You will also need a page named emailerror.php so that if there is a problem it redirects the user to this page that will let them know, Again... put your own content here, Now read on for an explanation of the code and a few things that need to be customized in the code.$EmailTo = "you@yourdomain.com";
$Subject = Trim(stripslashes($_POST['Subject']));
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$EmailFrom = "$Email";
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailerror.php\">";
exit;
}
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailsuccess.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailerror.php\">";
}
?>
Now for an explanation of the code, (I am not going to explain the HTML... This is a PHP tutorial therefore I will explain what you came here to learn.)
$EmailTo = "you@yourdomain.com";
$Subject = Trim(stripslashes($_POST['Subject']));
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$EmailFrom = "$Email";
This chunk of code defines your email (Seen on top line, Change it to your email.) grabs what the user inputs on the form and defines it for later use.
$Subject = Trim(stripslashes($_POST['Subject']));
$Name = Trim(stripslashes($_POST['Name']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$EmailFrom = "$Email";
This prepares the data for emailing, (Displays the data defined earlier)
Thats about it, Contact me if you are unable to get this to work or if you don't understand a part of the code.
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailsuccess.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailerror.php\">";
}
This code emails you the data (Top line), Determines if the email was successful (2nd line), and if it was a success redirect the user to the page that lets them know it was sent, If it was not a sucess (4th line) it redirects the user and lets them know it was not a sucess (5th line)
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailsuccess.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=emailerror.php\">";
}
Thats about it, Contact me if you are unable to get this to work or if you don't understand a part of the code.

