简体   繁体   中英

automatic emails from user account database

I have a database full of user accounts and other information that is linked to those user accounts. I would like to offer the users the ability to setup automatic emails which notify them that new data has been added to various tables that are linked to their account.

I am not looking for full blown out code, but a point in the right direction here. I have been searching and reading for the last day and have seen many different opinions on this.

The objective is a user will define an email address and rate at which to check for new data. For instance they supply test@test.com and 15 minutes. Once this is done I want to check if new data has been added to their account every 15 minutes, and if so, alert them via email. I have no problems writing the actual code to do this just not sure the 'correct' way to approach it as far as scheduling and automation of the script.

  • MySQL take scheduler - I cannot use this as it is more than strictly db operations. I need to actually run php code (sending the email and other stuff) on a scheduled basis rather than just perform operations in the db.

  • Cronjob - I could make this work, however, it would be ONE cron job that checks EVERY user in my database and would have to check at the lowest interval I offer my users (1 minute). Of course, my initial query on the database would only return those that it actually needs to check (where current timestamp is > last alert timestamp + rate seconds) I would think running this script on the entire user set of my database every minute would be a performance issue, especially as the user base grows. With this option there would also need to be some file locking on the script I would think to prevent it from running multiple times.

  • Software side - as the database is tied to software the user installs on their computer I could also have the software 'hit' the php script I create at the user defined rate interval. If they set it to 15 minutes then it would hit the script every 15 minutes to check. This would work also, but as the user base grows this is an extreme number of hits to the same page fairly often as each user account would be doing so at their interval setup, however, it would also only happen when the software is running for that user which would cut down on that number.

With that said, any recommendations? Positives, negatives, for and against one method over the other or some other suggestion I have not come across yet?

Forget the software side, your check needs to be driven server side.

I would use a cron job to do that. You are right, you will have only 1 cron job for all your user, but it is not a problem.

Imagine you have a cron job running every minutes. In the cron job, the first task will be to find users you need to verify if something happened for them according to the current time and the user periodical check choice. Sometimes, you will not find users to check, sometimes yes. Even if you have plenty of users, it will not take long time. Then, loop only through the users which need to be checked and send e-mail if needed.

My suggestion is not to make it time based but send an email every time there's something new. Analogous to receiving an email when somebody comments your post on social networks. Not sure if that's possible with your business requirements. Observer design pattern springs to mind, although there are many ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM