It seems like every Web site has a "Email this Article to a Friend" option. To enable this option, you can have the article "shared" in one of two ways. First, you can simply send the URL of the article. The second way is to send the text of the article. This article will examine how to accomplish the latter method.
Our entire system will consist of three ASP pages. The first ASP page will be any article
on our Web site. The second ASP page, ShareArticle.asp
, will collect information from the user, such as their name and email address, the name and email address
of the friend they want to send the article to, and a place to enter a descriptive message.
The user will be sent to ShareArticle.asp
when they click on a link from one
of the articles. ShareArticle.asp
will use the HTTP_REFERER
variable of the Request.ServerVariables
collection to
determine the URL of the article the user would like to share.
Our third and final ASP page, SendArticle.asp
, will be called when the Form on
ShareArticle
is submitted. SendArticle.asp
will use CDONTS to
send an email. Also, the FileSystemObject
will be used to open the HTML file
corresponding to the URL from ShareArticle.asp
. This HTML file will be read
into a string variable and have its HTML tags parsed out. Once we have this completed, we
can send the HTML tag-less article via CDONTS.
For more information on some of these related topics, be sure to check out the following articles:
- Sending Emails in ASP Using CDONTS
- Accessing the HTTP Headers Using
Request.ServerVariables
- Four Ways of Passing Information from One ASP Page to Another
Now, in any page that you want to provide a "Share this Article" feature, you simply need to add one line of HTML:
|
Place the above HTML in any web page that you want to allow your visitors share with a
friend. Now, let's look at the source code for ShareArticle.asp
. This ASP
page should do nothing more than present the user with a Form into which they can enter
their name and email address, and the name and email address of the person they are sharing
the article with. Also, the user can enter a paragraph or so of description. This page
also must pass the URL of the referring page to SendArticle.asp
:
|
Now all that we have left to code is SendArticle.asp
, which will do the
actual work of stripping the HTML tags from the requested article, and sending that
HTML-stripped article to the person specified in the Form in ShareArticle.asp
.
First, we need to use the FileSystemObject
to grab the HTML file on the web
server that the user requested. Problem is, the HTTP_REFERER
HTTP Header
returns an actual URL, whereas we need a physical path. No worries, though, we can use
Server.MapPath
to translate the second half of the URL into a physical path. That is, if Request.ServerVariables("HTTP_REFERER")
returns http://www.4GuysFromRolla.com/webtech/120199-1.shtml
, we want to feed
/webtech/120199-1.shtml
into Server.MapPath
, which will return the
mapped, physical path.
|
Pretty straight-forward. Now, we need to hack all of the HTML tags out of the variable
strBody
. We can do this by searching for any less than signs, and clipping
the text between the less than and next greater than sign. Here is the code, I'll let you
analyze it to see how it works, exactly.
|
Now all we need to do is create the CDONTS instance and send this puppy off! Here is the code that will accomplish just that!
|
Happy Programming!