Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Bringing the Web to Your In-Box

Use this technique to send an entire Web page to your friend (or yourself)—text, HTML formatting, graphics, and all.


ave you visited sites that allow you to "E-mail this page to your friend"? Have you tried it and sent the page to yourself to see what it looks like? Usually, you get a text e-mail providing you with the URL to visit to view the Web page. If you use this new technique, however, you can actually send the entire Web page to your friend (or yourself)—text, HTML formatting, graphics and all!

In earlier 10-Minute Solutions, you have seen how we can use the CDO for NTServer library to send e-mail from your ASP page (you can refer to those solutions here). Windows 2000 now introduces an updated version of CDO available in CDOSYS.DLL. This little nugget vastly improves the e-mail sending capabilities of CDO. One of the most interesting features of this CDOSYS is that it actually grabs the contents of an entire URL, packages it as an e-mail (complete with formatting, graphics and all), and then sends it as e-mail.

We will be using this feature to create an "E-mail this page to your friend" functionality that you can then adapt to your Web page. Here's a sample of what our interface will look like:

As you can see, this interface asks for your e-mail address in the "From" field, your friend's e-mail address in the "To" field and any valid URL as the content for the e-mail. To make this example interesting, I have not hard coded the value of the URL (as you would on your Web site), but I have left it open for you to play with. Choose any URL on the Web (be as specific as you can with the URL page name) and have it sent to you via e-mail complete with graphics and text—all by using an ASP page.

Obviously, you will need a Windows 2000 server to be able to utilize this technique. First, on the Windows 2000 server, make sure you have SMTP service up and running, set up according to the previous 10-Minute Solutions referred to above. Then, create an ASP page that will display a form to accept information: we are using three text boxes to ask for values for the "To," "From," and "URL" fields. The code to accomplish this can be found here. As you can see, we obtain a page that looks like this.

If you look at the code, you will notice that the form calls itself when submitted. The ACTION attribute of the form has been set to "Request.ServerVariables("SCRIPT_NAME")"; that is, the name of the page itself. This technique is useful when you do not want to bother hard coding the name of the page. No matter what the page is called, this code will always resolve to the current page's name—almost like referring to the "Me" object in Visual Basic to refer to a form without having to know the name of the form.

In addition, there is a hidden variable, "PASS," which has been hard-coded to a value of 1. As you know from my style of programming, I use this variable's value to determine if we are coming back after the user has entered values in the form or not. Within the ASP page, at the very top, we examine the value of this variable and then branch to a separate subroutine to handle the e-mailing if we are returning back from a FORM submission.

	Dim mintPass, mstrMsg
	mintPass = Request("pass")
	Select Case Int(mintPass)
		Case 1
	End Select

"HandleRepeatVisit" is my standard subroutine that I branch off to when I return from a form's submission. All the magic happens inside this subroutine.

This routine is relatively simple. It is split into three parts: obtain data from the form, validate it, and then send the e-mail. First, we obtain the data from the form using the Request.Form collection:

' -- Gather information from the Form
	strTo = Request.Form("emlT")
	strFrom = Request.Form("emlF")
	' -- Hardcoded subject value
	strSubject = "Send HTML Formatted Email Example"
	strURL = Request.form("emlB")

We then validate the data to make sure we have proper values. I have used a simple validation, but you can make it more robust if you want. All I do is check to make sure that the fields were not left empty.

' -- Valid Data?
	If strTo = "" or strFrom = "" or strURL = "" Then
		exit sub
	End If

Finally, we are ready to send the e-mail. We first create a CDO.Message object:

' -- Create a CDO Message Object
	Set iMsg = Server.CreateObject("CDO.Message")
Once we have the Message object, we can set its properties and invoke its methods. We first set the "To," "From," and "Subject" properties:

' -- Set variable values
        	.To = strTo
        	.From = strFrom
        	.Subject = strSubject
And then we start the magic!

Explaining the Magic!
The new CDOSYS has the capability to handle HTML formatted messages. You can supply HTML content or point the message to a remote URL.

For example, if you want to pull the content of your Web page, available as http://yourWebpage.com, you would use the CreateMHTMLBody method to obtain the entire contents of this Web page. You would then package the contents as a MIME format e-mail by using the following code:

Message.CreateMHTMLBody http://yourWebpage.com, 0

The CreateMHTMLBody method converts the contents of an entire Web page into a MIME Encapsulation of Aggregate HTML Documents (MHTML) formatted message body. The syntax of the CreateMHTMLBody method is:

CreateMHTMLBody(URL as String, [Flags as 
	CdoMHTMLFlags = 0], [UserName as String], [Password 
	as String])
If a Web page requires you to log in with a user name and password, you can supply those to the CreateMHTMLBody method. If you wish to only obtain the text and not the graphics, or vice versa, you can manipulate the "Flags" argument.

In our example, we are accessing a public URL and don't need a user name and a password. Also, since we are obtaining all the content, our "Flags" value is equal to the CDO constant cdoSuppressNone (0).

' -- contents of the remote URL
        	.CreateMHTMLBody strURL, 0

Finally, we are ready to send the message. We simply use the 'Send' method to do so:

' -- finally, send the message
And viola! If your e-mail client is capable of viewing HTML formatted e-mail (and I think most of them are), you should be able to view the entire HTML page, with graphics and all, when you view the e-mail. Take a look at this in action at http://www15.brinkster.com/theasppro/10MinCdoSys.asp. You can also download the entire code for the page at the same location.

What Could Go Wrong, and How to Fix It
If you download the code and copy it to your Web server, it may or may not work. If it works, congratulations. If it doesn't, here are some points to remember:

  • Remember, you need Windows 2000 to utilize the CDOSYS DLL.
  • Remember to configure SMTP properly before trying this example. Use the guidelines in the previous 10-Minute Solutions.
  • Make sure you are entering valid e-mail addresses for From and To fields.
  • Make sure your URL is a specific page. Most Web sites immediately redirect the user to a secondary URL from the main URL. In that case, you will not get anything in the e-mail since the e-mail will not perform the redirect. Test the URL in your regular browser, and when the page you want is finally up and ready, copy the address for the page from the browser and use that.

I hope you appreciate the value of this solution. Not only can you use this technique to send visually appealing e-mail and newsletters to your users (oh no—I've just made potential spammers out of all of you!), but you can also use this technique to enhance your Web pages with an "E-mail this page to a friend" functionality.

Rama Ramachandran is the Vice President of Technology with Imperium Solutions and is a Microsoft Certified Solution Developer and Site Builder. He has extensive experience with building database systems and has co-authored several books including "Professional Visual InterDev 6 Programming" and "Professional Data Access" (Wrox). Rama teaches Visual Basic and Web development at Fairfield University and University of Connecticut.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date