一个免费的邮件列表源程序(一)
发布时间:2006-10-14 2:38:23   收集提供:gaoqian
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 width="20%">&nbsp;</td>' );
   Out ( '<td width="60%">' );
    
      // 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 border="0" cellpadding="0">' );
            Out ( '<tr><td align="right" valign="top">' );
               Out ( 'Password:' );
            Out ( '</td><td align="left" 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" name="password" size="30"></input>' );
            Out ( '</td></tr>' );

            Out ( '<tr><td align="right" valign="top">' );
               Out ( 'Message:' );
            Out ( '</td><td align="left" 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 name="message" cols="30" rows="8" wrap="physical"></textarea>' );
            Out ( '</td></tr>' );

            Out ( '<tr><td align="right" valign="top">' );
               Out ( '&nbsp;' );
            Out ( '</td><td align="left" 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="images/source.gif" border=0></a></center>' );

   Out ( '</td>' );
   Out ( '<td width="20%">&nbsp;</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
}
%>

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50