When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
Technology Jobs
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.
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!

Software Developer / Programmer - Distributed Systems (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume


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