Make A Web-based E-mail Form Using PHP

Published Date
01 - Aug - 2005
| Last Updated
01 - Aug - 2005
Make A Web-based E-mail Form Using PHP
Consider this scenario-you have a Web site but you haven't yet set up e-mail IDs on it. Or for some reason you wouldn't like to do so (maybe because it's so tedious!).

However, if you have a Web site and would like to receive feedback or communication from your clients or visitors, you must have a business e-mail ID ( To have a Yahoo! or Hotmail address for business purposes is not only un-chic, it also seems downright unprofessional.

Regardless of whether or not you have a business e-mail ID, you may feel it takes a lot of effort on the part of the visitor or client to e-mail you. You may even want to do away with the e-mail addresses given in the 'Contact Us' page.

Let's take a look at what it takes to build a Web-based form ('Contact', 'Feedback', what have you) using PHP (Hypertext Pre-Processor, an HTML-embedded scripting language). You won't believe how easy it is. What's more, there are no restrictions on the number of forms you can create! However, to understand this workshop, a basic idea of HTML is essential. 

What It Does
Assuming you already have a Web site, you would like people to send you feedback or would like them to contact you when in doubt. So when they come to this feedback page (lets call it that, shall we?), they see a form wherein they can enter their details and queries. On clicking 'Submit', the form contents are processed using the PHP file and mailed to a pre-specified e-mail account. This can be any e-mail address in the whole wide world (or Web for that matter).

Step 1. Understanding The Form
For the scope of this workshop, we will consider a simple form consisting of fields for 'Name', 'E-mail', and 'Feedback/Comment'. Also, the PHP file will be called feedback.php.

To create a form, you can use Microsoft FrontPage or Macromedia Dreamweaver. These will let you create the forms in a single click, and you can proceed straight to the section about adding the PHP page and the reference in the form.

If you do not have the above- mentioned software, you will have to whip out Notepad and do it the good old manual way. If this page is going to have just this form, then, after you put in the <head>, tags, you need to put in the code for the form.

The code has two parts: the field title (such as 'Name', 'E-mail', and so on), and the actual place where the data will be entered.

Step 2. Writing The Code
The first line that you need to write is:
<form method="POST"action="">
This is the line that tells the browser to send the form data to the PHP file.
The URL must be absolute, i.e., it should incorporate the directory structure of your Web site. For instance, if you have made a directory in your Web site called 'phpforms' and have placed the feedback form in that directory, in the 'action' field above, your URL will become: ''

Step 3. Adding The Fields
To add the input fields such as 'Name' or 'E-mail', you need to write the following lines of code exactly as they appear here. Make sure all the lines are between the <body> and tags.
Name: <input type="text" name="T1" size="20">
Here, "Name:" is what you will see when you open the page in the browser. This will be followed by an empty text box-more on that later.

Make note of the entry "name="T1"" inside the <> symbols. Here, 'T1' is the name of the input box and can be anything you choose, but make sure you remember it as we will be using it in our PHP file.

To create more fields such as 'E-mail' and 'Comment' simply use the above code with the name field changed. You should take care that no two fields can have the same name, so if you are calling the first field (i.e., the field where you are entering the users name) 'T1', then no other field on the same page can be called 'T1'.

When you create the field for 'Feedback/Comment', it will obviously be greater than the field for 'Name', 'E-mail', and so on. Go back to the code above and in place of input type="text" you must define your text box as:
<TEXTAREA style="WIDTH: 230px" name=comments cols=34>

Step 4. Creating The Buttons
To submit the data to the PHP file, we need to tell the browser that the form has been filled and needs to go for further processing. To do that, we need to create a button that will post the form on clicking it. To create the button, you will need to type the following in your Web page source code:
<input type="submit" value="Submit" name="B1">
This will give you two buttons, one to submit and the other to reset the form. Here, anything that you put in place of 'submit' in the value="submit" field will appear as on the button when you view the page.
Again, here name="B1" is the name of the button and should be remembered as well will use the reference in our PHP file. Once you
are done creating the fields, don't forget to the close the form code by adding </form>.
Step 5. The PHP File
The first two characters that you need to enter are '<?'. These signify to the browser that this is a PHP file. We will first add the e-mail address to which you need to mail the form file. To specify the e-mail address, type exactly as it is given below:
Obviously, here you can put whatever e-mail address you want and all the data in the form we created above will be sent to that address as an e-mail. We will now set the subject line of the e-mail so you can identify where the mail has come from and set appropriate filters if you wish.

$subject = "Feedback Form";
Whatever you put in place of the Feedback Form will appear as the subject line in the e-mail you receive from the Web site.

Now we need to link the HTML form we created earlier to this PHP-based form. We need to give the path and tell the PHP file where the form is stored. To do so, enter the following line of code:
$formurl = "";
The URL here must be absolute and not relative.

Step 6. The Error And Thank You Pages
Suppose your users do something wrong or there is a server error, they will then see a page of garbled code and to prevent that, you will need to incorporate an error page. This will be a separate Web page with content showing that an error has occurred. This can be used effectively to ensure that the mandatory fields have been filled.

Include the following code:
$errorurl = "";
And for the 'Thank You' page once the form has been successfully submitted, you need to add:
$thankyouurl = "";
Both the incomplete.html and thankyou.html must reside at the paths mentioned in the file, or the Web visitor will get an error even if he does everything right!

Step 7. Writing The Code For Sending The Form
$name = $_POST['name'] ; //takes the name. Instead of "name" in // enter what ever name you gave to the NAME: field in the HTML form.
$email = $_POST['email'] ; //
$comments = $_POST['comments'];
$http_referrer = getenv( "HTTP_REFERER" );
if (!isset($_POST['email'])) {header( "Location: $formurl"); exit ;}
if (empty($name) || empty($email) || empty($comments)) {header( "Location: $errorurl" ); exit ; }

The code above makes the fields 'Name', 'E-mail' and 'Comments' mandatory. If the user forgets to enter these, he will be asked to go back and fill the form correctly. To remove or add any more fields, add them by keeping the shown format in mind.
if (get_magic_quotes_gpc()) {
    $comments = stripslashes( $comments ); }
$messageproper =        "This message was sent from:n" .
    "$http_referrern" .
    "------------------------- COMMENTS -------------------------nn" .
    $comments .
    "nn------------------------------------------------------------n" ;
The code above is the formatting of the data as it will appear in the e-mail you will receive. 'n' signifies a new line and anything you'd like to add can be entered between double quotes.
mail($mailto, $subject, $messageproper, "From: "$name" <$email>nReply-To: "$name" <$email>nX-Mailer: chfeedback.php 2.02" );
header( "Location: $thankyouurl" );
exit ;
The above few lines are the instructions for the mail programs (server-based, not Outlook) to put the appropriate information in the right fields so that e-mail address is not shown instead of the name and also vice-versa.
'?> //' signifies the end of the PHP file. That's all you need to do! Just save and upload the file and send your first Web-based form!

The Advantages
The benefits of having a such a form are many. As mentioned earlier, you can create it to allow people to get in touch with you instantly without messing around with mail clients or the more tedious Web-based e-mail accounts.

The best part is that you can create multiple forms by just changing the title of the input field and the address you would like it to be mailed to.
Say you have a Web site wherein you would like people to get in touch with you, send you feedback, or report any dead links or errors in your content. You can simply set up the requisite number of forms and change the title of the boxes as required.

We'd advise you to enter different e-mail addresses for each form. So, when you receive the mail, it is organised neatly in terms of content, or if different people handle different aspects such as feedback from visitors or delivery of products, the appropriate e-mail address can be given.

To slightly modify the settings-for instance, if you'd like to conduct a survey and want the results to be mailed to you-simply create a form with more fields than we've done here, and once the visitor is done with it, it will be mailed to you automatically.

If you want a more innovative implementation, you could use the form to take Web-based orders. Before you jump with delight, understand that it is not a replacement to online shopping, but an extension of it can be implemented so your customers can contact you directly and you can service them as soon as you get the e-mail.

Now, you may think, what about spam? Rest assured-there is no cause for concern. To avoid spam, include a mandatory field for telephone numbers which will let you check the authenticity of the mail and you can promptly go about servicing your customers.

The implementations can be many and we're sure you can think of many more original and myriad uses for this PHP form.

Team DigitTeam Digit

All of us are better than one of us.