Tell a Friend script help

Last post 05-17-2008 12:01 AM by mbanavige. 2 replies.

Sort Posts:

  • Tell a Friend script help

    05-14-2008, 11:59 AM

    Hi, first off, I am am total NOOB with .ASP! Embarrassed

    I downloaded a script, which is a basic "tell a friend" form that allows a user to send a plain text email to a recipeint. What I would like to do is make the received email look better by adding an image, or better yet, sending an html email with images,links and that sort of thing.

    I have tried all sorts of thing and nothing works.

    Here is the script:

     

     <%
    %>

    <H3>Tell a Friend:</H3>
    <%
    Dim objCDO                    ' Email object
    Dim strFromName               ' From persons' real name
    Dim strFromEmail, strToEmail  ' Email addresses
    Dim strSubject, strBody       ' Message
    Dim strThisPage               ' This page's URL
    Dim strReferringPage          ' The referring page's URL
    Dim bValidInput               ' A boolean indicating valid parameters

    ' Retrieve this page name and referring page name
    strThisPage      = Request.ServerVariables("SCRIPT_NAME")
    strReferringPage = Request.ServerVariables("HTTP_REFERER")

    ' Debugging lines:
    'Response.Write strThisPage & "<BR>" & vbCrLf
    'Response.Write strReferringPage & "<BR>" & vbCrLf

    ' Read in and set the initial values of our message parameters
    strFromName  = Trim(Request.Form("txtFromName"))
    strFromEmail = Trim(Request.Form("txtFromEmail"))
    strToEmail   = Trim(Request.Form("txtToEmail"))
    strSubject   = "Hey, check out RoboSync Sportsman!"
    strBody      = Trim(Request.Form("txtMessage"))

    ' I set the body message to a message that referenced the page the
    ' user arrived from.  This makes it great if you place a link to it
    ' from your different articles, but can be weird if people link in
    ' from other web sites.
    If strBody = "" Then
     If strReferringPage = "" Or InStr(1, strReferringPage, "
    www.google.com", 1) = 0 Then
      strBody = ""
      strBody = strBody & "I found a site I thought you'd like to see:" & vbCrLf
      strBody = strBody & vbCrLf
      strBody = strBody & "  
    http://www.google.com" & vbCrLf
     Else
      strBody = ""
      strBody = strBody & "I found an article I thought you'd like to see:" & vbCrLf
      strBody = strBody & vbCrLf
      strBody = strBody & "   " & strReferringPage & vbCrLf
     End If
    End If
     
    ' Quick validation just to make sure our parameters are somewhat valid
    bValidInput = True
    bValidInput = bValidInput And strFromName <> ""
    bValidInput = bValidInput And IsValidEmail(strFromEmail)
    bValidInput = bValidInput And IsValidEmail(strToEmail)

    ' If valid send email and show thanks, o/w show form
    If bValidInput Then
     ' Set up our email object and send the message
     Set objCDO = Server.CreateObject("CDO.Message")
     objCDO.From     = strFromName & " <" & strFromEmail & ">"
     objCDO.To       = strToEmail
     objCDO.Subject  = strSubject
     objCDO.TextBody = strBody
     objCDO.Send
     Set objCDO = Nothing

     ' Show our thank you message
     ShowThanksMsg
    Else
     If "http://" & Request.ServerVariables("HTTP_HOST") & strThisPage = strReferringPage Then
      Response.Write "There's been an error.  Please check your entries:" & "<BR>" & vbCrLf
     End If
     ' Show our information retrieval form
     ShowReferralForm strThisPage, strFromName, strFromEmail, strToEmail, strBody
    End If
    ' End of page logic... subs and functions follow!
    %>


    <%
    ' Subroutines and Functions that encapsulate some functionality
    ' and make the above code easier to write... and read.

    ' A quick email syntax checker.  It's not perfect,
    ' but it's quick and easy and will catch most of
    ' the bad addresses than people type in.
    Function IsValidEmail(strEmail)
     Dim bIsValid
     bIsValid = True
     
     If Len(strEmail) < 5 Then
      bIsValid = False
     Else
      If Instr(1, strEmail, " ") <> 0 Then
       bIsValid = False
      Else
       If InStr(1, strEmail, "@", 1) < 2 Then
        bIsValid = False
       Else
        If InStrRev(strEmail, ".") < InStr(1, strEmail, "@", 1) + 2 Then
         bIsValid = False
        End If
       End If
      End If
     End If

     IsValidEmail = bIsValid
    End Function

    ' I made this a function just to get it out of the
    ' logic and make it easier to read.  It just shows the
    ' form that asks for the input
    Sub ShowReferralForm(strPageName, strFromName, strFromEmail, strToEmail, strBody)
     ' I use script_name so users can rename this script witout having to change the code.
     %>
     <FORM ACTION="<%= strPageName %>" METHOD="post" name=frmReferral>
     <TABLE BORDER="0">
     <TR>
      <TD VALIGN="top" ALIGN="right"><STRONG>Your Name:</STRONG></TD>
      <TD><INPUT TYPE="text" NAME="txtFromName" VALUE="<%= strFromName %>" SIZE="30"></TD>
     </TR>
     <TR>
      <TD VALIGN="top" ALIGN="right"><STRONG>Your E-mail:</STRONG></TD>
      <TD><INPUT TYPE="text" NAME="txtFromEmail" VALUE="<%= strFromEmail %>" SIZE="30"></TD>
     </TR>
     <TR>
      <TD VALIGN="top" ALIGN="right"><STRONG>Friend's E-mail:</STRONG></TD>
      <TD><INPUT TYPE="text" NAME="txtToEmail" VALUE="<%= strToEmail %>" SIZE="30"></TD>
     </TR>
     <TR>
      <TD VALIGN="top" ALIGN="right"><STRONG>Message:</STRONG></TD>
      <TD><TEXTAREA NAME="txtMessage" COLS="50" ROWS="5" WRAP="virtual" READONLY><%= strBody %></TEXTAREA>
     </TR>
     <TR>
      <TD></TD>
      <TD><INPUT TYPE="reset" VALUE="Reset Form" name=rstReferral>&nbsp;&nbsp;<INPUT TYPE="submit" VALUE="Send E-mail" name=subReferral></TD>
     </TR>
     </TABLE>
     </FORM>
     <%
     '<P>The Message to be sent:</P>
     '<P><B>Subject:</B> < %= strSubject % ></P>
     '<P><B>Body:</B> < %= strBody % ></P>
    End Sub

    ' This just shows our thank you message... probably didn't need to
    ' be a function, but since I made the form one I figured I'd do this
    ' for consistency.
    Sub ShowThanksMsg()
     %>
     <P>Your message has been sent.  Thanks for helping us spread the word about ASP 101!</P>
     <%
    End Sub
    %>

     

    Any advice would be most appreciated!

    Regards,

    ~Messer

  • Re: Tell a Friend script help

    05-14-2008, 6:04 PM
    • Loading...
    • Bruce L
    • Joined on 02-08-2007, 6:53 PM
    • Posts 782

    Change this line

    objCDO.TextBody = strBody

    to

    objCDO.HTMLBody = strBody

    You'll need to construct the HTML body though.

    PS.  I really don't recommend you put up this script as is because there are many spammer who will use your "Tell a friend" page to send spam.  If you really want to have a tell a friend page, I recommend you put some capchat validation.  See http://en.wikipedia.org/wiki/Captcha

    Bruce
    DiscountASP.NET: Developer Ready ASP.NET Web Hosting
    - Microsoft Gold Certified Partner
    - Voted 2008, 2007, 2006 & 2005 Best ASP.NET Web Hosting by asp.netPRO Magazine
  • Re: Tell a Friend script help

    05-17-2008, 12:01 AM
    • Loading...
    • mbanavige
    • Joined on 11-06-2003, 8:29 AM
    • New England, USA
    • Posts 7,360
    • Moderator
      TrustedFriends-MVPs

    These forums are for ASP.NET, not classic ASP.  You could try your question on the Classic ASP Forum at Forums.IIS.NET, or the ASP Messageboard, Microsoft's ASP newsgroup (microsoft.public.inetserver.asp.general), or another community which deals with Classic ASP.

    Note that the membership is shared between ASP.NET and IIS.net, so you can use the same login credentials across both sites.

    Mike Banavige
    ~~~~~~~~~~~~
    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (3 items)