Visual Basic
Web Scripting
Online Tools


What's New


Code Downloads

Code Snippets

Message boards
Tool Box


Mailing List
Receive free code snippets and notices when this site is updated.


Creating a "Tell a Friend" feature in ASP

This is the ASP version of the article I wrote Creating a "Tell a Friend" feature for a web site using Python.

This Tell a Friend script lets a user recommend a page to their friends, It extents the python version by letting the user send the same message to more than one friend at a time.

You can see this script in action by clicking here.

The script sends email using the NewMail Object of the CDONTS Library.  The URL of the page that is being recommended is given in the query string like this:


If nothing is given in the URL parameter a default URL is used.

The script below (tellafriend.asp) is deliberately omits the page header and footer sections and is designed to be included in another page that will provide the interface.


  URL = Request.QueryString("URL")
  If Len(URL) = 0 Then URL = "" ' The default URL

   If Len(Request.Form("SendersEmail")) > 0 Then ' Time to send the emails
    Dim objMail,FriendEmail,I
    sBody = "The Page at " & URL & " has been recommend by " & Request.Form("SendersEmail") & vbCrLf & " -- Message -- " & vbcrlf & Request.Form("Message")

    ' Loop until no more email addresses are given.
    Do While True
      FriendEmail = Request.Form("FriendEmail" & I)
      If Len(FriendEmail) = 0 Then
        Exit Do
        Set objMail = CreateObject("CDONTS.NewMail")
        objMail.From = ""
        objMail.Subject = "Recommended Page"
        objMail.Body = sBody
        objMail.To = FriendEmail
      End If

  Set objMail = Nothing
  Response.write "<H1>Thank you for spreading the word</H1>"
  Response.write "<a href=""" & URL & """>Click here to return to " & URL & "</a>"

<form method=
"POST" action="tellafriend.asp?URL=<%= URL %>">
Recommended URL:</b> <%= URL %></p>
Your Email:</b> <input type="text" name="SendersEmail" size="25"></p>
Your friends emails.</b></p>

<p>1. <input type="text" name="FriendEmail0" size="29"><br>
<input type="text" name="FriendEmail1" size="29"><br>
<input type="text" name="FriendEmail2" size="29"><br>
<input type="text" name="FriendEmail3" size="29"><br>
<input type="text" name="FriendEmail4" size="29"><br>

<p><textarea rows=
"6" name="Message" cols="41"></textarea></p>
<p><input type=
"submit" value="Recommend Page"></p>

<% End If %>

This code simply goes into a loop and checks to see if there was a form element called "FriendEmail0" and if there is it sends an email to the address that this variable contains. The loop then starts again and the check is done on "FriendEmail1" and so on. If this variable is empty the loop is exited.

The code above makes it easy to add extra text boxes to allow more people to be contacted in one go.  e.g to allow up to six people to be contacted, add the html

6. <input type="text" name="FriendEmail5" size="29">

To add a user interface and navigation bars around tellafriend.asp, simple include <!--#INCLUDE file="tellafriend.asp"--> where desired.  This helps keep a good portion of the code out of the HTML making it easier to manage.

<HEAD><TITLE>Tell a Friend</TITLE></HEAD>
<!--#INCLUDE file="tellafriend.asp"-->
<a href="alink">Link1</a><a href="alink2">Link2</a>

Download code for tell a friend that uses CDO.

Download code for tell a friend that uses ASPMail.

Tell a friend about this article