Using Mailto to Email from a Form

Mailto.exe provides the ability to send data entered into an HTML form to be emailed to one or more specified email addresses. In addition, you can enforce data entry rules on the user such as forcing them to provide an email address (it will even do rudimentary email validation). Once the data has been processed, the browser can be sent a simple message verifying completion of the process, or it can even be sent a formatted HTML page.

Usage

Let's say we had a simple form that looked like like this:

First Name:
Last Name:
Email Address:

Note: You will return to this page if you submit the data.

The code for this form looks like this:-

<form action="/scripts/mailto.exe" method="POST">
<input type="hidden" name="email_email" value="Please enter an email address">
<input type="hidden" name="sendto" value="recipient@mydomain.com">
<input type="hidden" name="server" value="mail.xhost.co.uk">
<input type="hidden" name="SERVER_NAME_display" value>
<input type="hidden" name="resulturl" value="/v2tools/mailto.htm">
<input type="hidden" name="TestForm_counter" value>
<table border="0">
<tr><td>First Name:</td> <td><input type="text" size="20" name="firstname"></td></tr>
<tr><td>Last Name:</td> <td><input type="text" size="20" name="lastname"></td></tr>
<tr><td>Email Address:</td> <td><input type="text" size="20" name="email"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" value="Send Information "></td></tr>
</table>
</form>
<p>Note: You will return to this page if you submit the data.</p>

Notice the first tag which calls the mailto.exe in the /scripts directory. The next three lines specify the fields the user has to fill out. The three lines after that are the "hidden" fields that tell MAILTO what to do with the data it has been given from the form. All the possible "hidden" fields are listed below.

Mailto Fields

In an HTML form, a user enters data info form fields such as Last Name, City, etc. Mailto adds "hidden" fields which are not necessarily visible to the user, but force Mailto to either add information to the email message, execute a particular action, or check a particular "visible" field for correct information. Most of the fields listed below are optional unless otherwise specified.

Email Fields

The following fields specify what is done with the email message.

sendto

The address of the recipient of the email message. If this field is not present, no email message will be sent. For example:

<INPUT TYPE="hidden" NAME="sendto" VALUE="webmaster@mydomain.com">

cc

A comma separated list of other recipients of the form contents. For example:

<INPUT TYPE="hidden" NAME="cc" VALUE="abc@company.com,efg@company.com">

server

Name of one or more mail servers to send the mail to. If more than one mail server is specified (as a comma separated list) then MAILTO will work through the list giving each mail server five seconds before moving on to the next. You should use your SMTP server and backup SMTP server. For example, mail.xhost.co.uk is usually the primary SMTP server, while mail2.mydomain.com would be the backup SMTP server and this would appear as follows:

<INPUT TYPE="hidden" NAME="server" VALUE="mail.xhost.co.uk,mail2.mydomain.com">

subject

The subject of the mail message. If not specified, the value variable "HTTP_REFERER" will be used.

resulturl

The full URL for the next page to display if the mail operation was successful. If neither this nor resulttxt is defined, a default "thank you" message will be displayed.

resulttxt

Some text that is displayed in the "Thank you" message after the mail message has been sent. This allows a form to be created quickly. If neither this nor resulturl is defined, a default "thank you" message will be displayed.

uname

Name of the user who has completed the form. This field will be placed in the "From:" clause of the email message to aid filing.

email

The email address of the person filling out the form. The contents of this field will also be placed into the From: clause of the mail message. When you receive this message, you will be able to type "Reply" and reply to the person who filled the form.

webmaster

This field contains the email address of the World Wide Web Server Administrator. Whenever an error message is displayed and this value is defined, a "selectable" email address will be appended to the end of the message.

Field Contents fields

These hidden fields have the same name as the field they control except that they have a _command afterwards and the value may be text message to display to the user if the test fails. More than one command may be used for each field. The MAILTO program will search for any conditions that fail and display all the error messages at the same time. This means that the user may be presented with a list of things to correct rather than one item at a time.

_required

This command indicates that the specified field must be completed by the user before the email message is sent. As long as the user enters something, the test is satisfied. If nothing is entered, the text in the "value" clause is displayed. For example:

<input type="hidden" name="uname_required" value="Please enter your name">

will force the user to enter something in the field called "uname". If they do not enter anything, they will be told:

"Please enter your name"

_counter

The Mailto program will maintain a set of counters so that you can uniquely identify every message that has been processed. The _counter command will access the specified counter, increment it and place the result in the email message. If any of the conditions fail, the counter will not be incremented. For example:

<input type="hidden" name="applications_counter">

will add the following line to the resulting email message:

applications = 3

If the counter does not exist, it will automatically be created and given the value 1.

_email

Check that the specified field could be a valid email address. The check that MAILTO carries out is to make sure the text entered has a "@" sign in it. Thus "mark" would fail and "mark@company.com" would succeed. Note that "mark@" would also succeed and that the domain name is not verified. If the check fails, the message in the "VALUE" part is displayed. For example:

<input type="hidden" name="email_email" value="Please enter your email address">

will force the user to enter an email address in the field called "email". If they do not enter anything they will be told:

Please enter your email address

Please Note: if you require better error handling than this, we suggest you give ColdFusion a shot. Read through the ColdFusion manuals for more information.

_num

Check that the specified field contains a valid number. The following characters are allowed in a number:

0 1 2 3 4 5 6 7 8 9 0 + - . , E e

If anything else is entered the user will be presented with the text in the "value" field. For example:

<input type="hidden" name="age_num" value="Please enter your age in years">

will force the user to enter an number in the field called "age". If they do not enter anything they will be told:

Please enter your age in years

_display

This command can be used to place the values of variables supplied by the WWW server into the email message. The special field *_display will cause all variables from the WWW server to be displayed - this can be used to select those that may be of interest. The following values may be of interest:

SERVER_SOFTWARE Name of the server software.
SERVER_NAME Name of the WWW server.
SERVER_PROTOCOL HTML protocol supported by the server.
SERVER_PORT Port number of connection from remote client.
REMOTE_HOST IP address of the remote client.
REMOTE_ADDR IP Address of the remote client.
HTTP_USER_AGENT The remote client software.
HTTP_REFERER URL of the page where the form was entered.
Thus the command:
<input type="hidden" name="REMOTE_ADDR_display">

will add the field:

REMOTE_ADDR = 123.123.123.123

to the resulting email message.

_hidden

This command forces the variable to be excluded from the email message or file.

copyright 2003 Xhost Ltd All rights reserved