I have a system that sends emails for me and my clients every day in the morning. This process is triggered automatically through access to my website, via an iframe.
Often, I receive the same email twice.
The simple process I created on a MySQL database to control this is as follows:
run this query:
Select clients for send my emails
SELECT name, email FROM clients WHERE DATE_FORMAT(lastsend, '%d/%m/%y') <> DATE_FORMAT(now(), '%d/%m/%y')
Function for send emails
I use vb.net for send emails
Update Table Clients after send all emails
UPDATE clients SET lastsend = Now();
I need a way to lock this down to prevent the same emails from being sent multiple times on the same day.
Thanks,
What you have is a race condition. There are many ways to resolve this, some more robust than others. “Robust” means guaranteeing that there is never a duplicate email.
Here is a simple way. Add a second date column into your table: SendStart. Set send start to now() before you attempt to send the email and change the WHERE clause to:
You could still get a race condition between the first query and the second. However, the time span is much smaller, so the likelihood is much, much smaller.
In the end, you will have a record of both when the email was sent, and whether it was sent (don’t set lastsend unless the send is successful). You can do batch reporting and error handling, for the emails that are unsuccessful.