How can I keep this from being sent twice?

Posted on 2006-06-04
Last Modified: 2008-02-26

Below is the script that I'm using to facilitate the emailing of a newsletter. The problem is, my user is refreshing the screen and/or going to another page where he has the option to re-send the same content.

In theory, provided all the emails are sent, this wouldn't be an issue. But with 2000 plus emails going out, his box has a tendancy to time out which then produces an excellent environment to inadvertantly send out the same email twice.

What can I do to prevent this from happening. I thought about establishing a Session.Time_now variable and then qualifying what was going out by prefacing it with an "if" statement. That way if the page was refreshed, you would still have that Session variable to ensure that the same email wouldn't be going out to the person who's record had already been updated.

Before I start down this road, however, I wanted to let minds greater than my own weigh in on this. My question is, "Does what I described above sound like the best way to ensure that the same email wasn't going out twice, or is there a better way to do it?"

The code is below, I'm looking for any input and thank you in advance.

<%@ Language=VBScript %>

set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open session("dbConn_ConnectionString")
set rsReminder = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM MTSSCustomers" sql,dbconn,3,3

Dateline = Replace(Request.Form("Dateline")," ","+")

Server.ScriptTimeout = 60000

Letter = Replace(Request.Form("Greeting"),vbCrLf,"<br>")

Dim aMailBody(7)

aMailBody(0) = "<font face=""arial narrow"" size=""3"">"
aMailBody(1) = Letter
aMailBody(2) = "<P>Click on the link below to access the ""Middle Tennessee Scuba and Swim QuickDip Newsletter!"" <P>If you have any questions, " _
& "contact MTSS at 615-771-0002 or emailing them at <A HREF=""""></a>!<P>Here's the link:<P>"
aMailBody(3) = "<A HREF='" & server.htmlencode("" & Dateline) &"' target='blank'>" & Dateline & "</a>"
aMailBody(4) = "</font>"
emailbody = Join(aMailBody,"")

Set mailer = Server.CreateObject("ASPMAIL.ASPMailCtrl.1")

date_now = Now()
Do Until RSReminder.EOF

recipient = RSReminder("email")
sender = ""
subject = "QuickDip - your MTSS Newsletter"
message = emailbody
mailserver = ""

cTypeString = "text/html; charset=""iso-8859-1"""

result = Mailer.XHeader("Content-Type", cTypeString)

result = mailer.SendMail(mailserver, recipient, sender, subject, message)

sql2 = "update MTSSCustomers set LastSent = '" & date_now & "' "_
& "where email = '" & RSReminder("email") & "'"
DBConn.Execute sql2


set conn=nothing

response.redirect "NewsletterMailDone.htm"

Question by:brucegust
    LVL 7

    Accepted Solution

    why dont you check the last sent date in your first query

    SELECT * FROM MTSSCustomers where LastSent <> '" & date_now & "' "

    You'll have to actualy parse the date down to just the date since your usin Now() which includes seconds etc.

    Once you have the first query set to only select rows that have not yet been sent on this day you don't have to worry about the page being refreshed.  If your mailer chokes on a badly formated email address you just re run page confident that you won't email anyone twice.

    LVL 7

    Expert Comment

    For an accidental refresh, checking the date not so great, because it also prevents you from sending more than one email a day.
    Do it in two steps.
    Step one - create the list of people to send email to.  Make this a feature: "Targetted email campaigns"
    Get creative: people from certain areas, age groups, genders, etc.  maybe everyone with a certain widget in their purchase history
    The selected people get inserted in another table with a 'processed' flag set to false

    Step two - send the email
    for each email sent, mark the selected person as processed.

    at the end, either delete them, or keep it as a record of customer contact.

    keep it as two seperate steps, that way if they refresh half way thru sending, they dont recreate the list of people to send it to again.
    LVL 7

    Expert Comment

    Your right, but I figured since its the Scuba Club newsletter I doubted he sent it more then once a day :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now