CGIEMAIL: Sending Form Data Output to Email Recipient

Note: This tutorial is meant for people who are employed by CSU or by CSU Extension who have access to one of the Linux servers on campus that run the cgiemail script. If you don't have access to one of CSU's Linux servers, then this tutorial won't help you unless you are able to run cgiemail on a Linux server that you do have access to.

When creating an HTML form, there are different ways to transmit the form data; often times form data is sent to a database. If you are not familiar with working with databases as well as dynamic scripting languages (such as ASP, ASP.NET, or PHP), then it may be easier to set up your form so that the data is transmitted to a recipient in the form of an email. This tutorial will look at using MIT's cgiemail, which is a Perl script that processes your form data, packages it up into an email, and sends it to the specified recipient.

The following tutorial is based on a more simplified version located at: http://www.acns.colostate.edu/Help/LamarEmailForms

Note: cgiemail does not rely on using the user's email client to transmit the form data. Form data is transmitted through a mail server.

Here at CSU, the cgiemail script is running on the various Unix/Linux servers that are hosted by ACNS. You must place your HTML form on one of these servers in order to get cgiemail to work. This means that you'll need to have an account on Yuma, Holly, Simla, or Lamar and that the files related to your web form will need to reside on your web account on one of these servers. If you do not already have an account for your department or college, you may request one on the ACNS website.

Step 1: Simple HTML Form Example:

Here's a simple web form example that will submit the data in the form of an email to a static recipient.

From (your email address):

Passing form data is the spice of life! Please select one or multiple quotes from the list below:

  • Inside every large program, there is a small program trying to get out. -- Tony Hoare
  • The standard definition of AI is that which we don't understand. -- Bill Joy
  • The best way to have a good idea is to have lots of ideas. -- Linus Pauling
  • Testing can show the presence of errors, but not their absence. -- E. Dijkstra
  • Facts do not cease to exist because they are ignored. -- Aldous Huxley

Note: When you click the Send email button, you will be able to view the form data as it will be displayed in the email. To return to this page, use the BACK button on your browser.

Other comments:

Download this HTML form example. (don't forget to remove the ".txt" file extension and save it as an .html file)

Analysis:

The data output from this form would be sent to a recipient in the form of an email, and the format would look something like this:

Screenshot of Email

The format of the data is determined by a text file that you will need to set up. The text file not only controls the format of the data output, but it also creates the required header information for the email. The text file that created the output above may be viewed here. You should save your text file in the same directory as the HTML form (which will likely be on your Yuma cwis account).

Let's examine the HTML form:

Notice the areas that have been highlighted below. First, look at the <form action> tag and examine each element of the path to the text file. Notice the reference to /cgi-bin/cgiemail/. This must be inserted directly after the root of your domain name and before the remaining path to your text file.

Actual path to file:
  http://www.your-domain.colostate.edu/cewebdev/formdata/ex-simple.txt
Path used in <form action> tag:
  http://www.your-domain.colostate.edu/cgi-bin/cgiemail/cewebdev/formdata/ex-simple.txt

This reference to /cgi-bin/cgiemail/ is necessary and your form will not submit data without it. Also, during the initial testing of your form, you can replace cgiemail with cgiwrap, which will display the form data once submitted, but will not actually transmit the data via email.

<form method="POST" action="http://www.your-doamin.colostate.edu/cgi-bin/cgiemail/cewebdev/formdata/ex-simple.txt">
<p>From: </p><input name="required-from"/>
<p>Passing form data is so much fun! Select one or multiple quotes from the list below:</p>
<ul>
    <li><input type="checkbox" name="whichquote" value="quote-01"/> Inside every large program...</li>
    <li><input type="checkbox" name="whichquote" value="quote-02"/> The standard definition of AI is...</li>
    <li><input type="checkbox" name="whichquote" value="quote-03"/> The best way to have a good idea is...</li>
    <li><input type="checkbox" name="whichquote" value="quote-04"/> Testing can show the presence of...</li>
    <li><input type="checkbox" name="whichquote" value="quote-05"/> Facts do not cease to exist...</li>
</ul>
<p>Other comments: <input type="textarea" name="comments" rows="4" cols="60"/> 
<input type="submit" value="Submit"/>

<--! NOTICE THAT THIS NEXT SECTION GENERATES YOUR "SUCCESS MESSAGE" -->
<input type="hidden" name="addendum" value="
  <hr /><h3>This is the default success message</h3> 
  The text that you are reading here is custom text that you can add as an 
  input type when you create your form (just check the source code of the form 
  that generated this page and you will see). Or, you can create your own 
  web page that the user will be sent to after they have submitted the form; 
  for more information, check here: 
  http://web.mit.edu/wwwdev/cgiemail/user.html#success"/>

</p>
</form> 
      

Step 2: Create the Text File

Once again, the text file not only controls the formatting of the data output, but it also creates the required header information for the email. The first three (or four) lines of the text file are very important. They should conform to the standards of an email message. The Reply-To line is optional; it is a good idea to include it in your text file since certain email clients work better when a Reply-To field has been included. As previously stated, your text file should reside in the same directory as your HTML form file.

Note: In the sample text file below, anything contained within square brackets [...] is pulled in as data input from your HTML form.

From: [required-from]
To: your.email@url.com
Subject: cgiemail:
Reply-to: [required-from]

My choices for quotes:
---------------------------------
[whichquote]

Comments:
---------------------------------
[comments]

Regardless of your choice to use the Reply-To field or not, the following lines must be present in your text file:

From:
To:
Subject:
(blank line)
(start message body)

Important Considerations:

During the initial testing phase of your html form, you can replace "cgiemail" with "cgiecho" in your form method. The use of cgiecho will allow the user to see the results of the processed form data without emailing the data to the recipient; this is great for initial testing so that you don't fill up your email inbox with test messages. Once you are happy with the formatting generated by your text file, replace cgiecho with cgiemail and run email tests.

More Examples:

Additional Information - Write Data to a Text File

This tutorial discusses how to send form data via email to a static recipient. There are other ways of collecting form data. One very common way is to connect your form to a database and collect data from all of your users in the database. This is a difficult process if you have never done it before. If you are not database literate and if you need to collect form data from multiple users (and don't want to deal with hundreds of emails), an easy way to do it is to collect the data in a text file; this data can then be parsed to a database or spreadsheet. Information on this topic may be found here:
http://www.acns.colostate.edu/Help/LamarForms

References:

 

Updated 05/04/2011