A component is a specialized program that can interact with other programs. The Windows "File Open" dialog box most programs use to open files is an example of a component. Authors of various programs (Photoshop or Dreamweaver, for example) do not have to write their own file opening routines -- instead the authors make their program communicate with one of Microsoft's file management components. This saves programmers considerable time and effort.
Components can perform several tasks for web developers. BrowserHawk, for example, can perform very complicated browser detection. Other components can upload files, spell-check documents, and perform other tasks.
ASPEmail, the component we will be working with today, is a free component. Components must be installed on the server by server administrators. ASPEmail will only work with Windows servers, or with Unix servers running ChiliSoftASP. Components like ASPEmail are often used for routine tasks, like emailing password reminders or emailing users a copy of their order. These two examples are fairly complicated, and work best when used with a database.
In this scenario we'll do something a little easier -- we'll construct a simple Customer Support email system. We'll build a form that collects information from the user, and then send the data to a new page which will compile the information and send it in an email.
More information on ASPEmail is available on the developer's website.
Creating the Form
Names and capitalization are VERY important in this exercise. Do yourself a favor and name your files, form items, and variables EXACTLY as they are named below.
In the past few exercises we've used the GET method, which appends the data to the querystring. Get has a limitation we have not discussed -- in older browsers, the Get menthod is limited to 1 KB of data. Since an email could potentially have more characters we are switching to the POST method. In older browsers Post can handle up to 4KB of data. Data sent using Post is not appended to the querystring. (note: In newer browsers there are no size limits for POST and GET.)
Item Label Value I Love Your Website I Love Your Website I Hate Your Website I Hate Your Website I Have Trouble With Your Website I Have Trouble With Your Website Elvis Lives Elvis Lives Other Other **Make I Love Your Website the default value.
Creating The Processing / Display Page
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
firstName = Request.Form("firstName")
lastName = Request.Form("lastName")
emailAddress = Request.Form("emailAddress")
emailTopic = Request.Form("emailTopic")
emailBody = Request.Form("emailBody")
%>
<%
response.write firstName & "<br>"
response.write lastName & "<br>"
response.write emailAddress & "<br>"
response.write emailTopic & "<br>"
response.write emailBody & "<br>"
%>
<%
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Host = "smtp.smtp-server.com" ' Specify a valid SMTP
server
Mail.From = "sales@veryhotcakes.com" ' Specify sender's
address
Mail.FromName = "VeryHotCakes Sales" ' Specify sender's
name
Mail.AddAddress "andy@andrewscompany.net", "Andrew Johnson,
Jr."
Mail.AddAddress "paul@paulscompany.com" ' Name is optional
Mail.AddReplyTo "info@veryhotcakes.com"
Mail.AddAttachment "c:\images\cakes.gif"
Mail.Subject = "Thanks for ordering our hot cakes!"
Mail.Body = "Dear Sir:" & Chr(13) & Chr(10) & _
"
Thank you for your business."
On Error Resume Next
Mail.Send
If Err <> 0 Then
Response.Write "Error encountered: " & Err.Description
End If
%>
Mail.Host = "nlecommerce1.dcccd.edu" 'Specify a valid SMTP Server
Mail.From = "sales@veryhotcakes.com" '
Specify sender's address
Mail.FromName = "VeryHotCakes Sales" ' Specify sender's
name
We need to change these lines so they use the data from our form. Change them to read
Mail.From = emailAddress '
Specify sender's address
Mail.FromName = firstName + " " + lastName ' Specify sender's
name
The first line will now contain the user's email address. The second line is a little more complicated. If you remember, we asked for the user's first name in one form field and their last name in a second form field. We're going to concatonate (join together) these fields so that Mail.FromName is equal to the user's first and last name -- for example, "Nancy Drew." We have to add a space between the two field variables so we don't end up with NancyDrew. If you remember, we inadvertently concatonated numbers in our calculator so that work + bench = workbench.
Mail.AddAddress "andy@andrewscompany.net", "Andrew
Johnson, Jr."
Mail.AddAddress "paul@paulscompany.com" ' Name is optional
This message is being sent to two people -- andy@andrewscompany.net and paul@paulscompany.com. We want to send our message to your Customer Service department (you) and also send a courtesy copy to the person who filled out the form, so they have a copy for their records. Modify the lines as in the example below, using your email address in the second line.
Mail.AddAddress emailAddress,
firstName + " " + lastName
Mail.AddAddress "MyAddress@wherever.com" ' My email address
Mail.AddReplyTo "MyAddress@wherever.com" ' My email address
' Mail.AddAttachment "c:\images\cakes.gif"
Mail.Subject = "Thanks for ordering our hot cakes!"
We need to change this so it reads
Mail.Subject = emailTopic
Mail.Body = "Dear Sir:" & Chr(13) & Chr(10) & _
"
Thank you for your business."
If you look at your test email you'll see it looks like this:
|
Dear Sir: Thank you for your business. |
The text in quotes "Dear Sir:" and "Thank you for your business." appear in the email seperated by a blank line. In the past we've concatonated text using the plus (+) sign. It's also acceptable to use an ampersand (&) as this example does. The blank line is created by the Chr(13) & Chr(10) part of the code.
Typically, ASP commands are written one command per line. The underscore (_) is used when the command takes more than one line. The underscore lets the program know the following line(s) is also part of the command.
We want to modify this code so a short message appears, informing the user that this message is a copy of the message they sent from your site, that the message was sent on a specific date, and that the user can expect a reply in two working days.
We'll use the date command to get the current date from the system.
Modify these two lines so that they look like this:
Mail.Body = "Thanks for sending our Customer Support " & _
" team a message. A reply, if required, will be sent in " & _
"the next forty-eight hours. Your original message was " & _
" sent on " & date & Chr(13) & Chr(10) & _
" -----Original Message--------" & chr(13) & chr(10) & _
emailBody