1. Trang chủ
  2. » Công Nghệ Thông Tin

Teach Yourself E-Commerce Programming with ASP in 21 Days phần 8 ppsx

62 162 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 62
Dung lượng 569,97 KB

Nội dung

LISTING 18.3 Constants for CDONTS to Be Placed in cdonts.inc 1 <% 2 ‘ CDONTS Constants 3 4 ‘ CDONTS Attachment.Type values 5 Const CdoFileData = 1 6 Const CdoEmbeddedMessage = 4 7 8 ‘ CDONTS Message.Importance Values. Also used in NewMail.Importance 9 Const CdoLow = 0 10 Const CdoNormal = 1 11 Const CdoHigh = 2 12 13 ‘ CDONTS Message.MessageFormat and Session.MessageFormat Values 14 Const CdoMime = 0 15 Const CdoText = 1 16 17 ‘ CDONTS NewMail.AttachFile and NewMail.AttachURL EncodingMethod Values 18 Const CdoEncodingUUencode = 0 19 Const CdoEncodingBase64 = 1 20 21 ‘ CDONTS NewMail.BodyFormat Values 22 Const CdoBodyFormatHTML = 0 23 Const CdoBodyFormatText = 1 24 25 ‘ CDONTS NewMail.MailFormat Values 26 Const CdoMailFormatMime = 0 27 Const CdoMailFormatText = 1 28 29 ‘ CDONTS Recipient.Type Values 30 Const CdoTo = 1 31 Const CdoCc = 2 32 Const CdoBcc = 3 33 34 ‘ CDONTS Session.GetDefaultFolder Values 35 Const CdoDefaultFolderInbox = 1 36 Const CdoDefaultFolderOutbox = 2 37 38 %> The constants here are used to pass various numeric flags to CDONTS objects. The constants in lines 5–6 specify whether a particular attachment is a file or another message. Those constants in lines 9–11 specify whether a message has low, nor- mal, or high priority. The constants in lines 14–15 and 26–27 signal whether a message is to be transferred as plain text or as MIME. In lines 18–19, the constants specify whether attachments should be transferred as UUEncoded (the standard format for text 418 Day 18 INPUT A NALYSIS 24 0672318989 ch18 3/30/00 8:16 AM Page 418 Using Email from Active Server Pages 419 18 attachments) or Base64 (the standard format for MIME attachments). Lines 22–23 con- tain constants that specify whether the body of a message includes HTML or is exclu- sively text. The constants in lines 30–32 are used when examining the kinds of recipients of a message (for example, whether they are direct recipients or have received a CC or BCC copy of the message). Finally, the constants in lines 35–36 are used to specify which folder to open when using the Session object to open a folder. You will find it useful to specify <% Option Explicit %> in ASP files where you use CDONTS. Because the default value of an unspecified VBScript variable is 0, it is easy to create bugs that are extremely difficult to find. The most common exam- ple of this is misspelling a constant. With Option Explicit, the VBScript interpreter will flag an error when you reference an unspecified variable. Caution Send Yourself Email on Errors In Day 16, you learned how to write errors into a log file when your users encounter a problem with your Web site. This is very helpful to isolate hard-to-reproduce bugs, but it requires that you periodically check the error logs on your Web server. Finding these errors is even easier when you use CDONTS to automatically email you when your users encounter an error. Listing 16.6 shows how to write a CheckError function that writes errors into log files. Listing 18.4 adds a SendErrorLog function to debug.asp, which attaches that log file to an email message. By calling SendErrorLog from CheckError whenever an error occurs, the Web server sends the error and error log to the Webmaster whenever a problem occurs. You can then delete the file from the server. LISTING 18.4 Changes to debug.asp to Send Error Logs in Email 1 <! #include file=”cdonts.inc” > 2 3 Sub SendErrorLog(sLogFileName) 4 Dim NewMailObj 5 Set NewMailObj = CreateObject(“CDONTS.Newmail”) 6 NewMailObj.From = “webServer@levlin.com” 7 NewMailObj.To = “asprecipient@yahoo.com” 8 NewMailObj.Subject = “Web Server Error Log” 9 NewMailObj.Body = “An error occurred on the webserver. The error log ➥ is attached.” 10 NewMailObj.AttachFile sLogFileName, “Error Log” 11 NewMailObj.Importance = CdoHigh INPUT continues 24 0672318989 ch18 3/30/00 8:17 AM Page 419 12 NewMailObj.Send 13 Set NewMailObj = Nothing 14 End Sub First, the CDONTS constants are included in line 1. Then, lines 4–13 create and send a message as in previous listings in this lesson. The differences are that a filename is passed into the subroutine in line 3, and then that file is attached to the email message in line 10. Finally, the importance of the message is set to high in line 11. The results are displayed in Figures 18.8 and 18.9. In Figure 18.8, you might notice that the attachment is called Error Log. You can specify this name in the NewMailObj.AttachFile method (refer to Listing 18.4, line 10). The value of the file- name is arbitrary; that is, you may specify any text for it that you like. 420 Day 18 LISTING 18.4 continued ANALYSIS The appearance of the attachments will vary from mail reader to mail read- er. The relatively Spartan appearance of Figure 18.8 is due to use of the Yahoo! mail reader. Note that the Yahoo! mail reader does not display the high importance (line 11) of the message. Whether and how the NewMail.Importance property is displayed is up to the particular mail reader. (The Importance property is displayed in Microsoft Outlook and Microsoft Outlook Express.) Note FIGURE 18.8 The text of the error email. 24 0672318989 ch18 3/30/00 8:17 AM Page 420 Using Email from Active Server Pages 421 18 Sending New Users Email Many E-Commerce sites send their users email after registration. This allows the site to reconnect with its customers, and provides an opportunity to encourage the customers to revisit the Web site—perhaps by sending them a coupon or other special offer. Using the techniques we have learned so far today, this feature is simple to add to the Candy Store Web site. First, we will add the sendNewUserMail function in Listing 18.5 to the storeFuncs.asp file. Then, as the last line of the addUser function, call the sendNewUserMail function with the user’s name and email address. When a user successfully completes the registra- tion form, the sendNewUserMail function will automatically send that user an email (as shown in Figure 18.10). F IGURE 18.9 The error log attachment. This function requires adding <! #include file=”cdonts.inc” > to the top of storeFuncs.asp in order to give the function access to the CDONTS constants. Note 24 0672318989 ch18 3/30/00 8:17 AM Page 421 LISTING 18.5 The sendNewUserMail Function 1 SUB sendNewUserMail(sUserName, sUserMail) 2 Dim NewMailObj 3 Dim sMailBody 4 5 Set NewMailObj = CreateObject(“CDONTS.Newmail”) 6 NewMailObj.From = “customer-service@JohnsonGifts.com” 7 NewMailObj.To = sUserMail 8 NewMailObj.Subject = “Welcome to Johnson Candy and Gifts” 9 NewMailObj.MailFormat = CdoMailFormatMime 10 NewMailObj.BodyFormat = CdoBodyFormatText 11 sMailBody = “Dear “ & sUserName & “,” & vbNewLine & vbNewLine 12 sMailBody = sMailBody & “ Thank you for registering at our site!” & ➥ vbNewLine & vbNewLine 13 sMailBody = sMailBody & “ We look forward to serving you in the ➥ future. “ 14 sMailBody = sMailBody & “Visit us again soon at ➥ http://www.johnsongifts.com” & vbNewLine & vbNewLine 15 sMailBody = sMailBody & “Sincerely yours,” & vbNewLine & vbNewLine 16 sMailBody = sMailBody & “David Johnson,” & vbNewLine 17 sMailBody = sMailBody & “CEO, Johnson Candy and Gifts.” 18 NewMailObj.Body = sMailBody 19 20 NewMailObj.Send 21 Set NewMailObj = Nothing 22 END SUB The sendNewUserMail function (line 1) takes two strings: the new user’s name and his email address. Lines 5–20 create and send a message as we have in previ- ous listings in this lesson. The passed-in mail address is used to set the destination address of the email in line 7, and the passed-in name is used to personalize the email in line 11. Finally, line 14 sends a URL back to the store Web site so that the user can easi- ly return to the store after reading the message. 422 Day 18 INPUT ANALYSIS Lines 9 and 10 work around a known issue with CDONTS. Unless the MailFormat property of the NewMail object is set to CdoMailFormatMime, the line length of messages is limited to 74 characters or fewer. If the MailFormat property is set to CdoMailFormatMime, however, the default body format will be HTML; therefore, you must also set the BodyFormat property to CdoBodyFormatText. Note 24 0672318989 ch18 3/30/00 8:17 AM Page 422 Using Email from Active Server Pages 423 18 Sending HTML Mail So far, we have used only CDONTS to send text email. Most of your customers, howev- er, are probably using email viewers that enable them to read HTML email. If you are not familiar with it, HTML email is exactly what its name implies: email messages for- matted with HTML tags. This presents you with the opportunity to send your customers eye-catching promotional material through email. When read with an HTML-enabled mail reader such as Outlook, Outlook Express, or Hotmail, HTML messages are more attractive and easier to read than their text equiva- lents. When read with an old-fashioned mail reader such as Pine or elm, these messages look like… well… like HTML tags. Therefore, it’s important to make sure that any cus- tomer to whom you send HTML mail can actually read it. Most sites that send HTML mail ask customers during user registration whether they can read HTML mail. We can easily add this question to the Candy Store site. First, add a Boolean field called user_HTML to the Users table in the database. Then, add the lines in bold in Listing 18.6 to register.asp and to the addUser function in storeFuncs.asp. When a new customer registers, he will now be asked whether he can read HTML- formatted email and the response will be stored in the Users table of the database. F IGURE 18.10 The automatic email sent to a new user. The user_HTML field is already part of the storeDB.mdb file on the CD-ROM that accompanies this book. Note 24 0672318989 ch18 3/30/00 8:17 AM Page 423 LISTING 18.6 Changes to register.asp and to storeFuncs.asp register.asp 1 <% 2 newusername = TRIM( Request( “newusername” ) ) 3 newpassword = TRIM( Request( “newpassword” ) ) 4 email = TRIM( Request( “email” ) ) 5 street = TRIM( Request( “street” ) ) 6 city = TRIM( Request( “city” ) ) 7 state = TRIM( Request( “state” ) ) 8 zip = TRIM( Request( “zip” ) ) 9 cctype = Request( “cctype” ) 10 ccnumber = TRIM( Request( “ccnumber” ) ) 11 ccexpires = TRIM( Request( “ccexpires” ) ) 12 ccname = TRIM( Request( “ccname” ) ) 12.1 html = TRIM( Request ( “html” ) ) 13 14 submitpage = Request.ServerVariables( “SCRIPT_NAME” ) 15 %> … 70 <font face=”Courier” size=”2”> 71 <br><b>username:</b> 72 <input name=”newusername” size=20 maxlength=20 73 value=”<%=Server.HTMLEncode( newusername )%>”> 74 <br><b>password:</b> 75 <input name=”newpassword” size=20 maxlength=20 76 value=”<%=server.HTMLEncode( newpassword )%>”> 77 <br><b>email address:</b> 78 <input name=”email” size=30 maxlength=75 79 value=”<%=Server.HTMLEncode( email )%>”> 79.1 <br><input name=”html” type=”checkbox” value=”Yes” <% if ➥ Server.HTMLEncode( html ) = “Yes” then %>CHECKED<% end if %>> 79.2 <b>I can read E-Mail formatted in HTML.</b> 80 </font> storeFuncs.asp 115 SUB addUser 116 ‘ Get Registration Fields 117 newusername = TRIM( Request( “newusername” ) ) 118 newpassword = TRIM( Request( “newpassword” ) ) 119 email = TRIM( Request( “email” ) ) 120 street = TRIM( Request( “street” ) ) 121 city = TRIM( Request( “city” ) ) 122 state = TRIM( Request( “state” ) ) 123 zip = TRIM( Request( “zip” ) ) 124 cctype = Request( “cctype” ) 125 ccnumber = TRIM( Request( “ccnumber” ) ) 126 ccexpires = TRIM( Request( “ccexpires” ) ) 424 Day 18 INPUT 24 0672318989 ch18 3/30/00 8:17 AM Page 424 Using Email from Active Server Pages 425 18 127 ccname = TRIM( Request( “ccname” ) ) 127.1 if html <> “Yes” then 127.2 html = “1” 127.3 else 127.4 html = “0” 127.5 end if … 179 sqlString = “INSERT INTO users ( “ &_ 180 “user_username, “ &_ 181 “user_password, “ &_ 182 “user_email,” &_ 183 “user_street, “ &_ 184 “user_city,” &_ 185 “user_state,” &_ 186 “user_zip,” &_ 187 “user_ccnumber, “ &_ 188 “user_cctype, “ &_ 189 “user_ccexpires,” &_ 190 “user_ccname,” &_ 190.1 “user_HTML” &_ 191 “) VALUES ( “ &_ 192 “ ‘“ & fixQuotes( newusername ) & “‘, “ &_ 193 “ ‘“ & fixQuotes( newpassword ) & “‘, “ &_ 194 “ ‘“ & fixQuotes( email ) & “‘, “ &_ 195 “ ‘“ & fixQuotes( street ) & “‘, “ &_ 196 “ ‘“ & fixQuotes( city ) & “‘, “ &_ 197 “ ‘“ & fixQuotes( state ) & “‘, “ &_ 198 “ ‘“ & fixQuotes( zip ) & “‘, “ &_ 199 “ ‘“ & fixQuotes( ccnumber ) & “‘, “ &_ 200 “ ‘“ & cctype & “‘, “ &_ 201 “ ‘“ & ccexpires & “‘, “ &_ 202 “ ‘“ & fixQuotes( ccname ) & “‘, “ &_ 202.1 “ “ & html & “ “ &_ 203 “)” Lines 79.1 and 79.2 of register.asp add a check box to the registration form asking the new customer whether he can read HTML mail. When the customer clicks submit, and if his entries pass the validation in the addUser function, lines 127.1–127.5 of storeFuncs.asp set the value of the variable html to something that can be inserted into an SQL database. Lines 190.1 and 202.1 of storeFuncs.asp insert that value into the database along with other information about the new user. If the customer’s entries do not pass the validation in the addUser function, addUser dis- plays an error page that allows the customer to return to the registration form. If this occurs, lines 12.1 and 79.1 of register.asp make sure that the value of the new check box is reset to the value originally set by the user. ANALYSIS 24 0672318989 ch18 3/30/00 8:17 AM Page 425 Now that we know whether each user can receive HTML-formatted email, we can change the code that sends welcoming email to send formatted email to appropriate users. Listing 18.7 demonstrates a new sendNewUserMail function that sends messages in HTML format to new users who check the I Can Read HTML box on the registration form. A sample HTML-formatted message is illustrated in Figure 18.11. LISTING 18.7 A sendNewUserMail Function That Sends HTML 1 SUB sendNewUserMail(sUserName, sUserMail, fHtml) 2 Dim NewMailObj 3 Dim sMailBody 4 5 Set NewMailObj = CreateObject(“CDONTS.Newmail”) 6 NewMailObj.From = “customer-service@JohnsonGifts.com” 7 NewMailObj.To = sUserMail 8 NewMailObj.Subject = “Welcome to Johnson Candy and Gifts” 9 10 if fHtml = “0” then 11 NewMailObj.BodyFormat = CdoBodyFormatText 12 NewMailObj.MailFormat = CdoMailFormatMime 13 sMailBody = “Dear “ & sUserName & “,” & vbNewLine & vbNewLine 14 sMailBody = sMailBody & “ Thank you for registering at our site!” & ➥ vbNewLine & vbNewLine 15 sMailBody = sMailBody & “ We look forward to serving you in the ➥ future. “ 16 sMailBody = sMailBody & “Visit us again soon at ➥ http://www.johnsongifts.com.” & vbNewLine & vbNewLine 17 sMailBody = sMailBody & “Sincerely yours,” & vbNewLine & vbNewLine 18 sMailBody = sMailBody & “David Johnson,” & vbNewLine 19 sMailBody = sMailBody & “CEO, Johnson Candy and Gifts.” 20 NewMailObj.Body = sMailBody 21 else 22 NewMailObj.BodyFormat = CdoBodyFormatHTML 23 NewMailObj.MailFormat = CdoMailFormatMime 24 NewMailObj.ContentBase = “http://www.superexpert.com/” 25 NewMailObj.ContentLocation = “candystore/” 26 sMailBody = “<HTML><HEAD><TITLE>Thanks from Johnson’s Candy and ➥ Gifts</TITLE></HEAD>” 27 sMailBody = sMailBody & “<BODY><table width=””640”” border=””0”” ➥ bgcolor=””#ffffff”” cellspacing=””0”” cellpadding=””0””>” 28 sMailBody = sMailBody & “<tr><td><img src=””http://www.superexpert.com/ ➥ candystore/logo.gif”” WIDTH=””300”” HEIGHT=””30””></td></tr>” 29 sMailBody = sMailBody & “<tr><td colspan=””2””><hr width=””640””></td> ➥ </tr></table>” 30 sMailBody = sMailBody & “<font face=””Arial”” size=””2””><p>Dear “ ➥ & sUserName & “, “ 31 sMailBody = sMailBody & “<p>Thank you for registering at our site! ➥ <p>We look forward to serving you in the future. “ 426 Day 18 INPUT 24 0672318989 ch18 3/30/00 8:17 AM Page 426 Using Email from Active Server Pages 427 18 32 sMailBody = sMailBody & “Visit us again soon at ➥ <a href=””http://www.superexpert.com/candystore””>” 33 sMailBody = sMailBody & “http://www.johnsongifts.com</a>.<br> ➥ <br>Sincerely yours,<br><br>David Johnson” 34 sMailBody = sMailBody & “<br>CEO, Johnson Candy and Gifts</font> ➥ </BODY></HTML>” 35 NewMailObj.Body = sMailBody 36 end if 37 NewMailObj.Send 38 Set NewMailObj = Nothing 39 END SUB Line 22 sets the format of the message body to HTML. Lines 24 and 25 set the ContentBase and ContentLocation properties, which provide a default root URL and directory for images and other embedded objects. Lines 26–34 actually set the contents of the body to essentially the same contents as the text version of the message, but with HTML formatting that includes tables (lines 27–29), embedded images (line 28), and links (line 33). The mail is personalized for the recipient in line 30. ANALYSIS Not all mail readers support the ContentBase and ContentLocation proper- ties. To be safe, fully qualify all the references to images and other embed- ded objects in your HTML-formatted email by using an absolute address rather than a relative address. Note FIGURE 18.11 A new user email in HTML format. 24 0672318989 ch18 3/30/00 8:17 AM Page 427 [...]... “White”> 78 79 Select all customers 80 81 82 83 84 ” id=”submit1” name=”submit1”> 85 86 87 93 431 18 432 Day 18 ANALYSIS Lines 8 11... sent! Lines 8 14 create objects that are used in the loop in lines 16–32 That loop actually does the work of selecting the customer’s name into a Recordset given his email address (lines 17– 18) , creating a message for the user (line 20), addressing (line 22) and personalizing (line 26) the message, and then sending it (line 29) Line 34–36 closes the ADO connection Closing ADO connections... in the way of formatting How can I change these pages to have a better interface? A You can use the techniques described in Day 17 for uploading pictures with the Posting Acceptor to upload HTML files created in FrontPage, HomeSite, or Word Start by changing the tag in composeMsg .asp to the tag we used in Day 17, having the in composeMsg .asp submit to the Posting... that contains each customer’s name and email address Lines 53 85 define a form named custlist that is used to select the customers to whom the message will be sent Within that form, lines 55–76 define a table that is used to view each user Lines 56–60 define the header of that table Lines 64–74 loop through the customers in the database table and create a row in the table for each Line 68 defines a table... to find the email addresses and usernames of each customer once the form is submitted INPUT 1 2 3 4 5 6 7 8 9 LISTING 18. 8 The selectCust .asp Page That Allows Selection of Customers . action=”composeMsg .asp > 430 Day 18 LISTING 18. 8 continued 24 06723 189 89 ch 18 3/30/00 8: 17 AM Page 430 Using Email from Active Server Pages 431 18 54 55 <TABLE cellpadding=”2” cellspacing=”0” bordercolor=”#cccccc”. listings in this lesson. The passed -in mail address is used to set the destination address of the email in line 7, and the passed -in name is used to personalize the email in line 11. Finally, line. registering at our site! ➥ <p>We look forward to serving you in the future. “ 426 Day 18 INPUT 24 06723 189 89 ch 18 3/30/00 8: 17 AM Page 426 Using Email from Active Server Pages 427 18 32 sMailBody

Ngày đăng: 13/08/2014, 08:21