Mailing list subscription/unsubscription script
  • This script will add or remove emails to a list. It will not send emails. For this second porpouse you may check this script
  • The script code bellow has been split in different parts, each one corresponding to a subroutine. After each section of the script an explanation is provided.
  • Explanation of each part of the script is provided bellow.
  • The form asking for emails (included in the last subroutine of the script) may be place in a different page (but it must point to the script).
  • This script will createa file in the server the first time an email is submit (at this moment is set up to create the file at "/cgi-bin/subscribers.txt").
index.asp
<%
DIM Responsetext

if request.form("email")="" AND request.querystring="" then
   Sendform()
else

     if request.form("email")<>"" and request.form("action")="Subscribe" then
         GetrandomnamberandSavetoSession()
         CheckEmailandSendEmail()
         SendResponse()
     end if

     if request.form("email")<>"" and request.form("action")="Unsubscribe" then
         RemoveEmail()
         SendResponse()
     end if

     if  request.querystring<>"" then
        CheckrandonnumberandSaveEmailtoFile()
        SendResponse()
     end if 

end if 
%>

Explanation
  • The first time  the page is visited the form will be send to the client. 
  • If it is not the first time (request.form("email") or request.querystring has a value), depending on the situation, the corresponding subroutines will be run. Options are three:
    1. A new request for additon to the list

    2. request.form("email")<>"" and request.form("action")="Subscribe"
    3. A request to unsubscribe from the list

    4. request.form("email")<>"" and request.form("action")="Unsubscribe"
    5. A confirmation to add the email to the list. 

    6. request.querystring<>"" 
<% 
sub GetrandomnamberandSavetoSession()
  Randomize
   session ("randonnumber")=INT(Rnd ()*1000000)
   session ("email")=request.form("email")
end sub
%>
Explanation
  • Random number  is generate (lines 3-4)
  • Random number is stored in a session variable (line 4)
  • The email is stored in a session variable (line 5)
This random number will be send to the subscriber. It will be included in the url the subscriber must visit  a confirm the inclusion in the mailing list.
<% 
Sub CheckEmailandSendEmail()

Wemail=request.form("email") 
test1=instr(Wemail, "@")                   ' value must be >1 
test2=instr(Wemail, ".")                      ' value must be >4 
test3=len(Wemail)                              ' value must be >6 
test4=InStr (test1,Wemail,".",1)        ' value must be >test1 2 

if test1<2 OR test2<5 OR test3<7 OR test4<test1 3 then 
    Responsetext="Your email is not valid<br>Please go back and write it correctly"
else 
   Sendmessage()
end if 

End Sub
%>

Explanation
  • It is check whether email provided is valid

  • If email is not valid, Responsetext variable is provided a appropiate value (you may change it)
    If email is valid Sendmessage() subroutine will be run
Responsetext above may be change to meet your preferences. This variable will be used in the response page which may be also customized in the corresponding subroutine.
<%
Sub Sendmessage()

message= "Dear Sir/madam," & CHR(10) & CHR(10)
message=message & "A request to join our mailing list has been get at mydomain.com. " & CHR(10)
message=message & "Please visit the following link to make efective you subscription to our mailing list." & CHR(10) & CHR(10)
message=message & Request.servervariables("SERVER_NAME") & Request.servervariables("URL") & "?" & session ("randonnumber") & CHR(10) & CHR(10)
message=message & "Take care" & CHR(10) & CHR(10)
message=message & "John Smith" & CHR(10)
 

set smtp=Server.CreateObject("Bamboo.SMTP") 
  smtp.Server="mail.yourdomain.com
  smtp.Rcpt=request.form("email") 
  smtp.From="webmaster@yourdomain.com"
 smtp.FromName="John Smith
  smtp.Subject="Mailing list inclusion request
  smtp.Message = message 
  on error resume next 
  smtp.Send 
  if err then 
    response.Write("Internal server error")
   response.end
  end if 
  set smtp = Nothing 

   Responsetext="One more thing to do: you must visit the link you will get by email within next minutes in order to confirm your inclusion in our mailing list.<p>This operation must be done suring this session, otherway your information will be lost"

End Sub
%>

Explanation
  •  An email will be send to the user for verification. 
  • The message is defined in the first part of the subroutine
  • Withing the message an url  the user must visit is provided. This url is formed this way:
    • Request.servervariables("SERVER_NAME") & Request.servervariables("URL")  will deffine the url as for example www.mydomain/mydir/myfile.asp. This code will allow as to change the name or location of the script in our server withou changing the code of the script
    • "?" & session ("randonnumber") will add the random number generated  in advance to the url, so that it may be recornize by using Request.querystring 
    • The resulting url will be like this one: www.mydomain/mydir/myfile.asp?123456
You must change in the script some parameters (mail server, email address, your name, subject of the message) and you may also change the complete body of the message in the first part of the subroutine. 
Responsetext above may be change to meet your preferences. This variable will be used in the response page which may be also customized in the corresponding subroutine.
<% 
sub CheckrandonnumberandSaveEmailtoFile()
  if Clng(request.querystring)=session ("randonnumber") then
  SaveEmailtoFile()
 else
   Responsetext="We have no data related to your previous visit to this site<p>Probably your session has espired, so we are unable to add your email to our mailing list<p>Please try to join our mailing list again, and visit to the corresponding link as soon as possible, before the session is expired."
  end if 
end sub
%>
Explanation
    In case the subscriber confirms he wants to subscribe the mailing list he will click in the url send by email to him. In this url is included the random number we generated before sending the email to the subscriber, and this random number will be the value we may recover with the code "Request.querystring". As this value is a string, it is transformed to a number with  instruction "Clng()" and it is compared with the random number stored at "session ("randonnumber")". If they are the same number the email of the subscriber will be save to our file. 

    In case they are not the same number, the most probable reason may be the subscriber has responded by clicking in the url send to him by email a bit late, so his session has expired and the value for "session ("randonnumber")" is null. In this case "Responsetext" is set up.

    Responsetext above may be change to meet your preferences. This variable will be used in the response page which may be also customized in the corresponding subroutine.
     

<% 
Sub SaveEmailtoFile()

email=session ("email")
email=lcase(email)
email="<" & email & ">"

Wfile=server.mappath("\") &"/cgi-bin/subscribers.txt"
Set fs = CreateObject("Scripting.FileSystemObject")

on error resume next 
Set a = fs.OpenTextFile(Wfile) 
allemails = a.ReadAll
a.close 

if instr(allemails,email)>0 then
  Responsetext="You are already a subscriber to this mailing list"
else
  allemails= allemails & email
  Set a = fs.CreateTextFile(Wfile,True) 
  a.Write(allemails) 
  a.Close 
  Responsetext="Your email has been added to our list successfully"
end if

end sub
%>

Explanation
    The email which was stored at session("email") is save to a variable named email, transformed to lower case and added to it "<" and ">" in the first and last positions. Adding this characters in those positions is very usefull, as it will allow us to better control the list (see subroutine bellow).

    Then we will open the file where emails are stored and it will be checked whether the email is already in the list. If so, the apropiate Responsetext is defined. If not, the email (including the additional characters) will be added to the list.

    In case "/cgi-bin/subscribers.txt" is not present in the server, it will be created when adding the first email to the liost(it s not necessary to crate it in advance). T location the serverof the fila and its named can be changed without problems. 

    The file where the emails are stored will look like this one:
     

    <subscriber1@domain1.com><subscriber2@domain2.com><subscriber3@domain3.com>
    <subscriber4@domain4.com><subscriber5@domain5.com><subscriber6@domain6.com>
    <subscriber7@domain7.com>

    After adding the email Responsetext is defined.
     

<% 
Sub RemoveEmail()

email=lcase(request.form("email"))
email="<" & email & ">"

Wfile=server.mappath("\") &"/cgi-bin/subscribers.txt"
Set fs = CreateObject("Scripting.FileSystemObject")

on error resume next 
Set a = fs.OpenTextFile(Wfile) 
allemails = a.ReadAll
a.close 

if error then 
  Responsetext="The mailing list is not available at the moment"

else
 if instr(allemails,email)>0 then
     allemails=replace(allemails,email,"")
      Set a = fs.CreateTextFile(Wfile,True) 
      a.Write(allemails) 
      a.Close 
      Responsetext="Your email has been remove from our mailinglist"
  else
      Responsetext="Your email is not included in this mailing list"
  end if
end if

end sub
%>

Explanation
    The email (which was stored in a session obhect) will suffer the same transformation as in the previous subroutine.Then the file contain the list of emails is open and read (in case an error occurs when trying to open the file a Responsetext is defined), I the email is included in the list it will be deleted. If not the corresponding response will be set up. 

    When trying to delete the email (see subroutine bellow), this additional character added in first and last position ("<> and "<")  will prevent the script to delete aditional emails:
    p.e. when deleting from the list "a@domain.com" we will avoit deleting "ana@domain.com" (because we will delete "<a@domain.com>").

    After deleteing the email  the corresponding answer will be set up. 
     

<% Sub SendResponse() %>

<html><head><title>Request to join mailing list</title></head>
<body bgcolor="#FFFFFF" text="#000000">
 

<% =Responsetext %>
 

</body></html>

<% End Sub %>

Explanation
    This subroutine is used to display our answer ae corresponding action  and it may be totally customized. Just keep "<% =Responsetext %>" within the code. 

    "Responsetext" is the piece of tee defined somewhereelse in the script depending on what action has took place. 

<% Sub SendForm() %>

<html><head><title>Request to join mailing list</title></head>
<body bgcolor="#FFFFFF" text="#000000">
<center>
Add your email to our mailing list

<FORM METHOD=POST ACTION="<% =Request.servervariables("URL") %>">
<BR>
Email address: &nbsp; <INPUT TYPE="text" NAME="email" Size="60" MAXLENGTH="128">
<BR><BR>
<input type=radio name="action" value="Subscribe" checked> Subscribe &nbsp;&nbsp;&nbsp;&nbsp;
<input type=radio name="action" value="Unsubscribe"> Unsubscribe<BR><BR>
<INPUT TYPE="SUBMIT" VALUE="Submit/Enviar"><INPUT TYPE="RESET" VALUE="Clear/Borrar">
</FORM>
</center>

</body></html>

<% End Sub %>

Explanation
    This subroutine is used to introduce the email o the user the first time he  to the page. 
    The form may be place in different page as far as it points to the script. 
 

AspTutorial.info.