MailToList.asp
1@ Language=JavaScript
1
2// output relevant meta tags
3Init( "Mail to list" );
4
5// output common top of page
6Header( '
<a href="work.asp">Work</a>
1\--> Mail to list', 3 );
2
3// output page content
4Content ( );
5
6// output common bottom of page
7Footer( );
1 /* standard page elements */
1
2// ============================================
3// the content of this page
4// ============================================
5function Content ( )
6{
7Out ( '
<td width="20%"> </td>
1' );
2Out ( '
<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 name="password" size="30" type="password"/>' );
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 cols="30" name="message" rows="8" wrap="physical"></textarea>' );
Out ( '</td></tr>' );
Out ( '<tr><td align="right" valign="top">' );
Out ( ' ' );
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"/>' );
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 border="0" src="images/source.gif"/></a></center>' );
Out ( '</p></p></p></p></td>
1' );
2Out ( '
<td width="20%"> </td>
1' );
2}
3
4// ============================================
5// email me!
6// ============================================
7function Email ( sSubject, sEmail, sMessage )
8{
9// send an email to the address just to confirm what just happened
10var oMail = Server.CreateObject ( "CDONTS.NewMail" );
11
12// setup the mail
13oMail.From = oMail.To = '[email protected]';
14
15oMail.Bcc = sEmail;
16oMail.Importance = 1;
17
18oMail.Subject = sSubject;
19oMail.Body = sMessage;
20
21// send it
22oMail.Send ( );
23
24// release object
25oMail = null;
26}
utils/Database.asp
1
2// globals
3var oConnection;
4var oRecordSet;
5var sConnection;
6
7// ============================================
8// example usage:
9// DBInitConnection ( );
10//
11// var sSQL = "SELECT * FROM Somewhere";
12//
13// DBGetRecords ( sSQL );
14//
15// ...use oRecordSet
16//
17// DBReleaseRecords ( ); // optional step
18//
19// DBReleaseConnection ( );
20// ============================================
21
22// ============================================
23// initializes database variables for first use on page
24// ============================================
25function DBInitConnection ( )
26{
27// don't open it again if already opened!
28if ( sConnection != undefined )
29return;
30
31// get connection object
32oConnection = Server.CreateObject( 'ADODB.Connection' );
33
34// get the database connection string
35// use MapPath to make relative path into physical path
36sConnection = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + Server.MapPath ( sDBPath );
37
38// open the connection
39oConnection.Open( sConnection );
40
41// as an attempt at optimization we now open
42// the recordset here, not in DBGetRecords()
43oRecordSet = Server.CreateObject ( 'ADODB.Recordset' );
44}
45
46// ============================================
47// tidies up after DBInitConnection
48// ============================================
49function DBReleaseConnection ( )
50{
51// don't release the connection if not connected!
52if ( sConnection == undefined )
53return;
54
55// as an attempt at optimization we now close
56// the recordset here, not in DBReleaseRecords()
57if ( oRecordSet.State != 0 )
58oRecordSet.Close();
59oRecordSet = undefined;
60
61oConnection.Close();
62oConnection = undefined;
63
64sConnection = undefined;
65}
66
67// ============================================
68// executes the passed in SQL statement
69// and returns the oRecordSet object
70// ============================================
71function DBGetRecords ( sSQL )
72{
73// remember that this can fail if passed garbage, and hence
74// 'oRecordSet' will already be 'closed'
75oRecordSet = oConnection.Execute( sSQL );
76}
77
78// ============================================
79// tidies up after DBGetRecords
80// ============================================
81function DBReleaseRecords ( )
82{
83// IMPORTANT: THIS FUNCTION INTENTIONALLY BLANK
84// as an attempt at optimization we now open/close
85// the recordset with the connection, not separately
86// so all code was moved to DBReleaseConnection.
87
88// it is recommended that you still call this function as soon
89// as the recordset is finished with.
90
91// note that it is assumed by the caller that it is legal
92// to call DBReleaseConnection without calling this function
93}