SUPPORT >> PROGRAMMING >> ASPMAIL
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 information from your site as an email to any number of recipients. The information can be specified directly or it can be the results of a form.
Please note, the ASPMail code can only be added to an ASP page and it needs to be added directly to the code. If you are using a web builder like FrontPage you will need to be in the HTML view when entering the code.
With HostMySite.com's recent addition of Spam filtering software, you will need to add an "X-MimeOLE:" header to your ASPMail script to allow the message to be sent correctly. You can see an example of this in the code below.
Below are examples of using ASPMail to send specific information as well as using ASPMail to send the results of a form.
To send specific information as an email from your website you will need to add the following block of code.
<%
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 = "Form Submission"
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
%>
* 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 to first create the HTML form. Within the form you will need to set the action to the page containing your ASPMail code. For example:
<form action="aspmailform.asp" method="post">
Next, you will need to create the ASP page specified in the action which will contain the ASPMail code. You will start with the above form and replace the Mailer.BodyText line with the following:
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
The pertinent lines of this code are explained below:
| Set Mailer = Server.CreateObject("SMTPsvg.Mailer") |
Required: Do not alter this line |
| Mailer.FromName = "website" |
Optional: this text will appear in the "from" field on the Email message that is sent. |
| Mailer.FromAddress = "email@yourdomain.com" |
Required: a valid Email address from your domain. |
| Mailer.RemoteHost = "mail.yourdomain.com" |
Required: Enter the address of the mail server for your domain. |
| Mailer.AddRecipient "Name", "name@yourdomain.com" |
Required: Separated into two parts. The first part ("Name") can be any name or an email address. The second part ("name@yourdomain.com") must be a valid Email address. |
| 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...
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
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. |