How do I use the ASPMail component to send email from my web page?
The following article explains how to use the ASPMail component within your website. This component allows you to send an email from your website to any number of recipients. The details of the email can be entered directly in the code or can include the results of a form.
With HostMySite.com's recent addition of Spam filtering software, you will need to add an "X-MimeOLE:" header to your ASPMail script to reduce the chances of the message being considered spam. You can see an example of this in the code below.
Send an email
To send email, create/edit a webpage and enter the following block of code, replacing the information in italics with information pertaining to your website and recipients:
<%
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "website"
Mailer.FromAddress= "user@yourdomain.com"
Mailer.RemoteHost = "mail.yourdomain.com"
Mailer.AddRecipient "Name", "name@yourdomain.com"
Mailer.AddExtraHeader "X-MimeOLE:Produced yourdomain.com"
Mailer.Subject = "Subject"
Mailer.BodyText = "Enter the body of the message here."
if Mailer.SendMail then
Response.Write "Form information submitted..."
else
Response.Write "Mail send failure. Error was " & Mailer.Response
end if
set Mailer = Nothing
%>
Note: The Remote Host cannot be a mail server that requires SMTP authentication, but "localhost" can be substituted if the website server permits email sending.
* If you are using a web builder like FrontPage you will need to enter the ASP code in HTML view.
Email the results of a form
To send the results of a form as an email from your website you will need two components:
- An HTML form
- A submission page, for example: aspmailform.asp
The HTML form should reference the submission page in the action, as follows:
<form action="aspmailform.asp" method="post">
The submission page will contain the following block of code:
<%
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "website or display name"
Mailer.FromAddress= "user@yourdomain.com"
Mailer.RemoteHost = "mail.yourdomain.com"
Mailer.AddRecipient "Name", "name@yourdomain.com"
Mailer.AddExtraHeader "X-MimeOLE:Produced yourdomain.com"
Mailer.Subject = "Form Submission"
strMsgHeader = "Form information follows" & vbCrLf
for each qryItem in Request.Form
strMsgInfo = strMsgInfo & qryItem & " - " & request.Form(qryItem) & vbCrLf
next
strMsgFooter = vbCrLf & "End of form information"
Mailer.BodyText = strMsgHeader & strMsgInfo & strMsgFooter
if Mailer.SendMail then
Response.Write "Form information submitted..."
else
Response.Write "Mail send failure. Error was " & Mailer.Response
end if
set Mailer = Nothing
%>
The pertinent lines of the ASPMail code are explained below:
Set Mailer = Server.CreateObject("SMTPsvg.Mailer") | Required:Do not alter this line. | |
Mailer.FromName = "website" | Optional: This will be the Display Name appear in the "from" field on the Email message that is sent. Example: "MyWebSite.com" or "MyCompany" |
|
Mailer.FromAddress = "email@yourdomain.com" | Required: a valid Email address from your domain. Example: john@mywebsite.com |
|
Mailer.RemoteHost = "mail.yourdomain.com" | Required: replace yourdomain.com with your domain name. Example: mail.mywebsite.com |
|
Mailer.AddRecipient "Name", "name@yourdomain.com" | Required:Separated into two parts: |
|
Mailer.Subject = "Form Submission" | Required: This will be the title of your Email message. |
Note: It is not necessary to alter the remaining lines of code. Only alter the remaining lines of code if you are familiar with ASP and ASPMail.
Order your ASPMail Fields
To return form contents in the original form order your code might be...
<%
Set Mailer = Server.CreateObject("SMTPsvg.Mailer")
Mailer.FromName = "website or display name"
Mailer.FromAddress= "user@yourdomain.com"
Mailer.RemoteHost = "mail.yourdomain.com"
Mailer.AddRecipient "Name", "name@yourdomain.com"
Mailer.AddExtraHeader "X-MimeOLE:Produced yourdomain.com"
Mailer.Subject = "Form Submission"
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
if Mailer.SendMail then
Response.Write "Form information submitted..."
else
Response.Write "Mail send failure. Error was " & Mailer.Response
end if
set Mailer = Nothing
%>
Note this code only works for forms containing 128 or fewer field items
Sending HTML Emails
To send the body of the email as HTML include this line:
Mailer.ContentType = "text/html"
ASPQMail
AspQMail uses the AspMail component to send messages to the queue. To use the component you use it the same way you normally would except that you set one additional property.
Mailer.QMessage = true
If QMessage is true then the message will be sent to the \Que directory and queued for delivery. If it is false (the default) it will be sent normally via SMTP. Queuing can greatly increase the response time of your application if you are delivering to several recipients at once.