ASP FormMail

Parsing Multiple Values

View the source code for this ASP script.

All the email addresses in the _recipients field are validated using a function called IsValidEmailAddress() which checks that an address follows proper format. Any invalid address results in an error.

It's useful to note how the script separates individual email addresses of this field, as the same technique is used in other control fields that allow multiple values.

<% 'Check all recipient email addresses.

   recipients = Split(Request.Form("_recipients"), ",")
   for each name in recipients
     name = Trim(name)
     if not IsValidEmailAddress(name) then
       call AddErrorMsg("Invalid email address in " _
         & "recipient list: " name & ".")
     end if
   next
   recipients = Join(recipients, ",") %>

The built-in VBScript function Split() is used to break the input string up using the comma (',') character as the delimiter. It returns the individual email addresses as an array, even if only one was given.

Looping through these, it first trims any leading or trailing whitespace from the individual string and then makes the validation check.

Finally, the complimentary Join() function is used to put the individual addresses back into a single string, again separated by commas. They are rejoined for this particular field because almost all email components accept multiple addresses in this format. It allows the same email to be sent to multiple recipients with a single send call.

Processing the Optional Control Fields

The script then checks for the optional control fields. For _replyTo and _subject it merely saves the value for use when sending the email.

The _replyToField works a little differently. Its value is taken to be the name of another field present in the form data. The script takes the value of this other field to be used as the Reply-To address.

<% 'Get replyTo email address from specified field, if given, and
   'check it.

   name = Trim(Request.Form("_replyToField"))
   if name <> "" then
     replyTo = Request.Form(name)
   else
     replyTo = Request.Form("_replyTo")
   end if
   if replyTo <> "" then
     if not IsValidEmailAddress(replyTo) then
       call AddErrorMsg("Invalid email address in " _
         & "reply-to field: " & replyTo & ".")
     end if
   end if %gt;

Again, since this is an email address, the IsValidEmailAddress() function is used to ensure the address format is correct.

Like the _recipients control field, _requiredFields may have contain multiple values separated by commas. It too is first broken up into individual values. Each value is used like the _replyToField value, its value is taken as the name of another field which the script looks for in the form data.

<% 'If required fields are specified, check for them.

   if Request.Form("_requiredFields") <> "" then
     required = Split(Request.Form("_requiredFields"), ",")
     for each name in required
       name = Trim(name)
       if Left(name, 1) <> "_" and Request.Form(name) = "" then
       call AddErrorMsg("Missing value for " & name)
       end if
     next
   end if %gt;

If any of these specified fields has a null string value, an error message is generated.

The _fieldOrder control field is also parsed in this manner, except that the resulting array of field names is not checked, merely saved for use later.

Building the Email Message

Once these control fields have been processed, and no errors have occured, the script then contructs body of the email note, using HTML formatting.

<% 'Build table of form fields and values.

   body = "<table border=""0"" cellpadding=""2""" _
        & cellspacing=""0"">" & vbCrLf
   for each name in fieldOrder
     body = body _
          & "<tr valign=""top"">" _
          & "<td><b>" & name & ":</b></td>" _
          & "<td>" & Request.Form(name) & "</td>" _
          & "</tr>" & vbCrLf
   next
   body = body & "</table>" & vbCrLf %>

The fieldOrder array is simply an array of field names. It is built using the _fieldOrder control, when supplied, or using a function called FormFieldList().

This function corrects a small problem with VBScript. While it's possible to simply loop through all the form fields using a statement like...

<% for each name in Request.Form
     ...
   next %>

VBScript doesn't guarantee that the field names will be given in the same order as they appear in the form request sent by the browser.

To ensure that the fields are listed in the order received, the FormFieldList() function is used to generate the names in an array in the proper order. Here's the code.

<% function FormFieldList()

     dim str, i, name

     'Build an array of form field names ordered as they
     'were received.

     str = ""
     for i = 1 to Request.Form.Count
       for each name in Request.Form
         if Left(name, 1) <> "_" and _
            Request.Form(name) is Request.Form(i) then
           if str <> "" then
             str = str & ","
           end if
           str = str & name
           exit for
         end if
       next
     next
     FormFieldList = Split(str, ",")

   end function %>

Note that it also ignores any fields whose name begins with an underscore character, so control fields are not included.