PHP form to email tutorial – Part 2 – Setting up the PHP
In Part 1 we setup the actual contact form using basic html form tags and form elements.
Here in Part 2, we are going to setup the PHP code to receive the data (or info) from that contact form. Open up your html editor and create a new PHP page. Enter the following code bits, in order, within PHP tags.
First thing, define the variables. These will be the names of the fields exactly as they are in your contact form. In our example, we had 1 text input box called cName and 1 text area box called notes.
Let’s also add a few variables that we will use to send our contact form via email. We don’t need these over on the contact form, but we want to define them here so our PHP script can use them later on.
// define our variables, the info that we will be gathering from the form and any variables we want to use later in the script
$EmailFrom = "youremail_here@where.com";
$EmailTo = "youremail_here@where.com";
$Subject = "Contact Form";
$cName = Trim(stripslashes($_POST['cName']));
$notes = Trim(stripslashes($_POST['notes']));
It’s often handy to have the user’s IP address passed along for troubleshooting if needed, so let’s add in a variable for that:
$user_ip = ($_SERVER['X_FORWARDED_FOR']) ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
Ok, now that everything’s defined, let’s setup what the email message should look like when it’s received. Here we are basically just formatting the look of the message body, making it more readable.
// formatting the data for the email message body
$Body .= "Contact Name: ";
$Body .= $cName;
$Body .= "\n";
$Body .= "Notes: ";
$Body .= $notes;
$Body .= "\n";
$Body .= "Users IP: ";
$Body .= $user_ip;
$Body .= "\n";
Here is a quick check to see if a spambot is adding emails to our form. This is just a basic check, so at some point your going to want to make sure your form isn’t being used by a spammer. I will talk more about this in Part 3 – validation.
For now, let’s just do a simple check to see if an extra line or breaking space is being inserted in either the Name text box.
if ( ereg( "[\r\n]", $cName )) ) {
//if spambot send directly to error page
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.php\">" ;
Ok, so now that we’ve used an “if” statement to check and re-route to an error page if they are a spammer, what do we do if it’s ok to send the form? We use the “else” statement, like this:
// if all good then send our contact form via email and display the results
} else {
// send email to the address specified above and include the data
$success = mail($EmailTo, $Subject, $Body, "From:youremail_here@where.com");
And finally, we display the info that the user entered in the form as confirmation that it was sent and so they have a copy for their records. It also let’s us say “Thank you!” for sending the message:
// display results on page so the user has confirmation of their form details
echo "<h3>Thank you! We have received your message!</h3>";
echo "<br />";
echo "<br />";
echo "<p>Hello ".$cName.", </p>";
echo "<br />";
echo "<p>Following are the details of your message:</p>";
echo "<br />";
echo "<p>".$cName."</p>";
echo "<br />";
echo "<p>Your message was: </p>";
echo "<p>".$notes."</p>";
echo "<br />";
}
So, that’s it! Not too bad right? Remember to type the code snippets above inside PHP tags and to save the file as adaction2.php before testing. If you want to change the name of this file, be sure and go back to your form and edit the form tag so it knows where to send this data:
<form name="adReg" method="POST" action="adaction2.php">
To learn PHP in more detail, check out TiZag – many props to TiZag – I learned more from his tutorials than anywhere on the web.
And to get some additional code samples, check out Steve’s site here.








Want to Leave a Reply?