开发者

How to avoid sending multiple emails to the same person, when each email is triggered individually

开发者 https://www.devze.com 2023-03-24 22:17 出处:网络
I have a site that gets a us开发者_开发百科er 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.

I have a site that gets a us开发者_开发百科er 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?


You can create a DB that will store information about wich user got access to what item.

Create an User table, an Item table, and an intermediate table that makes a relation between User and Item.

When someone gives access to another one to an item, you can query the DB to know if the person has already got access to that item before, and then send an e-mail or not!

Just an idea... that is, if having a database is an option, otherwise... I have to think more... let me know! =)


There's 2 simple ways I can think of here:

  1. 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.

  2. 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 Users and has an id column as its primary key, and an email column which records the current email address for that user:

  1. Create a Emails table which has columns of id (primary key), user_id (foreign key representing the user entry in the users table) and email_text (the text of the emails you want to send).

  2. Each time you would normally send an email, instead add a record to the Emails table, with the user_id pointing to the correct record in your Users table.

  3. Write a separate C# (or other language) application/script which does the following:

    1. Get 1 record from the Emails table, 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)

    2. Send the email based on the information you pulled from the database

    3. Delete the record from the Emails table

    4. If you have more email to send, go back to (sub) step 1.

  4. 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.


Easiest option, with very little re-factoring, is to:

  • Store the emails in the db instead of sending. To store them in the database in a very simple manner, just create a table with email/subject/body and send the info there.

  • After it is stored, you will then need a trigger to send the email.

    • If you want to schedule it to send every 30 minutes, you will need to have a service or scheduled program created to do this. Best way to do it would be to use a service with a timer in it.

    • If the emails are not that crucial, you can have it triggered by a queue. Like the example mentioned above; when you insert a new email record, check to see if there are 10, if there are, then send it out.

  • After the email is sent, remove the records or tag them as sent.

0

精彩评论

暂无评论...
验证码 换一张
取 消