ASP FormMail
View the source code for this ASP script.If any environment variables were requests via the _envars
control field, the script appends another listing to the email message body for
them.
The code is similar to that used to list form fields from the
Request.Form collection. An array of names is derived from the list
of environment variables given and used against the
Request.ServerVariables collection.
<% 'Add a table for any requested environmental variables.
if Request.Form("_envars") <> "" then
body = body _
& "<p> </p>" & vbCrLf _
& "<table border=""0"" cellpadding=""2""" _
& "cellspacing=""0"">" & vbCrLf
envars = Split(Request.Form("_envars"), ",")
for each name in envars
name = Trim(name)
body = body _
& "<tr valign=""top"">" _
& "<td><b>" & name & ":</b></td>" _
& "<td>" & Request.ServerVariables(name) & "</td>" _
& "</tr>" & vbCrLf
next
body = body & "</table>" & vbCrLf
end if %>
Sending the Email
The SendMail() function handles the task of creating the proper
email object, setting the proper parameters and sending it.
As noted before, the script supports four different email components, and
more can be added. The mailComp setting is used in a series of
if statements to decide which code to use. Here's the code for the
CDONTS component that's supplied with Microsoft's IIS web server.
<% if mailComp = "CDONTS" then
set mailObj = Server.CreateObject("CDONTS.NewMail")
mailObj.BodyFormat = 0
mailObj.MailFormat = 0
mailObj.From = fromAddr
mailObj.Value("Reply-To") = replyTo
mailObj.To = recipients
mailObj.Subject = subject
mailObj.Body = body
mailObj.Send
set mailObj = Nothing
exit function
end if %>
The values for the variables recipients, subject, fromAddr, etc.
have all been set at this point from processing the control fields set in the
configuration variables.
CDONTS doesn't provide any return values or error checking on the send call but other components do. For these the function returns any error message or code provided. For example, this code is used when ASPMail is specified.
<% if mailComp = "ASPMail" then
...
if not mailObj.SendMail then
SendMail = "Email send failed: " & mailObj.Response & "."
end if
exit function
end if %>
Displaying an Output Page
The last step is to create some form of output page. If any errors occured,
the script prints out all the error messages collected. If a
_redirect field was given, the script will redirect to the supplied
URL.
Otherwise, it displays a simple "Thank you" message along with body of the email sent. Since the email message was formatted in HTML, the same string value can be used in the page display.
Conclusion
Although the script allows for some input validation, many online forms will require much more stringent checks. But it does serve well for generic purposes and the basic processing flow can easily be expanded to accomodate more complex data validation and processing requirements.