ASP FormMail

View the source code for this ASP script.

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.

Name:
Email Address:
Comments:

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">
<p>
<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" />
</p>
</form>

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" />
or
<input name="_recipients" type="hidden"
value="sales@invalid.com,orders@invalid.com" />
_replyTo An email address that will be used in the Reply-To header of the email.
<input name="_replyTo" type="hidden"
value="guest@example.org" />
_replyToField Like _replyTo except that you specify the name of another field on the form. Generally, you'd use the name of a field that asks for the user's email address. Then, the email would have a Reply-To header set to that address making replies easy.
<input name="_replyToField" type="hidden"
value="Email Address" />
...
Your email address: <input name="Email Address"
type="text" size="40" />
_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" />
_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"
value="HTTP_REFERER,HTTP_USER_AGENT,REMOTE_ADDR" />
_redirect Normally when the form has been submitted and the email sent without any errors the script will display a thank you message along with the form data. You can change this by specifying the URL of another page in this field and the user will be sent there instead.
<input name="_redirect" type="hidden"
value="termsofservice.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.