Jul 30,  PST

Creating an Email Web Form with PHP
Written by: Thomas, Haily | Learn how to easily create a secure email web form for your website using HTML and PHP.
 Facebook Google Buzz Mxx Digg StumbleUpon  

Creating the Base of your Form -by Thomas

A PHP e-mail form is very helpful in many ways. You can let your visitors easily contact you. It's also much safer than posting your e-mail on the internet in case a spam bot picks it up.

To get started you need to create a new page where you want the form to display. I made mine "contact.php"

Paste the following code on contact.php
Code:
<form method="post" action="contact2.php">
Name: <input name="name" type="text"><br>
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="5" cols="30"></textarea><br>
<input type="submit" value="Submit Form"><input type="reset" value="Clear Form" name="Clear">
</form>

Next you need to make another page. This page must end with ".php". I made mine contact2.php. This page will process through the information sent by contact.php and send the email.

NOTE: If you decide to change the name of this page make sure you change this code on contact.php:
Code:
action="URL OF NEW PAGE"

On the page you just made put the following code.

Code:
<?php
$to = "EMAIL HERE"; //put the email you want the messages sent to where is says email here
$subject = "SUBJECT HERE"; //put the subject there
$name = $_POST['name'];
$email = $_POST['email'];
$message = "IP: ".$_SERVER["REMOTE_ADDR"]." Message: ".$_POST['message'];
$headers = "From: $name <$email>";
$sent = mail($to, $subject, $message, $headers);
if($sent)
{echo "Your message was successfully sent!"; } //message if email was sent
else
{echo "Your message was not sent. Please try again."; } //message if email was not sent
?>

On the second line put the e-mail you wish the form to be sent to where it says EMAIL HERE.
On the third line put the subject of the message.
You don't need to change anything until the 10th line. Put the message you wish will display if the message was sent.
And the last line to edit is line 12. Put the message you want it to say if the message was not sent. You can use HTML but make sure you put a backslash (\) before any double quotes (").
Example:
Code:
<font color=\"red\"><b>Sorry, your message was not sent. Please try again.</b></font>

If you have enough HTML and PHP knowledge you can probably edit the above code's and add more fields, options, etc. Feel free to customize it to meet your needs.

Adding Security to your Form -by Haily

Now we're going to add some security options to the form to prevent an email from being sent when the user inputs invalid information.

Check for a Valid Email

First we'll add to contact2.php so that we can confirm that the information entered into the email field is a valid address. Add this to the top of the file:

Code:
if(!preg_match("/^[\.A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])){
die("<b>Error!</b> The email address you entered is not a valid email address.");
}

Check for a Valid Message

To prevent spam we also need to check that whatever was entered as the message isn't spam or empty. For this, we will count how many characters are in the submitted message (excluding any whitespace). You can add this also to the top of contact2.php.

Code:
if(strlen(trim($_POST['message'])) < '10'){
die("<b>Error!</b> The message you submitted is too short. Please do not submit spam.");
}

Adding a Captcha

Adding an image confirmation to your web form is easier than you think. Sure, it's complicated to configure your web server to support one of the various options out there for an image captcha. But there a perfect and FREE substitute to to that called reCaptcha. I won't cover how to install reCaptcha to your form mainly because they do it better on their website. All you need to know is the difference between the two files: contact.php (where the form is) and contact2.php (where the form is processed)
Try it: http://recaptcha.net/

The Final Code

That's it! Here's what you should have (without the Captcha).

contact.php

Code:
<form method="post" action="contact2.php">
Name: <input name="name" type="text"><br>
Email: <input name="email" type="text"><br>
Message:<br>
<textarea name="message" rows="5" cols="30"></textarea><br>
<input type="submit" value="Submit Form"><input type="reset" value="Clear Form" name="Clear">
</form>

contact2.php

Code:
<?php
if(!preg_match("/^[\.A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])){
die("<b>Error!</b> The email address you entered is not a valid email address.");
}
if(strlen(trim($_POST['message'])) < '10'){
die("<b>Error!</b> The message you submitted is too short. Please do not submit spam.");
}
$to = "EMAIL HERE"; //put the email you want the messages sent to where is says email here
$subject = "SUBJECT HERE"; //put the subject there
$name = $_POST['name'];
$email = $_POST['email'];
$message = "IP: ".$_SERVER["REMOTE_ADDR"]." Message: ".$_POST['message'];
$headers = "From: $name <$email>";
$sent = mail($to, $subject, $message, $headers);
if($sent)
{echo "Your message was successfully sent!"; } //message if email was sent
else
{echo "Your message was not sent. Please try again."; } //message if email was not sent
?>


 Facebook Google Buzz Mxx Digg StumbleUpon  

| Written by: Thomas, Haily | Added: Sep 25 2009 | Last Modified: May 18 2010 | Views: 2,162 | (Log in to rate) |

Member Comments

Zaphiie of roguecity.netSeptember 26 2009, 4:07 am PST - Karma: 0 - Quote - Link -
Omg, this is just what I've been looking for.... for like.... forever
Yay Thankies!
kyraSeptember 26 2009, 8:15 am PST - Karma: 0 - Quote - Link -
4.5/5
I've been looking for a new contact for myself :O so hopefully this works...xD
Did you guys consider adding a security php snippet that blocks some words? .-. The spam-y and inappropriate ones, I mean o.o
I think there was a tutorial on doing that on tutorialtastic..hm...
Nice job guys : P
Jenny of windymill.skyne...September 26 2009, 12:49 pm PST - Karma: 0 - Quote - Link -
I just downloaded a script from somewhere but this could really help in trying to adjust that script to my liking.
silhouette of tugboat.th...January 18 2010, 12:53 pm PST - Karma: 0 - Quote - Link -
Wow, what a wonderful script. However, it's not working for me. I sent a test e-mail using the form and haven't received anything. /: Could you help me with the problem? I have the form on my page tugboat.dropbeat.net/contact1.php.
IcyJanuary 18 2010, 3:09 pm PST - Karma: 0 - Quote - Link -
Have you checked your spam folder ? Have you set the $to variable to your email , and all other settings accordingly ?
Thomas of icecaves.netJanuary 18 2010, 10:57 pm PST - Karma: 0 - Quote - Link -
5/5
If it still isn't working after following Icy's instructions, post the code from contact2.php here and I'll look for anything you may have accidentally removed while adding the code to your site. =)
silhouette of tugboat.th...January 23 2010, 9:26 am PST - Karma: 0 - Quote - Link -
Uhhh, I'm confused. XD Here's my code:

Code:
<?php
$to = "leseichiest@aol.com"; //put the email you want the messages sent to where is says email here
$subject = "Tugboat's Form"; //put the subject there
$name = $_POST['name'];
$email = $_POST['email'];
$message = "IP: ".$_SERVER["REMOTE_ADDR"]." Message: ".$_POST['message'];
$headers = "From: $name";
$sent = mail($to, $subject, $message, $headers);
if($sent)
{echo "Your message was successfully sent! Expect a reply over the weekend."; } //message if email was sent
else
{echo "Your message was not sent. Please try again."; } //message if email was not sent
?>
  
Thomas of icecaves.netJanuary 28 2010, 4:13 pm PST - Karma: +1 - Quote - Link -
It works for me. I've contacted your host and they said everything should be working. If you have another email try your other one.

You are not currently logged into your IceCaves account. Log in?

Username: Password: | Create an account 

If you would prefer, you can post as a guest below:

Name:
Email: (required; for identification purposes; will not be published)
Site URL: (optional)
Rate:You must log in in order to rate.
Post:
BBCode?

Smilies:
Human Check:

Username:

Password:


FigmintSmiley Helper

The IceCaves.net Topsites


Neo Crave | Enchant Me Not | FGN-Guild.com | Rabidish | Unloadeed | Blue Stapler | Moonwalked.net | RockyRoadRules | KeliJo.net | Daily Neopets | RainbowBliss | Tugboat | Guildpets | NeoWishes | Neo Nutters | Neoeditor | Gamexe.net | Dash of Color | Figmint | Hearted Kind | Darkgirl's Life | Neo-Richies | Geeks' Planet | Skyline Designs