When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs

















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Windows Systems Administrator
Jupitermedia
US-CT-Darien

Justtechjobs.com Post A Job | Post A Resume

Published: Wednesday, December 15, 1999

Sending Web Articles through Email


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.

- continued -

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:

Now, in any page that you want to provide a "Share this Article" feature, you simply need to add one line of HTML:

<A HREF="/scripts/ShareArticle.asp"> Share this Article with a Friend! </A>

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:

<FORM METHOD=POST ACTION="SendArticle.asp">
  <INPUT TYPE=HIDDEN NAME="URL"
     VALUE="<%=Request.ServerVariables("HTTP_REFERER")%>">
     
  Your Name: <INPUT TYPE=TEXT NAME=FromName>
  <BR>
  Your Email: <INPUT TYPE=TEXT NAME=FromEmail>
  <BR>
  What is the name of the person you are sending
  this to? <INPUT TYPE=TEXT NAME=ToName>
  <BR>
  What is their email address?
  <INPUT TYPE=TEXT NAME=ToEmail>
  <P>
  Your Message:
  <TEXTAREA COLS=40 ROWS=5 NAME=Message></TEXTAREA>
  <BR>
  <INPUT TYPE=SUBMIT VALUE="Send the Article!"
</FORM>

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.

Const forReading = 1
Dim objFSO, objOpenFile, strBody, iStart strLeft, strRight

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Get the refered URL
strLeft = Request("URL")

'Hack off the "http://"
strRight = Right(strLeft,Len(strLeft)-7)

'Find the first forward slash
iStart = instr(1,strRight,"/")

'Grab everything to the right of the forward slash
strRight = Right(strRight,Len(strRight)-iStart+1)

'Open the text file
Set objOpenFile = objFSO.OpenTextFile(Server.MapPath(strRight), _
                                      ForReading)

'Read the contents of the text file into a string variable
strBody = objOpenFile.ReadAll

'We've got the contents of the file, so we can
'close the file and clean up
objOpenFile.Close
Set objOpenFile = Nothing

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.

iStart = 1
iStop = 1
	
Do Until ((iStart = 0) or (iStop = 0))
  iStart = instr(1,strBody,"<")
  iStop = instr(1,strBody,">")
  iLength = Len(strBody)
  if Not ((iStart = 0) or (iStop = 0)) then
     strLeft = Left(strBody,iStart-1)
     strRight = Right(strBody,iLength-iStop)
     strBody = strLeft & strRight
  end if
Loop

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!

strBody = strNameFrom & " recommended the following at " & _
          FormatDateTime(Now) & vbCrLf & _
          String(50,"%") & vbCrLf & vbCrLf & strBody & vbCrLf & _
          vbCrLf & String(50,"%") & vbCrLf & _
          vbCrLf
	
Dim objCDO
Set objCDO = Server.CreateObject("CDONTS.NewMail")
	
objCDO.To = strTo
objCDO.From = strFrom
	
objCDO.Subject = "Read this Article!"
objCDO.Body = strBody
	
objCDO.Send

Set objCDO = Nothing

'At this point, you may want to print a thank you message, or
'redirect the user to some web page...

Happy Programming!


Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES