ServerObjects
ASPMail
Using the ASPMail
script is a very straightforward process. All you need do is specify
a page on your web site as the "action" page for your
form, and then include the ASPMail script on that
page.
Each form on your website will
consist of 2 pages: A "form" page which actually holds
your form, and an "action" page which processes your form
when someone clicks on the "submit" button. For purposes
of this discussion, we will refer to these pages as "form.html"
and "aspmailform.asp". *
*Note: Your action
page must have a .asp extension,
even though it is an ordinary HTML file.
Once you have constructed your form
using regular HTML make sure that your <form> tag specifies
the "post" method, and that the action statement points to
your "action" page. Example:
Simple Mail Example
Using the component is as simple as
- Creating the object
- Setting a few properties
- Calling the SendMail method
The following code demonstrates how to use
AspMail from VBScript. In this example Joe from Joe’s Widgets wishes to send
an email to John Smith. Joe’s mail server is located at mailhost.localisp.net.
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "Joe’s Widgets Corp."
Mailer.FromAddress= "Joe@somehost.com"
Mailer.RemoteHost = "mailhost.localisp.net"
Mailer.AddRecipient "John Smith", "jsmith@anotherhostname.com"
Mailer.Subject = "Great SMTP Product!"
Mailer.BodyText = "Dear Stephen" & VbCrLf & "Your widgets order has been processed!"
if Mailer.SendMail then
Response.Write "Mail sent..."
else
Response.Write "Mail send failure. Error was " & Mailer.Response
end if
By testing the result of the SendMail method we
can determine if the mailing process was successful or not.
Form Handling
All or partial input for a message may come
from a form. For example, a form posted to the server with a request method of
GET (i.e. <form action="/scripts/AspMail.asp" method=get>) may
provide the message recipient’s email address, subject and message text as
follows:
Mailer.AddRecipient Request.QueryString("ToName"), Request.QueryString("ToAddress")
Mailer.Subject = Request.QueryString("Subject")
Mailer.BodyText = Request.QueryString("MsgBody")
The form may also use the POST method (i.e.
<form action="/scripts/AspMail.asp" method=post>) in which case
the code would look as follows:
Mailer.AddRecipient Request.Form("ToName"), Request.Form("ToAddress")
Mailer.Subject = Request.Form ("Subject")
Mailer.BodyText = Request.Form ("MsgBody")
You can use any mixture of static and dynamic
data in setting the components properties as dictated by your needs. For
example, you may wish to send the mail to a single user. In this case you could
modify the code to look something like this:
Mailer.AddRecipient "John Smith", "jsmith@alocalhost.com"
Mailer.Subject = Request.QueryString("Subject")
Mailer.BodyText = Request.QueryString("MsgBody")
Generic Form Handling
In some cases users may wish to use a number of
different forms to send email with the same block of code. ASP allows you to
loop through each QueryString or Form variable and append each one to string
variable which is then assigned to the BodyText property.
strMsgHeader = "Form information follows" & vbCrLf
for each qryItem in Request.QueryString
strMsgInfo = strMsgInfo & qryItem & " - " & request.querystring(qryItem) & vbCrLf
next
strMsgFooter = vbCrLf & "End of form information"
Mailer.BodyText = strMsgHeader & strMsgInfo & strMsgFooter
To return form contents in the original form
order your code might be...
strMsgHeader = "Form Information Follows: " & vbCrLf
for i = 1 to Request.Form.Count
strMsgInfo = strMsgInfo & Request.Form.Key(i) & " - " & Request.Form.Item(i) & vbCrLf
next
strMsgFooter = vbCrLf & "End of form information"
Mailer.BodyText = strMsgHeader & strMsgInfo & strMsgFooter
' Note this code only works for forms containing 128 or fewer field items
Setting Mail Priority
There are a couple of headers that can be
modified to set message priority.
The Priority property sets the message
priority on a scale of 1 to 5. A priority of 1 means HIGH. A priority of 3 means
NORMAL and a priority of 5 means LOW. In addition to this you can also set the Urgent
property if the message status is urgent. The Urgent property is a
true/false property.
How to Use the DateTime Property
The component creates a Date/Time value for the
message based on the calculated GMT time. The DateTime property was added to
allow users to set a custom date/time timezone. The following code demonstrates
how to set the DateTime to US Central Standard Time. By slightly altering the
values underlined you can adjust this to work for your timezone.
[set other Mailer properties]
Mailer.DateTime = WeekDayName(WeekDay(Date), true) & ", " & Day(Date) & " " & MonthName(Month(Date), true) & " " & Year(Date) & " " & FormatDateTime(Now, 4) & " -0600 (CST)"
Mailer.SendMail
Notes About Creating the Mailer Object
You can create the mailer object at two
different points in time:
- Immediately before sending an email
- At the session scope and saved as a session
object
You will have to decide when and where it is
appropriate to create the object based on your particular application. If you
aren't sure which way to create the object reference, or for typical usage, you
should create the object immediately before sending your email. Your code would
look like this:
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
... [Set properties]
if Mailer.SendMail then ...
Creating these local references, as
demonstrated above, allow you to use the object on multiple application threads
at the same time.
To create an object reference at the session
level, your code might look something like this:
if Not IsObject (session("Mailer")) then
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Set session("Mailer") = Mailer
else
Response.write "Cached session object reference being used<p>"
Set Mailer = session("Mailer")
end if
Multiple Host Support
AspMail provides one host property to set up
remote SMTP server addresses. The RemoteHost property should be set to
your primary and secondary server’s address separated by semicolons. In the
event that the primary server is down, AspMail will attempt to use the
secondary server. For example,
Mailer.RemoteHost = "mailhost.localisp.com;mailhost.anotherisp.com"
.
|