A free mailing list source program (iii)

Source: Internet
Author: User
Tags header include mail regular expression
Subscribe.asp
<%@ Language=javascript%>

<!--#include file = "include/setglobals.asp"-->
<!--#include file = "include/dbpath.asp"-->

<%
Output relevant META tags
Init ("Subscription");

Output common top of page
Header (' <a href= "work.asp" >Work</a>--> Subscription ', 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% ' > </td> ');
Out (' <td width= "60%" > ");

If the form has an email address, validate it
So, if it fails we can show the form to fix
var semail = "";
var bsubmitted = (Request.Form.Count > 0);

Has the form been submitted?
if (bsubmitted)
{
Get "email address" from the form ...
Semail = "" + Request.Form ("email");

Validate the email address and moan if it fails
if (! IsValidEmail (Semail))
{
Out (' Out (' <p><font color= "Red" >if for you disagree, please <a href= "contact.asp" >contact me</a> Directly.</font><p> ');
Pretend the form hasn ' t been sent yet
bsubmitted = false;
}
}

Show the form if not submitted yet
if (!bsubmitted)
{
Out (' If you\ ' re interested in hearing whenever a new article are posted, or a existing one is updated, type in your Emai L address below and hit <b>Subscribe!</b> ');
Out (' <p>whenever your want to stop receiving my emails, guess what? That\ ' s right, enter your e-mail address and hit <b>unsubscribe</b>
Out (' <p><i>your e-mail address would never sold to or otherwise used by any third party, just me.</i> ') ;

Here ' s the form tag. The action attribute is the name of
The file that'll be called with the answer-in this case
It ' s the same page. The method can is "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 ' neater and ' get ' is limited
In the amount of data this can be sent.
Out (' <center><form action= "subscribe.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 (' Email: ');
Out (' </td><td align=, left ' valign= ' top ' > ');
A simple text box. We ' ll reference it with the name ' name '
and show 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.
//
important! Using names that are commonly used by
Other web sites has a big advantage to the User-ie
Would drop down a list of previous answers, which they
Can usually pick from rather than type in. I'm about this.
Out (' <input type= "text" name= "email" size= "" value= "' + Semail + '" ></input> ');
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" name= "action" value= "Subscribe" ></input> <input type= "Submit" name= "action" Value= "unsubscribe" ></input> ");
Out (' </td></tr> ');

Out (' </table> ');
Out (' </form></center> ');
}
Else
{
var saction = "" + Request.Form ("action");

if (saction = = "Subscribe")
Addemail (Semail);
Else
Removeemail (Semail);

Out (' <p> ');
}

Out (' Did you want to the How ' I ' form adds and removes addresses to my database? All of the source code is just a click away! ');
Out (' <p><center><a href= "showsource.asp? Page=subscribe" ></a></center>");
Out (' <p>in <a href= "mailtolist.asp" >part 2</a>-how-I wrote a form to mail all I subscribers ... ') ;

Out (' </td> ');
Out (' <td width= ' 20% ' > </td> ');
}

// ============================================
Validate Email Address
// ============================================
function IsValidEmail (semail)
{
Regular expression courtesy of ed.courtenay@nationwideisp.net
I won ' t even pretend I ' ve read through this yet!

if (Semail.search (-\w+) | (/\w+) | ( \.\w+) | (\_\w+)) *\@[a-za-z0-9]+ ((\.| -) [a-za-z0-9]+) *\. [A-za-z] {2,5}/)!=-1)
return true;
Else
return false;
}


// ============================================
Add Email to database
// ============================================
function Addemail (semail)
{
Open the connection
Dbinitconnection ();

The if they are already subscribed
var sSQL = ' SELECT Email from mailinglist WHERE email= ' + Semail + ';

Dbgetrecords (sSQL);

if (!orecordset.eof)
{
Out (' Return
}

This section needs the more work-what should to be-is-that-an-email is
Sent to the "email address" and "only" added to the "database" when we
Get a reply. That way we know the ' address is valid and the recipient
Really wants to join the list. For now though, we'll add to the db now.
sSQL = ' INSERT into mailinglist (Email) VALUES ("' + Semail + '");

Oconnection.execute (sSQL);

Free the connection
Dbreleaseconnection ();

Out (Semail + ' has been successfully subscribed to my mailing list. ' );
Out (' <p>you'll now receive a email whenever I write new articles, or if I make a important update to any. ')

Email (' joined the shawthing mailing list ', Semail, ' You have successfully subscribed to the mailing list at shawthing. I f you didn\ ' t request this please reply to this email, or visit http://www.shawthing.com/subscribe.asp to unsubscribe.\n\n Thank you.\n\njames shaw\nhttp://www.shawthing.com/');
}


// ============================================
Remove email from database
// ============================================
function Removeemail (semail)
{
Open the connection
Dbinitconnection ();

The if they are already subscribed
var sSQL = ' SELECT Email from mailinglist WHERE email= ' + Semail + ';

Dbgetrecords (sSQL);

if (orecordset.eof)
{
Out (' Return
}

Delete from the database
sSQL = ' DELETE from mailinglist WHERE email= ' + Semail + ';

Oconnection.execute (sSQL);

Free the connection
Dbreleaseconnection ();

Out (Semail + ' has been successfully removed to my mailing list. ' );
Out (' <p>you have been sent a confirmation emails, but after that you won't receive any more emails. ')

Email (' removal from shawthing mailing list ', Semail, ' Your have been successfully removed from the mailing list at shawth Ing. If you are didn\ ' t request this please reply to this email, or visit http://www.shawthing.com/subscribe.asp to re-subscribe.\n \nthank you.\n\njames shaw\nhttp://www.shawthing.com/');
}


// ============================================
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 = ' DB@shawthing.com ';

omail.to = 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 the 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 a attempt at optimization we 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 a attempt at optimization we-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 this can fail if passed garbage, and hence
' Orecordset ' would already be ' closed '
Orecordset = Oconnection.execute (sSQL);
}

// ============================================
Tidies up after Dbgetrecords
// ============================================
function Dbreleaserecords ()
{
Important:this FUNCTION intentionally BLANK
As an attempt in optimization we now open/close
The recordset with the connection and not separately
So all code is moved to Dbreleaseconnection.

It is recommended which you still call this function as soon
As the recordset is finished with.

Note that it's assumed by the caller, it is legal
To call Dbreleaseconnection without calling the This function
}
%>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.