一个免费的邮件列表源程序(一)

网络整理 - 07-26
MailToList.asp
<%@ Language=Javascript %>

<!--#include file = "include/SetGlobals.asp"-->
<!--#include file = "include/DBPath.asp"-->

<%
// output relevant meta tags
Init( "Mail to list" );

// output common top of page
Header( '<a href="work.asp">Work</a> --> Mail to list', 3 );

// output page content
Content ( );

// output common bottom of page
Footer( );
%>

<% /* standard page elements */ %>
<!--#include file = "utils/Init.asp"-->
<!--#include file = "utils/Database.asp"-->
<!--#include file = "utils/Header.asp"-->
<!--#include file = "utils/Footer.asp"-->

<%
// ============================================
// the content of this page
// ============================================
function Content ( )
{
   Out ( '<td> </td>' );
   Out ( '<td>' );
    
      // if the form has a password, validate it first
      // so that if it fails we can show the form again
      var bSubmitted = (Request.Form.Count > 0);

      // has the form been submitted?
      if ( bSubmitted )
      {
         // get the password from the form...
          sPassword = "" + Request.Form ( "password" );

         // validate the password and moan if it fails
         if ( sPassword != sDBPath )
         {
            Out ( '<h3><font color="red">Invalid password!</font></h3>' );
            // pretend the form hasn'\t been sent yet
            bSubmitted = false;
         }
      }

      // show the form if not submitted yet
      if ( !bSubmitted )
      {
         Out ( 'In <a href="Subscribe.asp">Part 1</a> I showed you how I allowed you to subscribe to my mailing list. Here\'s where I can post an email to members of that mailing list.' );
         Out ( '<p>Strangely, I\'m not going to let you do it, but you <i>can</i> get the source code from the bottom of the page, and learn how I did it.' );
         // here's the form tag. the action attribute is the name of
         // the file that will be called with the answer - in this case
         // it's the same page. the method can be "post" to send the
         // form data 'behind the scenes' or "get" to appending the
         // data to the URL in the style page.asp?data1=a&data2=b
         //
         // use post most of the time - it's neater and "get" is limited
         // in the amount of data that can be sent.
         Out ( '<form action="MailToList.asp" method="post">' );
    
            // another table to line up the titles and inputs
            Out ( '<table cellpadding="0">' );
            Out ( '<tr><td valign="top">' );
               Out ( 'Password:' );
            Out ( '</td><td valign="top">' );
               // a simple text box. we'll reference it with the name "password"
               // and show 37 characters on the form. use the maxlength
               // attribute to set the maximum characters they can enter.
               // use value="some text" to pre-fill the input with data.
               Out ( '<input type="password" size="30"></input>' );
            Out ( '</td></tr>' );

            Out ( '<tr><td valign="top">' );
               Out ( 'Message:' );
            Out ( '</td><td valign="top">' );
               // textarea is a multiline text box. specify the size with the
               // cols and rows attributes. wrap can be "off" (the default)
               // "physical" or "virtual". as an example, consider the user
               // typing in the following text in a 40 character wide input:
               //
               // "I wonder how this text will appear to the server when I send it?"
               //
               // wrap="off" will send it as typed, but the user has to scroll off
               // to the right to see the text. (Horrid)
               //
               // wrap="physical" will physically split the line after the word
               // 'server' and send two lines to the server
               //
               // wrap="virtual" will send one line, as typed, but the user
               // will see the text nicely wrap in the input. Perfect!
               Out ( '<textarea cols="30" rows="8" wrap="physical"></textarea>' );
            Out ( '</td></tr>' );

            Out ( '<tr><td valign="top">' );
               Out ( ' ' );
            Out ( '</td><td valign="top">' );
               // type='submit" provides a submit button to perform the
               // form action. the button says "Submit" unless you override
               // with the value attribute.
               Out ( '<input type="submit" value="Send Mail"></input>' );
            Out ( '</td></tr>' );

            Out ( '</table>' );

         Out ( '</form>' );
      }
      else
      {
         // get the message from the form
         var sMessage = "" + Request.Form ( "message" );

         // open the connection
         DBInitConnection ( );

         // get the emails addresses
         var sSQL = 'SELECT Email FROM MailingList;';

         DBGetRecords ( sSQL );

         var sEmailList = "";
         var sSep = "";

         while ( !oRecordSet.EOF )
         {
            sEmailList += sSep + oRecordSet ( 0 );

            sSep = ";";

            oRecordSet.MoveNext ( );
         }

         // free the connection
         DBReleaseConnection ( );

         Email ( 'It\'s a ShawThing - what\'s new?', sEmailList, sMessage );

         Out ( '<p>Email sent successfully.<p>' );
      }

      Out ( 'Want to see how this form to mail the subscribers was done? Click below to get all the source code!' );
      Out ( '<p><center><a href="ShowSource.asp? page=MailToList"><img src="http://www.newasp.net/Article/asp/example/2005/images/source.gif" border=0></a></center>' );

   Out ( '</td>' );
   Out ( '<td> </td>' );
}

// ============================================
// email me!
// ============================================
function Email ( sSubject, sEmail, sMessage )
{
   // send an email to the address just to confirm what just happened
   var oMail = Server.CreateObject ( "CDONTS.NewMail" );

   // setup the mail
   oMail.From = oMail.To = 'MailingList@shawthing.com';

   oMail.Bcc = sEmail;
   oMail.Importance = 1;

   oMail.Subject = sSubject;
   oMail.Body = sMessage;

   // send it
   oMail.Send ( );

   // release object
   oMail = null;
}
%>
     
utils/Database.asp
<%
// globals
var oConnection;
var oRecordSet;
var sConnection;

// ============================================
// example usage:
//      DBInitConnection ( );
//
//      var sSQL = "SELECT * FROM Somewhere";
//
//      DBGetRecords ( sSQL );
//
//      ...use oRecordSet
//
//      DBReleaseRecords ( );      // optional step
//
//      DBReleaseConnection ( );
// ============================================

// ============================================
// initializes database variables for first use on page
// ============================================
function DBInitConnection ( )
{
   // don't open it again if already opened!
   if ( sConnection != undefined )
      return;
       
   // get connection object
   oConnection = Server.CreateObject( 'ADODB.Connection' );

   // get the database connection string
   // use MapPath to make relative path into physical path
   sConnection = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + Server.MapPath ( sDBPath );

   // open the connection
   oConnection.Open( sConnection );

   // as an attempt at optimization we now open
   // the recordset here, not in DBGetRecords()
   oRecordSet = Server.CreateObject ( 'ADODB.Recordset' );
}

// ============================================
// tidies up after DBInitConnection
// ============================================
function DBReleaseConnection ( )
{
   // don't release the connection if not connected!
   if ( sConnection == undefined )
      return;
       
   // as an attempt at optimization we now close
   // the recordset here, not in DBReleaseRecords()
   if ( oRecordSet.State != 0 )
      oRecordSet.Close();
   oRecordSet = undefined;

   oConnection.Close();
   oConnection = undefined;
    
   sConnection = undefined;
}

// ============================================
// executes the passed in SQL statement
// and returns the oRecordSet object
// ============================================
function DBGetRecords ( sSQL )
{
   // remember that this can fail if passed garbage, and hence
   // 'oRecordSet' will already be 'closed'
   oRecordSet = oConnection.Execute( sSQL );
}

// ============================================
// tidies up after DBGetRecords
// ============================================
function DBReleaseRecords ( )
{
   // IMPORTANT: THIS FUNCTION INTENTIONALLY BLANK
   // as an attempt at optimization we now open/close
   // the recordset with the connection, not separately
   // so all code was moved to DBReleaseConnection.
    
   // it is recommended that you still call this function as soon
   // as the recordset is finished with.
    
   // note that it is assumed by the caller that it is legal
   // to call DBReleaseConnection without calling this function
}
%>