Sending the Emails
The first thing that we need to do is to create the mail object using this command:
Set Mail = Server.CreateObject("Persits.MailSender")
This creates an object that has properties, which we can set or check, and methods, which we can execute. First, we'll set the basic information needed to send the mail, such as the mailserver and the "Reply-To" information:
Mail.Host = "mail.nicholaschase.com" ' Specify a valid SMTP server Mail.From = "firstname.lastname@example.org" ' Specify sender's address Mail.FromName = "Nick Chase" ' Specify sender's name Mail.Subject = request("subject") Mail.Body = request("message")
From here, we need to make a decision. We can send an individual email to each user, or we can just send one message to all of them. Because in this case we are sending the same text to all of our users, we'll opt for a single message this time around. We don't want to give every user the entire list of addresses, however, so instead we'll address the message to ourselves and add our users as "Blind Carbon Copies." This means that their address information won't appear in the message, but they'll still receive it.
Finally, before we start adding users to the message, we'll check to see whether we are sending an HTML message or a text message.
This is an important distinction. Increasingly, users are sending and receiving HTML email, but this involves more than just adding a few tags to a text message. The reason for this is that the receiver's email client, such as Netscape Messenger or Microsoft Outlook, needs special lines of information, called headers, that identify the message as HTML. Using the choice from our "send" form, we'll set the isHTML property of the Mail object to let ASPEmail know whether to send those headers:
if request("mailtype") = "HTML" then Mail.isHtml = TRUE Else Mail.isHtml = FALSE end if
Now that we've set all of the relevant properties, we're ready to add all of our users. ASPEmail uses an array of addresses, so we'll just keep adding each address as we find it. Then, because we will no longer need them, we'll close and destroy our database objects in order to free up the connection and the memory.
while not userSet.EOF Mail.AddBCc userSet("email"), userSet("first_name")&" "&userSet("last_name") userSet.moveNext wend userSet.close set userSet = Nothing mailDB.close set mailDB = Nothing
All of this is great, but we haven't actually sent the mail yet! In actuality, the email is sent with a single line:
Unfortunately, several things can go wrong when you're sending an email, so we'll plan for that. This statement turns off the default error page:
On Error Resume Next
This way, if an error occurs, it sets the properties of the Err object, which we then can check. If Err.Number is 0, then no problem has occurred; otherwise, we can get a description of the problem.
Finally, we deallocate the memory for the Mail object, and we're done!