<% Option Explicit %> <% 'Set the response buffer to true as we maybe redirecting Response.Buffer = True 'Declare variables Dim rsNewListMember 'Database recordset variable Dim rsRemoveListMember 'Database recordset variable Dim strEmailAddress 'Holds the users e-mail address Dim strEmailBody 'Holds the body of the e-mail Dim strAppendToEmail 'Holds the link to get removed from the mailing list Dim strSubject 'Holds the subject of the e-mail Dim strMessage 'Holds the error message if the user is not entered into the database Dim strUserCode 'Holds a unique code for the new list member Dim strMode 'Holds whether the user is subscribing or un-subscribing Dim blnError 'Set to true if the user is not enetered into the mailing list Dim blnSendWelcome 'Set to ture when the welcome e-mail is sent 'Initialise variables blnError = False 'Read in the form details strEmailAddress = LCase(Request.Form("email")) strMode = Request.Form("mode") 'Clean up the email address address getting rid of unwated characters strEmailAddress = characterStrip(strEmailAddress) 'Check to see if the user has entered an e-mail address and that it is a valid address If Len(strEmailAddress) < 5 OR NOT Instr(1, strEmailAddress, " ") = 0 OR InStr(1, strEmailAddress, "@", 1) < 2 OR InStrRev(strEmailAddress, ".") < InStr(1, strEmailAddress, "@", 1) Then 'Set an error message if the users has not enetered a valid e-mail address strMessage = strMessage & "You must enter a valid e-mail address" 'Set the error boolean to true blnError = True End If 'Slect the mode of the page whether we are subscribing or un-subscribing Select Case strMode 'If the user wants to subscribe then add them to the list Case "add" 'Create recordset object Set rsNewListMember = Server.CreateObject("ADODB.Recordset") 'Initalise the strSQL variable with an SQL statement to query the database strSQL = "SELECT tblMailingList.* FROM tblMailingList;" 'Set the cursor type property of the record set to Dynamic so we can navigate through the record set rsNewListMember.CursorType = 2 'Set the Lock Type for the records so that the record set is only locked when it is updated rsNewListMember.LockType = 3 'Query the database rsNewListMember.Open strSQL, adoCon 'Randomise the system timer Randomize Timer 'Calculate a code for the user strUserCode = Left(strEmailAddress,2) & (9876989856 * CInt((RND * 32000) + 100)) 'Loop through all the records in the recordset to check that the user id and the e-mail address are not already in the database Do While NOT rsNewListMember.EOF 'If there is no user code or it is already in the database make a new one and serch the recordset from the begining again If strUserCode = rsNewListMember("ID_Code") Then 'Randomise the system timer Randomize Timer 'Calculate a code for the user strUserCode = Left(strEmailAddress,2) & (9876989856 * CInt((RND * 32000) + 100)) 'Move to the first record to make sure the new user code is not in the database rsNewListMember.MoveFirst End If 'If the e-mail address is already in the database then create an error message and exit loop If strEmailAddress = rsNewListMember("Email") Then 'Create an error message strMessage = strMessage & "Your e-mail address is already in the mailing list" 'Set the error boolean to true blnError = True 'Exit the for loop Exit Do End If 'Move to the next record in the recordset rsNewListMember.MoveNext Loop 'If there is no error message then add the new user to the database If blnError = False Then 'Add new record to a new recorset rsNewListMember.AddNew rsNewListMember.Fields("Email") = strEmailAddress rsNewListMember.Fields("ID_Code") = strUserCode rsNewListMember.Update 'Set the suject of the e-mail thanking teh user for joining and the -mail body strSubject = "Thank you for joining " & strWebsiteName & "'s mailing list" 'If there is a welcome message then send it to the new mailing list member If strWelcomeMessage <> "" Then 'set the message body of th e-mail strEmailBody = strWelcomeMessage 'Create email object Call createMailObject(strMailComponent) 'Write a remove from mailing list message to add to the end of the e-mail in HTML Format strAppendToEmail = mailBody(strWelcomeFormat, strUserCode, blnLCode) 'Send the email Call SendMail(strEmailAddress, strMailComponent, strWelcomeFormat) 'Drop email component Call dropMailObject(strMailComponent) End If 'Set the message to thank the user for joining strMessage = strSubject & ".
" End If 'Reset recordset variable rsNewListMember.Close Set rsNewListMember = Nothing 'If the mode is to delete then un-subscribe the user Case "delete" 'Create recorset object Set rsRemoveListMember = Server.CreateObject("ADODB.Recordset") 'Initalise the strSQL variable with an SQL statement to query the database strSQL = "SELECT tblMailingList.* FROM tblMailingList WHERE tblMailingList.Email = '" & strEmailAddress & "';" 'Set the cursor type property of the record set to Dynamic so we can navigate through the record set rsRemoveListMember.CursorType = 2 'Set the Lock Type for the records so that the record set is only locked when it is deleteed rsRemoveListMember.LockType = 3 'Query the database rsRemoveListMember.Open strSQL, adoCon 'If there is no error message then add the new user to the database If NOT rsRemoveListMember.EOF Then 'Delete the record rsRemoveListMember.Delete 'Create a mesage for the user strMessage = "You have been removed from " & strWebsiteName & "'s mailing list.
Sorry for any inconvenience. " Else 'As there is no record returned we need an error message strMessage = "The e-mail address enetered did not match any in the database.
You have not been removed from " & strWebsiteName & "'s mailing list." End If 'Reset recordset variable rsRemoveListMember.Close Set rsRemoveListMember = Nothing End Select 'Reset server objects Set strCon = Nothing Set adoCon = Nothing %> Mailing List
<% = strWebsiteName %>'s Mailing List

<% 'If there is a message for the user then display it If IsEmpty(strMessage) = False Then 'Write the message for the user Response.Write(strMessage) Else %>
An unspecified error has occurred.
Please try again.
<% End If %>


Close Window


<% %>