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
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Monday, January 24, 2000

Creating a Mailing List Program, Part 2

By Hunter Powers


Read Part 1


A screen shot of an admin page. In Part 1 we discussed how to create the DataStore.inc script, the AddUser.asp script, and the Default.asp script. In this part, we are going to discuss how to create administration pages for the mailing list program! Recall that we have the following three tasks to complete for our administration pages:

- continued -

  • List the current users
  • Delete users, and
  • E-mail our users

Let's start with the script to display the current users/subscribers to our mailing list. We shall call this file /admin/list.asp. It will list our current users and provide a link to delete any given user. To the right, you should see a screenshot of this page in action! Below is the code:

<% LANGUAGE="VBSCRIPT" %>
<% response.buffer = True%>
<!-- #INCLUDE FILE="../DataStore.inc" -->
<!-- #INCLUDE FILE="../adovbs.inc" -->
<!-- #INCLUDE FILE = "security.inc" -->
<%
'/admin/list.asp
'this page lists the current accounts
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Current Accounts</title>
</head>
<body>
<p align="center">Current Users</p>
<div align="center"><center>
<table border="2" cellpadding="2" cellspacing="0" bgcolor="#FFFFFF"
bordercolor="#000000" bordercolorlight="#000000" bordercolordark="#000000">
<tr>
<td bgcolor="#008080"><p align="center">
  <font color="#FFFFFF">E-Mail Address</font>
</td>
<td bgcolor="#008080">
  <font color="#FFFFFF"><p align="center">Delete User</font>
</td>
</tr>
<%
    'declare variables
    Dim objRec 'recordset object
    'create the recordset object
    Set objRec = Server.CreateObject ("ADODB.Recordset")
    'now open it
    objRec.open "EMailList", strConnect, adOpenKeyset, _
        adLockReadOnly, adCmdTable

    'loop once through the records
    While Not objRec.EOF
    
      'display the e-mail address in the table
      Response.Write("<tr><td><p align=""center""><a href=""mailto:")
      Response.Write(objRec("EMail"))
      Response.Write(""">")
      Response.Write(objRec("EMail"))

      'put a link to the delete script to delete the user
      Response.Write("</a></td><td>")
      Response.Write("<p align=""center""><a href=""delete.asp?id=")
      Response.Write(objRec("ID"))
      Response.Write("""><u><font color=""#FF0000"">X</font>")
      Response.Write("</u></a></td></tr>")
    
      'go to the next record
      objRec.MoveNext
    Wend

    'clean everything up
    objRec.Close
    Set objRec = Nothing
%>
</table>
</center></div>
</body>
</html>

Now that we have a script to show our users we will create a script to delete our users, /admin/delete.asp. This is linked to in the above script with the parameter ID passed to it with the given database id for the specified record. The code follows (the code is also available to download in zip format at the end of the article!):

<!-- #INCLUDE FILE = "../DataStore.inc" -->
<!-- #INCLUDE FILE = "../adovbs.inc" -->
<!-- #INCLUDE FILE = "security.inc" -->
<%
'/admin/delete.asp
'this page deletes accounts from the database
%>
<%
    'declare the variables
    DIM ID,Refer,objRec,varBookMark

    'grab the id variable from the querystring
    ID = Request.QueryString("id")
    'grab the users previous location
    Refer = Request.ServerVariables("HTTP_REFERER")
    'create the recordset object
    Set objRec = Server.CreateObject ("ADODB.Recordset")
    'now open it
    objRec.open "EmailList", strConnect, adOpenStatic, _
        adLockOptimistic, adCmdTable

    'keep our current place
    varBookmark = objRec.Bookmark

    'loop through the records
    While Not objRec.EOF
        'test to see if it is the correct record
        If (objRec("ID") = ID) THEN
            'if so, bookmark the record
            varBookmark = objRec.Bookmark
        End If
        'move to the next record
        objRec.MoveNext
    Wend

    'set the recordset to the correct record
    objRec.Bookmark = varBookmark

    'delete the record
    objRec.delete

    'clean everything up
    objRec.Close
    Set objRec = Nothing

    'redirect the user to where they came from
    Response.Redirect(Refer)
%>

We have now programmed two of our three desired administrative functions. That leaves one left. The most important. The mailing script, the script that actually sends the emails to all of the subscribers, will be named /admin/broadcast.asp. The purpose of this script will be to open up the database we created and e-mail all of the users. Now for an important note. This part of the script uses CDONTS. CDONTS is a standard part of ASP 2.0. Please make sure that CDONTS is enabled on your server. Alternatively if you are running some other third party mail component you should be able to change the code easily to work with your component. (Note that CDONTS works only on NT Server. There is a good article on 4Guys about using CDONTS: Sending Emails in ASP Using CDONTS. There is also an article discussing the other non-CDONTS email options using ASP: Sending Email without Using CDONTS.)

<!-- #INCLUDE FILE = "../DataStore.inc" -->
<!-- #INCLUDE FILE = "../adovbs.inc" -->
<!-- #INCLUDE FILE = "security.inc" -->
<%
'/admin/broadcast.asp
'the purpose of this page is to send out the message to the list
%>
<%
    'declare variables
    'Strings for recipient, subject, boby
    Dim strTo, strSubject, strBody, strFrom 
    Dim objCDOMail 'The CDO object
    Dim objRec

    'grab the variables from the form
    strFrom = Request.Form("From")
    strSubject = Request.Form("subject")
    strBody = Request.Form("body")
    Set objRec = Server.CreateObject ("ADODB.Recordset")
    'open the database
    objRec.Open "EmailList", strConnect, adOpenKeyset, _
    adLockReadOnly, adCmdTable

    'notify the user that the broadcast has started
    Response.Write("<h1>Broadcast Starting</h1>")

    'loop through the recordset
    While Not objRec.EOF
        'create a new cdo object
        Set objCDOMail = Server.CreateObject("CDONTS.NewMail")
        'set its properties
        objCDOMail.From = strFrom
        objCDOMail.To = objRec("EMail")
        objCDOMail.Subject = strSubject
        objCDOMail.Body = strBody
        'send the mail
        objCDOMail.Send
        'set the object = nothing
        Set objCDOMail = Nothing
        'confirm to the user that the mail has been sent
        Response.Write "Message sent to " & objRec("EMail") & "!<br>"
        'go to the next record
        objRec.MoveNext
    WEND
    'notify the user that the broadcast has completed
    Response.Write("<h1>Broadcast Comlete</h1>")
%>

We now need a page to post the e-mail from. We shall create one and call it /admin/message.asp. It is from this URL that you, as the administrator, will enter an email message into a Form. When this Form is submitted, /admin/broadcast.asp will be called, and all of the subscribers will receive an email.

<!-- #INCLUDE FILE = "security.inc" -->
<%
'/admin/message.asp
'this page provides an example of how to post to the broadcast.asp script
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Send A Message</title>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
</head>
<body>
<div align="center"><center>
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
  <tr>
    <td width="100%">
      <form method="POST" action="broadcast.asp">
       <div align="center"><center><p>From Email Address<br>
       <input type="text" name="From" size="22"></p>
       </center></div>
       <div align="center"><center><p>Subject<br>
       <input type="text" name="subject" size="45"></p>
       </center></div>
       <div align="center"><center><p>Body<br>
       <textarea rows="9" name="body" cols="46"></textarea></p>
       </center></div>
       <div align="center"><center><p>
       <input type="submit" value="Send Message" name="B1"></p>
</center></div>
</form>
</td>
</tr>
</table>
</center></div>
</body>
</html>

And now for one last page. We need a page to link to our two main admin pages from. We will call it /admin/default.asp.

<!-- #INCLUDE FILE = "security.inc" -->
<%
'an easy set of links to the two admin pages
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Main Menu </title>
</head>
<body>
<p><a href="list.asp">Click Here To View Current Users</a></p>
<p><a href="Message.asp">Click Here To Send A Message</a></p>
</body>
</html>

Ok, we have now finished putting together our ASP Mailing List Program. I hope you all had some fun and learned something new. All of the source code and the database are available for download below. If you have any improvements / questions / suggestions or anything else feel free to e-mail me at HunterPowers@WireThePlanet.Com.


Attachments:

  • Download the entire article, source code, an example database in ZIP format


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