An ASP Tutorial to create your own News Letter
|
An ASP Tutorial to create your own News Letter News letters are part and parcel of every good site on the web today. When a web site visitor enters it's email address into the news letter input box, it is saved into the database. As more and more visitors enter their email addresses, the mailing list grows. Then when ever the you want, you can send emails in one go to all the users of your mailing list. News letters are a great way to inform web site users of latest news and information about your web site. High-end commercial email component : In this article I will guide you through the steps of creating a simple but effective news letter. We will divide our work into three steps :
Alright then lets now move to the creation of our News Letter messaging system. Simple HTML Form News Letter : <form action="mail_ac.asp" method="post"> <input type="text" name="email" size="11"> <input type="submit" value="submit"> </form> Above HTML code will produce a simple input box where users can enter their email addresses. After entering their email address and pressing the 'submit' button, the users email address will be ported to our 'mail_ac.asp' page where the email address will be inserted into the database. You can edit it to suite the design of your site. Form Action Page <%
' Declaring variables
Dim email, con, data_source, sql_insert, sql_check, rs
email = Request.Form("email")
' Check that email address is empty or not. If email address
' is not empty then continue, otherwise redirect the user back to
' the main page
If Len(email) Then
' Note you can add your database's DSN here if you want
data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("mail.mdb")
' Our SQL statements
sql_check = "select email from users where email = '" & email & "'"
sql_insert = "insert into users( email ) values ('" & email & "')"
' Lets first check to see if the email address is alread present in
' the database. Create the instance of Connection Object and then
' open the database
Set con = Server.CreateObject("ADODB.Connection")
con.Open data_source
Set rs = con.Execute (sql_check, , 1)
' If email address is not present then add it to the database
If rs.EOF Then
con.Execute sql_insert
' Displaying the result of successful entry to the user
Response.Write "Thank you. Your email address <b>" & email & _
"</b> was successfully entered into the database"
' But if email address is already present then display it to
' the user
Else
Response.Write "Your email address " & email & _
" is already present in the database. Thank you."
End If
' Done. Disconnecting and setting Connection Object to Nothing
con.Close
Set con = Nothing
' And if the email address submitted was empty Then
Else
Response.Redirect "mail.htm"
End If
%>
This 'mail_ac.asp' page will first check to see if the user has just pressed the 'submit' button and has not entered the email address, if it is so then the user will be redirected back to the 'mail.htm' page. If the user entered it's email address then first check database to see if it's already present or not. If email address is not found in the database then add it otherwise tell the user that the email address is already present and exit. Simple Access Database ![]() Users - Table Ok, we've completed first two steps. Now we'll move on to the next page where we will create 'admin' page and an email deletion page from where users can delete their email addresses from your database. We have created an HTML Form to receive user's email address, an ASP action page to insert that email address into the database and an Access database to receive and store those email addresses. We will now create an admin page from where we can send emails to all the users on our mailing list at once and an email deletion page from where users can delete their email addresses if they later want to. HTML Admin Page <p align="center">ADMIN PAGE</p> <form action="admin_ac.asp" method="post"> Subject :<br> <input type="text" name="subject"><br><br> Message :<br><br> <textarea name="message" cols="60" rows="15"></textarea><br> <input type="submit" value="send"> </form> Save it as 'admin.htm'. All it will do is to provide you with an online Form from where you can send messages to all the users of your mailing list. ASP Admin Action Page |
<%
' 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.
posted on 2004-10-18 20:04 settinghead 阅读(383) 评论(0) 收藏 举报

浙公网安备 33010602011771号