Sending Mass Emails to Subscribers
November 13, 2000
Another potential use for your registration database ties together
just about everything discussed in this series of articles. This time,
let's assume that you have a big database full of registrants, and
you need to contact each and everyone of them to update them about a
change to your service offerings. The best way, is of course, email,
but how will you send it to all of them without setting up a mailing
list and bulk email program? Easy, use a combination of form handling,
CDONTS and SQL to get the job done.
For this particular exercise, I am not going to give you all the code,
just snippets to help you on your way, you need to work on it and set
it up yourself. We will begin by taking a closer look at the
conceptual design.
First, you will need a simple web page that has a number of form
fields on it. One should be a "From" field to say who the message
is from. A second should be the subject of the email. For a third
field, consider an option for sending HTML or plain text. Finally,
the fourth field should be the body of the message.
Next, you need to develop a system that checks if the form is
submitted and then processes that form appropriately. You will need
it to A) Create the email message B) Obtain the email addresses for
all your registered users, and C) Send an email to each user.
Now, part "A" should be simple, assign the value of the body field
to bodyHTML or some other variable (I use msgBody below). Then, when
the time comes, set objCDO.body = bodyHTML. But how
about getting the email addresses out of the system and sending the
mail? When sending a bulk message you need to start thinking about
your server's capacity and the most efficient way to do things,
because sending out a few hundred to thousand emails is pretty taxing.
I recommend something like the following:
<%
'assume variables are declared and assigned
'values up here, among other things.
strSQL = "SELECT First_Name, Last_Name, Email FROM tbl_Users;"
rst.Open strSQL, conn,3,3
while not rst.EOF
msgBody = ""
Set objCDO = Server.CreateObject("CDONTS.NewMail")
objCDO.From = "Administrator@Somedomain.com"
objCDO.To = rst("Email")
objCDO.Subject = subj
'subj is the variable for the form field
msgBody = "Dear " & rst("First_Name") & " "
msgBody = msgBody & rst("Last_Name") & ": "
msgBody = msgBody & bodytext & vbCrLf
'bodytext is the variable for the form field
objCDO.Body = msgBody
objCDO.Send
set objCDO = nothing
rst.MoveNext
wend
%>
Again, nothing too complex that cannot be understood with a little
effort. Presuming all the preprocessing is done, like assigning
variables and validation, the first thing one needs to do is
establish a connection to the backend database. The SQL statement
used above retrieves the first name, last name and email address of
every user in the tbl_Users table. The next section of code is the
standard while not rst.EOF or end-of-file technique.
Each iteration (or time through) the loop, the server will invoke a
new mail object, configure the email attributes and send the email.
msgBody = "Dear " & rst("First_Name") & " " &
rst("Last_Name") & ": " might be a bit confusing, so let's
take a look at it more carefully. All that is really happening is
that the first line of the msgBody is being set equal to "Dear So and
So: ". We do this by concatenating (sticking together) the first name,
a space, last name, and a colon, using the ampersand: &. The
same sort of thing occurs on the next line. msgBody is set equal to
itself to carry over changes from the previous line and bodytext is
added to it. Assume bodytext is the variable you set equal to
request.form("email_body"). Finally, the message is sent
and the object set to nothing to free up resources and be less taxing
on the server as it sends out many emails. The whole process repeats
until the end of the recordset!
One thing I did not mention was how to deal with the "Send HTML or
Plain Text" type form field. I am not going to show you line-by-line,
but I will give you a terribly obvious hint. In the while loop, add
some conditional processing (if statement) to check what the value of
that form field is. If it is HTML, write some code that sets the mail
and body formats equal to 0, if not, set them to 1. Easy as pie!
Conclusion
Hopefully some of you out there have learned what you wanted to learn
with this series. We have discussed using ASP for form handling,
validation, sending email, and working with simple databases. Using
any of these techniques in conjunction with another can solve many
problems the average web developer will encounter and can, in fact, be
fun to work with. Next month I will be talking about something new,
but I have not decided on any one topic. If there is something you
are aching to know how to do but have not been able to figure it out,
drop me an email and I will consider it for my next article! See you
next month!
Using the Registration Database
Using ASP for Form Handling: Part 4 - Filling the Gaps
|