PHP Tutorials

Email Forms

Learn how to email the contents of a form, includes basic flood protection using cookies...
1
Most websites these days have forms on their contact pages for you to email to. Its quicker and easier so you get more emails.

Using PHP this is made very easy (I did it) ;P lets get started. This tutorial will show you the basic, you may want to take it further.

Firstly, you must create your HTML form that will be displayed, if you know HTML this is fairly easy, here is the code for mine:

<form method="post" action="thanks.php">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="108">Your Name:</td>
<td width="192"><input type="text" id="name"></td>
</tr>
<tr>
<td width="108">Your E-mail:</td>
<td width="192"><input type="text" id="thereemail"></td>
</tr>
<tr>
<td width="108" valign="top">Your Message:</td>
<td width="192"><textarea id="message"></textarea></td>
</tr>
<tr>
<td width="108">&nbsp;</td>
<td width="192"><input type="submit" name="Submit" value="Send Form"></td>
</tr>
</table>
</form>

Now as you can see the bold parts are the names of each text box. These will later be passed as variables so remember them.
2
The next step is to create the PHP page that will send the information and display the "Thank you" page. So guess what I'm calling it? thanks.php.
Now this is where it could seem complicated. This is the part which sends the form contents. In thanks.php I will put the following code in:
<?php
if($sentemail == "2"){
include("sorry.php");
}else{
$num = $sentmessage + 1;
setcookie("sentemail","$num",time()+600); //set the cookie
$email = "Sender Name:\t$name\nSender E- Mail:\t$thereemail\nMessage:\t$message\nIP:\t$REMOTE_ADDR\n\n";
$to = "You@mail.com";
$subject = "Site Message";
$mailheaders = "From: $thereemail <> \n";
$mailheaders .= "Reply-To: $thereemail\n\n";
mail($to, $subject, $email, $mailheaders);
include("thanksecho.php");
}
?>

Onto the next step to run through what the code does.
3
Right, before sending the email the script checks for a cookie in the users browser. This is to stop the user from spamming your email by sending you a billion messages.
<?php
if($sentemail == "2"){ //If user has submitted twice already display sorry.php
include("sorry.php");
}else{
//else set the cookie
$num = $sentmessage + 1; //this adds 1 to the cookie
setcookie("sentemail","$num",time()+600);
//set the cookie
...

The +600 is the time in seconds of how long until it expires. You can edit this to what you wish.
You can also edit the if($sentemail == "2") to the number of your choice. This changes how many emails the users is allowed to send before they are sent to sorry.php.
4
Lets go through the next part.
...
$email = "Sender Name:\t$sender_name\nSender E- Mail:\t$sender_email\nMessage:\t$message\nIP:\t$REMOTE_ADDR\n\n";
$to = "You@email.com";
$subject = "Site Message";
$mailheaders = "From: $thereemail <> \n";
$mailheaders .= "Reply-To: $thereemail\n\n";
mail($to, $subject, $email, $mailheaders);
include("thanksecho.php");
}

The variable $email is what you want to be displayed in the email and includes the basic format. As you can see there are four other variables inside this one. $sender_name, $thereemail, $message, and $REMOTE_ADDR see how these have the same names as our text boxes on the HTML form? all except the last one, this is because $REMOTE_ADDR is a variable that displays the senders IP address. So to wrap it all up, we are going to be sending all the info from our form along with the sender IP address.

The next part is the variable $to. This is where you want the email to be sent so you set this to your email address. $subject, this will be the subject of the email you receive. I've chosen to have "site message" but you could make this whatever you wish. Now, the next two variables are the $mailheaders. This is the information also sent with an email, usually "who the email is from" and "who to reply to" so we will sent this info to. I've set it to From: $thereemail and Reply-To: $thereemail. So when I hit reply in my inbox, it knows who its replying to.

So that's the variables done, simple eh? Lets move on.
5
Ahh, at last the part that sends all the info. The Mail() function.
This is very simple, its the part that says what to mail and who to.

mail($to, $subject, $email, $mailheaders);

Then after it has sent the email, it will display the Thank you message:
include("thanksecho.php");
}

You may be wondering why I have told it to show the file thanksecho.php rather than just echoing "Thanks for the email". This is just so that I can make the thank you page have the design of the site. Its separates the code from the design which helps later when redesigning. Same with Sorry.php.

Now all you have to do is create your sorry.php file and thanksecho.php
6
Another clever thing you can do when creating these files is this. You can include the users name, for example you could say:
Thank you $sender_name, I appreciate your feedback.

Or in the sorry.php:

Sorry $sender_name, you have already sent two emails recently.

Nifty eh? Once you have created your thanksecho.php and sorry.php files your ready to go.

Upload and test it out. Hope it worked out well for you. For an online demo, see my contact page.
This tutorial was brought to you by Robouk, please post any questions in the forum. Thank you.
BACK TO TUTORIALS