Using cgiemail


The instruction below can be used to modify an existing HTML form to work with a presinstalled script called cgiemail.

Example files for the form to work are:
Sample Form
Sample mailtemp.txt file
The mailtemp.txt file and form should be placed in your root directory. Modify the forms to meet your needs and be sure that the input form field names match the names in the mailtemp.txt file.

The cgiemail script resides in the server's cgi-bin directory. Merely reference cgiemail as illustrated in the following HTML form action:

This code calls the presinstalled script cgiemail which is in the server's cgi-bin directory once the web page visitor clicks the "submit" button. The script cgiemail in turn calls a text file (or template) called mailtemp.txt. The template defines the format by which the submitted form content is to be sent via email.

You can create the template in a basic text editor beginning with the following lines which is called the 'header':

To: username@yourdomain.com
From: webform@yourdomain.com
Reply-To: replyname@yourdomain.com
Subject: Information Request
Substitute the desired recipient email address and subject above. Next, you will need to add ALL field names within the HTML form to the template in a form similar to the following:
To: username@yourdomain.com
From: webform@yourdomain.com
Reply-To: replyname@yourdomain.com
Subject: Information Request

Name            [name]
Email           [required-email]
Address         [address]
Phone           [telno]

As long as each form field name is contained within brackets ("[ ]") within the template, the email should be sent successfully and contain all submitted data. We suggest you use all lowercase field names within your HTML form and template. Note that use of the word required in the field 'required-email' means that the program requires an entry before allowing the submission to be complete. Be sure to upload the template in ASCII transfer mode. Also ensure that there are no spaces between the header lines containing the TO, FROM , REPLY-TO, and SUBJECT
There MUST be a space between the header lines and the body portion that contains the form field names.

If you want a specific web page to load when the email is successfully sent, such as a thank you note, add the follow code to your HTML form:

Substitute the URL of the file you wish to load after form data is submitted. The field name in this case MUST be "success", but does not need to be added to the template.

You can also create a custom error page using the following format:
<input type="hidden" name="failure" value="http://www.website.com/formerror.html">

If you are using embedded variables in the success or failure files, ensure that you do not precede the file name with the complete URL information as follows:
<input type="hidden" name="failure" value="formerror.html">

Similarly for the 'success' page.

*** Important ***
If you are implementing a secure form in conjunction with cgiemail, you will need to reference the script in the form ACTION as well as the "success" URL securely.

*** Helpful Tip ***
When you receive the submitted form content via email, you will see that the return address is what you placed in the FROM: section of the mailtemp.txt file. In some instances, you may want to reply directly to the person that completed the form.

First, add a field to the HTML form (eg. "email" to prompt the visitor for his/her complete email address. Next, add the field name to the corresponding template in the following manner:

   To: username@yourdomain.com
   From: [required-email]
   Subject: Information Request
The cgiemail script will insert the address entered in the field named required-email. When the emailed form is received it will be tagged with this return address.

Special formatting

To make a form value show up correctly on a success/failure template, a special formatting directive has been added. It translates certain characters (<, >, &, ") into their appropriate HTML character entities. It is used like this:
[%H,mailaddr]

To include form values in URLs, another special formatting directive has been added. Here is an example of its use:
<a href="/bin/cgicso?query=[%U,name]">lookup</a>

Special variables

Although the date is always included in the header of a mail message, you may also include it in the body by putting [cgidate] in your template file.

Insert [cgirelease] where you want the release number (e.g. 1.4) inserted. Always use this in custom failure templates.

Insert [cgierrmsg] where you want the error message inserted. Always use this in custom failure templates.

Insert [cgierrinfo] where you want additional error information inserted. Always use this in custom failure templates.
Here is an example custom error template:

You didn't fill everything in!

Please go back and fill in at least the required information:

name
quest

Here is the exact error message:

[cgierrmsg]

[%H,cgierrinfo]

cgiemail [cgirelease]

A complete detailed instruction guide is available at:
http://web.mit.edu/wwwdev/cgiemail/user.html


© 1996 SUNNY OASIS