Bookmark and Share

ASP FormMail

View the source code for this ASP script.

[Updated Feb. 28, 2008.]

This script takes generic form data and sends it as an email. It is modeled after the widely used FormMail script available at Matt's Script Archive but is written in ASP instead of Perl.

Note that it is not a direct port of Matt's script. While it has similar functionality it does not attempt to emulate every feature of that script.

It has support for four of the most widely used email components available and can be easily modified to support others. The email produced uses simple HTML formatting which most email clients can support.

Here's a sample, enter your own email address to receive the results.

Email Address:

Keep in mind that this script will require some customization in order to work on any particular site. You may also wish to alter it to suit your needs better, such as by reformatting the output or adding new options.

Setting Up the Form

Just about any new or existing form can use the script by setting its ACTION attribute to the URL where the script resides and making sure its METHOD is set to "POST".

<form action="/scripts/formmail.asp" method="post">
<input name="_recipients" type="hidden" value="support@example.net" />
<input name="_requiredFields" type="hidden"
  value="Name,Customer ID,Email,Comments" />
Name: <input name="Name" type="text" /><br />
Customer ID: <input name="Customer ID" type="text" /><br />
Email Address: <input name="Email" type="text" /><br />
Comments:<br />
<textarea name="Comments" rows=5 cols=50></textarea>
<input type="submit" value="Submit" />
<input type="reset" value="Clear" />

Like Matt's version, this one uses specially named form fields as parameters for controlling the processing. These can be added to the form as hidden fields.

Control Fields

Below is a listing of these fields. Note that all begin with an underscore ('_') character to distinguish them from any other form fields. Any field name that begins with an underscore is not displayed in the resulting email.

FormMail Control Fields
Field Name Description
_recipients Required The email address to send the form to. Multiple recipients can be specified by separating addresses with commas (',').
<input name="_recipients" type="hidden"
value="admin@example.net" />
<input name="_recipients" type="hidden"
value="sales@invalid.com,orders@invalid.com" />
_subject Specifies the text to use in the email subject line.
<input name="_subject" type="hidden"
value="Site Feedback" />
_requiredFields A comma-deliminated list of field names that should be checked for a value. Any missing values causes an error message to be displayed and the form will not be submitted.
<input name="_requiredFields" type="hidden"
value="Name,Address,City,State,Zip,Email" />
_fieldOrder A comma-deliminated list of field names. When building the email, the fields and values will be displayed in the order specified here. Note that if you use this option, you must specify the names for all form fields you want sent.
<input name="_fieldOrder" type="hidden"
value="Name,Email,Phone,Address,City,State,Zip" />
_replyToField If your form includes a field for the user's email address, you can specify the name of that field here. The address entered by the user in that field will be used on the Reply-To header of the generated email, making it easy to reply to the user.
<input name="_replyToField" type="hidden"
value="Email Address" />
Your email address: <input name="Email Address"
type="text" size="40" />
_ccToField Works the same as _replyToField, except that it also sends a copy of the generated email to that address. Note that this control field overrides the _replyToField code field.
<input name="_ccToField" type="hidden"
value="Email Address" />
Your email address: <input name="Email Address"
type="text" size="40" />
_envars A comma-deliminated list of environment variable names. These can be any of the fields available in the Request.ServerVariables collection.
<input name="_envars" type="hidden"
_continueUrl When the form has been submitted and the email has been sent, the script will display a "thank you" page showing the information included in the email. If you specify a URL in this field, the script will add a 'Continue' link pointing to that URL.
<input name="_continueUrl" type="hidden"
value="myHomePage.html" />
_redirectUrl Specify a URL in this field to redirect the user to a specific page once the email has been successfully sent, rather than showing the default "thank you" page.
<input name="_redirectUrl" type="hidden"
value="myThankYouPage.html" />

You should note that FILE input types are not supported by the script as ASP has no built-in methods for easily dealing with files uploaded from forms.

Before the script can be used, some customization is needed. Much of this depends on the script's location and host environment. There are also options available to help prevent spamming.