Visual Basic
Python
Web Scripting
Java
Online Tools


Home

What's New

Articles

Code Downloads

Code Snippets

Message boards
Links
Tool Box

Books

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

Contact

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:

mytellafriendpage.asp?URL=http://www.findacomponent.com/apage.asp

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.

tellafriend.asp

<%
  URL = Request.QueryString("URL")
  If Len(URL) = 0 Then URL = "http://www.yourbigglobalcompany.com" ' 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")
    I=0

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

  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>"
Else
%>

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

<p>1. <input type="text" name="FriendEmail0" size="29"><br>
2.
<input type="text" name="FriendEmail1" size="29"><br>
3.
<input type="text" name="FriendEmail2" size="29"><br>
4.
<input type="text" name="FriendEmail3" size="29"><br>
5.
<input type="text" name="FriendEmail4" size="29"><br>
</p>

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

<% 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.

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

Download code for tell a friend that uses CDO.

Download code for tell a friend that uses ASPMail.

Tell a friend about this article