How can I keep this from being sent twice?

Ninjas...

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"
rsReminder.open 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=""mailto:rick@mtss.net"">rick@mtss.net</a>!<P>Here's the link:<P>"
aMailBody(3) = "<A HREF='" & server.htmlencode("http://www.brucegust.com/MTSS/QuickDip.asp?Dateline=" & Dateline) &"' target='blank'>http://www.brucegust.com/MTSS/QuickDip.asp?Dateline=" & 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 = "MTSS@brucegust.com"
subject = "QuickDip - your MTSS Newsletter"
message = emailbody
mailserver = "mail.brucegust.com"

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

RSReminder.MoveNext
Loop

set conn=nothing


response.redirect "NewsletterMailDone.htm"

%>
brucegustPHP DeveloperAsked:
Who is Participating?
 
chisholmdConnect With a Mentor Commented:
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.


0
 
HecatonchiresCommented:
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.
0
 
chisholmdCommented:
Your right, but I figured since its the Scuba Club newsletter I doubted he sent it more then once a day :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.