Blog

From the minds of Division Overlay. Learn new techniques with our well formated tutorials, and get project updates all right here.

Introduction

In this tutorial you will learn how to make a basic email form using xhtml, css and php. In this tutorial we will be making a small contact form where the user will fill out a form and using php will email it to your inbox. We will look in to error processing for eliminating some spam in this tutorial as well.

End results:

- Try it -

Step 1 – Basic HTML template

First lets create the basic template for the form to be placed in. This, of course will not be the template you will use, as you will probably implement this into your own design and site. This is the beginning of the template, it contains all the dividing inlays and correct tags to start out the template.

We will need to declare the doctype and create all the necessary header information such as the content-type and title. We also link the style sheet in this section.

In the body section I have placed a number of div tags that will hold the content we will place inside them, using css later we can do some formatting.

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>

<head>

<meta http-equiv=”Content-type” content=”text/html; charset=UTF-8″ />

<title>

Contact form by Division Overlay

</title>

<link rel=”stylesheet” href=”style.css” type=”text/css” media=”screen” />

</head>

<body>

<div id=”wraper”>

<div id=”container”>

</div><!– close container –>

</div><!– close wrapper –>

</body>

</html>

Next lets make the the form with all the inputs and labels for the user to fill out that will ultimately be emailed to you when finished.

To correctly flow with the template we have just made place the create the form inside the “container” div.

<form action=”email.php” method=”post”>

<fieldset><label for=”name”>Full Name: *</label><input type=”text” name=”name” id=”name” /></fieldset>

<fieldset><label for=”email”>Email: *</label><input type=”text” name=”email” id=”email” /></fieldset>

<fieldset><label for=”msg”>Message: *</label> <br /><textarea name=”msg” id=”msg” cols=”30″ rows=”3″></textarea></fieldset>

<div class=”buttons”>

<input type=”reset” value=”Reset” /> <input type=”submit” value=”Send” />

</div>

</form>

Final index.html file

Step 2 – Styling the index

For the sake of the tutorial I chose to add a little style to the page. Although the css is very generic, it is not required to use on your own form. It is simply used to give the tutorial a little flair.

To use the two images in the tutorial download them here:

Graphics

Background Image

Container Box

Final style.css file

Step 3 – Declaring Variables

Now that we have the html template and the page styled using css and the graphics. We need to start building the actual php script that will email you when filled out.

First thing I like to do is declare all my variables at the top of the script for easy reference later on.

<?php

//Gather user input and store in variables.

$name = $_POST["name"];

$email = $_POST["email"];

$msg = $_POST["msg"];

$subject = “Form submission from $name”;

These variables are pulled from the form in the index.html file. I placed them in variable names that would be easy to remember. Name, Email, Message, and the Subject is a static variable and you set it as you please. It can easily be pulled from an input if added on the form.

Step 4 – Error processing

For the sake of sorting out the false emails at the application level, this step should never be overlooked. As this is a basic tutorial, You only learn how to check if the fields are empty. If I get enough demand I will write more on error handling.

We need to check if there is any value to each field (name, email and message) and have the form die if the fields are empty.

if (empty($email)) {

die(“Please enter your email address.”);

}

if (empty($name)) {

die(“Please enter your name.”);

}

if (empty($msg)) {

die(“You must have forgot to enter a message.”);

}

The die() function will stop the form from executing any further. So it will display the first error it catches. If you place an email, but not a name, the name name error will display and stop the script from executing.

If all the fields have input, then we need it to show a message to give the user a peace of mind that their message has been received.

else {

echo “Your email has been sent, Thanks.”;

}

Step 5 – Email formatting

Now that we have all the data we will need to send a message, we need to format the email in a way that is easy to read for you.

Setting up the time stamp. Fairly simple format.

Month text (July) Day, Full Year(2009) Hour:Minutes am/pm.

$todayis = date(“l, F j, Y, g:i a”) ;

This block makes up the body section of the email.

$theMail = “

$todayis\n

From: $name ($email)\n

Subject: $subject\n

Message: \n

$message

“;

This will display the time stamp we just made first. The “\n” is a line break in the text. So the next variable is on its own line. Then it will display the name and email followed by the subject and message.

We need a from line, so you know who the sender is before you open the email.

$from = “From: $email\r\n”;

Step 6 – Email the form.

The script is now ready to email the content to the email you choose.

You’ll need to place your email in the place of “your-email@doamin.org”. You can add more than just your email address if you are needing to send it to several people or departments. You can add a comma and add more email addresses in that section.

mail(“your-email@domain.org”, $subject, $theMail, $from);

?>

End the script and you are ready to upload and test the script.

Final email.php file

Conclusion

Final EmailForm.zip

You should now have a working email form that will email you if all the fields have been filled out. You can do much more with this application, such as check if the email is a real email address, check for words or links you dont want inside the message, the options are endless. I hope this tutorial has helped you out, and you may have learned something new and useful today.

Written by Tyler

Web designer, developer, Funny guy, Thinker, Music lover. Follow me on Twitter @tjefford

You can leave a response, or trackback from your own site.

8 Responses to “Creating a basic email form using PHP”

  1. Chris Says:

    July 16th, 2009 at 8:17 pm

    I think this is a typo:

    You’ll need to place your email in the place of “$lt;a href=”your-email@doamin.org”> your-email@doamin.org”.

    Did you mean to say change your-email@domain.org?

  2. Tyler Says:

    July 16th, 2009 at 8:22 pm

    Thanks for that! The editor I used placed a link there instead plain text. It is fixed now.

  3. Darien Says:

    October 7th, 2009 at 12:50 pm

    sweet thanks got it to work

  4. Keith Says:

    November 9th, 2009 at 4:48 am

    Thanks for this mate been trying to get a form that works for ages, Im a designer not a coder and this has been doing my nut in.

  5. Tyler Says:

    November 9th, 2009 at 4:51 am

    @Keith Im glad I could help. Thanks for taking the time to comment on the article.

  6. Consumer Wealth System Says:

    May 28th, 2010 at 11:48 am

    What Is The Point Of Article Directories…

    sizeable archives of written content covering just about every area …….

  7. create mirror Says:

    July 28th, 2010 at 1:16 am

    can i copy this blog post? i will link back to this post.. please let me know, thanks

  8. Tyler Says:

    July 28th, 2010 at 2:00 am

    @create mirror
    Yes, you may. thank you for asking, and I am glad you enjoyed the article.

Leave a Reply