Instructional Module W26d

Understanding FormMail


to Top Overview

In many situations where a Website needs to get information from its visitors, the process can be simplified by using pre-written software. One of the more popular software packages for this purpose is FormMail. FormMail allows Web designers to take returned user data and do some commonly needed procedures in a simple, elegant way - without needing to program anything. All the control is carried out using "hidden fields" in the HTML form, so anyone who know how to create a form in HTML can make use of it. (For a review of HTML forms, see module W26c.)

In this module, we'll take a look at FormMail, the fields used to control it, and what it can be used form.


to Top What Is FormMail?
Where FormMail fits in

to Top
Link to Top

To understand where FormMail fits in, let's take a look at the process of getting information from a user to the Webmaster or whoever needs to process the information.

CGI FormMail process

1 User fills in form, hits "Send" button. The user follows the normal procedure for filling in a form. When done, they press the button that send the form. (The button may have some label other than "send", such as "Continue", "Done", or whatever the page designer thinks appropriate.)
 
2 Form information goes to server. In general, this can be done either by "Get" or "Post" methods, though FormMail requires the Post method. Get and Post are two variants of the Common Gateway Interface (CGI) protocol.
 
3

Input processed by FormMail on server. When form data is sent to a server, it can be emailed directly to an email address, but is usually handled by some software or other. Software to handle this data can be written in any computer programming language, but one of the most widely-used is the Perl language. FormMail is written in Perl.

By convention, software for handling CGI is usually kept in a sub-directory named cgi-bin (bin is short of "binary" - that is, executable code).

 
4 Feedback page to user. Though not strictly necessary, it's helpful to users when the server sends back some sort of acknowledgement, usually as an HTML page that displays on the user's screen. FormMail makes this relatively easy.
 
5

FormMail sends data to Webmaster as email. The data sent by FormMail is arranged according to instructions it receives in the hidden fields of the form.

If CGI data is handled on the server by specially written programs, it's not usually sent to individuals as email. Instead, it's entered into a database, or requested information is extracted from a database and returned to the user, or some other form of automated processing is done. FormMail does not try to automate the entire process - it arranges the information in a way the makes it easier for people to respond to.

 

 

Where FormMail came from

to Top
Link to Top

FormMail was written in November, 1995, by Matt Wright, and has been upgraded many times since. "FormMail is quite possibily the most used CGI program on the internet, having been downloaded over 2,000,000 times since 1997." You can learn more about FormMail at Matt's Script Archive: http://www.scriptarchive.com/formmail.html


 
to Top How to Use FormMail
What FormMail Does

to Top
Link to Top

FormMail takes information sent in CGI-protocol format and performs simple manipulations for you. Since CGI format is not necessarily easy for humans to read, this can be a great help.

Example: Raw CGI
Explanation:

We'll start with an example of "raw" CGI - sent directly to an email addres without using FormMail.

The example is from W26c, and uses the Stony Creek Historical Museum site to illustrate how it works.

Example code: <html>
<head>
<title>Stony Creek Historical Museum: Volunteer Today!</title>
</head> <body>

<h3>Stony Creek Historical Museum</h3>
<form method=POST action="mailto:krieg@stu.wccnet.edu">

<table border=0 cellpadding=4 align=center>
<tr>
<td align="right">First name:</td>
<td><input type="text" name="FirstName" size=20></td>
<td align="right">Last name:</td>
<td><input type="text" name="LastName" size=20></td>
</tr>
<tr>
<td><strong>What school district do you live in?</strong></td>
<td>
<input type="radio" name="SchoolDistrict" value="L" CHECKED> Lincoln<br>
<input type="radio" name="SchoolDistrict" value="A"> Ann Arbor<br>
</td>
<td>
<input type="radio" name="SchoolDistrict" value="M"> Milan<br>
<input type="radio" name="SchoolDistrict" value="S"> Saline<br>
</td>
<td>
<input type="radio" name="SchoolDistrict" value="Y"> Ypsilanti<br>
<input type="radio" name="SchoolDistrict" value="o"> <em>other</em><br>
</td>
</tr>
</table>
<div align="right"><input type="submit" value="Send"></div>
</form>

</body>
</html>

Browser View:

Stony Creek Historical Museum

First name: Last name:
What school district
do you live in?
Lincoln
Ann Arbor
Milan
Saline
Ypsilanti
other
Example email: FirstName=Sarah&LastName=Strong&SchoolDistrict=Y

 

Here are some of the more useful things FormMail does, and how to do them.

Action: ~
Explanation:  
Hidden Field:

~~~

Example code: ~~~
Example email:  

 

Action: Send Data as Email
Explanation: There is the one essential thing you must tell FormMail: where to send the information! That takes the form of an email address.
Hidden Field:

recipient

Example code: <FORM ACTION = "/cgi-sys/formmail.pl" METHOD = "POST">
<input type=hidden name="recipient" value="ssw101@stu.wccnet.edu">

Example email:  

 

Action: Add a Subject to your Email
Explanation: Depending on the browser the user is in, you may get a blank subject, or something meaningless. You can set the email subject to something useful this way.
Hidden Field:

subject

Example code: <input type=hidden name="subject" value="Another Volunteer for SCHM!">
Example email:  

 

Action: Let the User Add a Subject to your Email
Explanation: If you would like the user to enter a subject of their own - for example, if you're having them send you email from your Web page - you can do it simply by allowing the subject field to be visible.
Visible Field:

subject

Example code: <input type=text name="subject">
Example email:  

 

Action: Send the User to Another Page
Explanation: Users will get a default response after sending in their information. But if you prefer, you can have FormMail redirect them to a different Web page.
Hidden Field:

redirect

Example code: <input type=hidden name="redirect" value="http://yourdomain.com/to/ file.html">
Example email:  

 

Action: Require the User to Fill In Certain Fields
Explanation: In many forms, some fields are optional and others are required. You can get FormMail to reject the user's submission if they didn't fill in the required fields.
Hidden Field:

required

Example code: <input type=hidden name="required" value="FirstName,LastName">
Example email:  

 

Action: Send you Info about User's Environment
Explanation:

The user's "environment" is the software and domain they are running in. You can get FormMail to send you this information:

  • Browser brand and version
  • Domain name of user's ISP
  • IP address of user's ISP
Hidden Field:

env_report

Example code: <input type=hidden name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT">
Example email:  

 

Action: Sort the Fields
Explanation:

You can get the data fields in three orders:

  1. Same order as in the HTML page. This is the default, so you don't need to do anything for this.
  2. Alphabetical order, by the name of the field (not in order by what the user typed in).
  3. Arbitrary order: any order you specify.

Why would you want to have the fields in a different order than they appear in the HTML page? Possibly to make it easier for you to create a report, or process the information with a program.

Hidden Field:

sort

Example code:

<!-- Sort alphabetically by field name -->
<input type=hidden name="sort" value="alphabetic">

<!-- Sort in arbitrary order -->
<input type=hidden name="sort" value="order:SchoolDistrict,LastName,FirstName">

Example email:  

 

More Information

In addition to the capabilities listed here, FormMail can do a few other things with the data. Full information, including how to set up FormMail on a server, is available at ScriptArchive: http://www.scriptarchive.com/readme/formmail.html

Try This

to Top
Link to Top

...Coming Soon...


to Top About This Document
Audience

to Top
Link to Top

This module is for people who are familiar with HTML forms (see module W26c) and would like to learn how to use the multi-purpose, freeware CGI script, FormMail.cgi. No previous understanding of scripting is expected.

 

Objectives

On successful completion of this module, you will be able to:

  1. Describe the process by which form information travels from the person who fills out the form to the person who processes it
  2. Explain the role of the open-source utility FormMail.pl
  3. Explain what "hidden fields" are and what they are used for in FormMail.pl
  4. List the hidden fields used by FormMail.pl, their purposes and potential usefulness.
  5. Demonstrate use of FormMail.pl in some simple examples.
Link to Top
Module W26d: Understanding FormMail.cgi
This document is part of a modular instruction series in Computer Instruction. For more information, see the overview or the list of modules in this series, W: World Wide Web. This document has been used in the following classes: INP 270 .
History
Original: 14 April 2003, by Laurence J. Krieg
Last modification: Monday, 31-Aug-2009 11:48:06 EDT
Copyright
Copyright © 2003, Laurence J. Krieg, Washtenaw Community College
Instructors: You may point to this file in your Web-based materials; however, its location may change without notice.
Students: You are welcome to make a copy for your personal use.
All other uses: Please contact the author, Laurence J. Krieg, for permission: krieg@ieee.org.