|
<%
' Declaring variables
Dim rs, mail, subject, message, data_source, sql_select, no
no = 0
subject = Request.Form("subject")
message = Request.Form("message")
' Adding a link to all messages by which users can delete their
' emails if they would want later
message = message & vbcrlf & vbcrlf
message = message & "To stop receiving emails click here :"
message = message & vbcrlf
message = message & "http://yoursite.com/urfolder/del.asp?email="
sql_select = "select email from users"
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("mail.mdb")
' Check to see if you have not pressed the 'send' button mistakenly
If Len(message) Then
' If you have written some message then lets send it
' You can use ASP Email component of your choice, here I will
' stick with CDO
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open sql_select, data_source
While Not rs.EOF
Set mail = Server.CreateObject("CDONTS.NewMail")
mail.From = "webmaster@yoursite.com"
mail.To = rs("email")
mail.Subject = subject
mail.Body = message & rs("email")
mail.Send
Set mail = Nothing
no = no + 1
rs.MoveNext
Wend
' When messages have been sent to all the users, exit
Response.Write "Emails sent to " & no & " users."
rs.Close
Set rs = Nothing
' Had you pressed the button mistakenly with text area empty, then
' redirect back to the HTML Form
Else
Response.Redirect "admin.htm"
End If
%>
Done sending emails to all the users in our mailing list. Lets now create a 'del.asp' page
whose link we have already added in our messages. Users will be able to delete their emails
from out database if they want it by clicking the link we provided to them in their emails.
Note if you are getting errors when testing this script on your own computer, then search in the
'windows/sytem/' diretory ( win95/98 users ) or 'winnt/system32' directory ( winnt/2000 users ) if
'cdonts.dll' is present there or not. If it's not then get it from the Windows CD or search it on
www.microsoft.com. You should change the URL provided in
above code for the email deletion page to the URL where that 'del.asp' page will actually reside.
ASP Email Deletion Page
Create a new page and add the following lines of code into and then save it as 'del.asp' in the same
folder where you have kept other pages :
<%
' Declaring variables
Dim email, con, data_source, sql_delete
email = Request.QueryString("email")
sql_delete = "delete email from users where email = '" & email & "'"
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("mail.mdb")
Set con = Server.CreateObject("ADODB.Connection")
con.Open data_source
con.Execute sql_delete
con.Close
Set con = Nothing
Response.Write "Your email address " & email & _
" was successfully deleted from our database."
%>
We are now done with creating our ASP based news letter for our site. I have delibrately kept things
simple so that beginners can also understand whats going on. You can add your own features to it as you
want.
|