I have a site that gets a user to approve another users access to something.. What I want to do is send off an email to that user, when their access has been approved.
Each access item is in a gridview, with an approve button that someone clicks.
The issue is that I don’t want 10 emails to be sent to the user if they have 10 approvals to be done.
I want just one email to be sent.
This somehow requires the email procedure to be delayed, and grouped..
Does anyone know a nice and simple way of doing this?
There’s 2 simple ways I can think of here:
Don’t send emails when the user clicks, and instead send 1 email every 30 minutes or so. This would require some sort of script which runs every 30 minutes to check what emails need to be sent, which then goes out and sends the emails. This would probably be the best way to do this, but a bit harder. You can extend this a little if your script runs regularly (every minute or so) to only email users which have been inactive, if you really want to avoid sending multiple emails.
Batch the emails up so that when the amount of stored emails reaches (e.g.) 10 emails for a user, then all the emails for that user are sent. This has the potential major downside (depending on your expected usage) that some emails may be massively delayed or not sent at all (if a user is only ever asked to approve 1 thing), so you really shouldn’t do this unless you are 100% certain this will never happen (e.g. all users asked to approve at least 10 items per day), and even then I’d recommend not doing this.
Regardless of which way you pick, you’re going to need to store which emails still need to be sent in some way, and I can’t really give much advice here because I’m not sure of the specifics of your setup.
Update: Since you’re using c#, you’ll have a database (e.g. Microsoft SQL Server) backing your website, and probably a table for users. I’ll assume it’s called
Usersand has anidcolumn as its primary key, and anemailcolumn which records the current email address for that user:Create a
Emailstable which has columns ofid(primary key),user_id(foreign key representing the user entry in the users table) andemail_text(the text of the emails you want to send).Each time you would normally send an email, instead add a record to the
Emailstable, with theuser_idpointing to the correct record in yourUserstable.Write a separate C# (or other language) application/script which does the following:
Get 1 record from the
Emailstable, and its corresponding user entry, using something like:SELECT users.email, emails.email_text FROM users, emails WHERE user.id = email.user_id LIMIT 1;(I don’t have a SQL server handy to test, so adjust syntax as required)Send the email based on the information you pulled from the database
Delete the record from the
EmailstableIf you have more email to send, go back to (sub) step 1.
Set up a scheduled task (or Cron task, if anyone’s trying to apply this to a non-windows deployment) which runs your C# application (or whatever language you chose) every 30 minutes.