shivam0101 wrote:my question is,
how to do it?
That is a very vague question, one that your code snippet doesn't even really seem to address remotely. However, what you are asking is an interesting thought, and I'll probably end up implementing a similar feature in the future.
I think you should save events as they occur in a table that associates the events with the user. I'd have each event sent to a central event processing object that would do this for me, and then it would have an instance variable that serves as a flag to inform the event processor whether an event has occurred during the current page request. After all of the data is served, the event processor would be told to handle the events. During that process, it would check the user's preferences to see if they wanted per-event notification, and, if they did, get all of their events, mail out notifications, and remove the events from the table. If not, then all of this user's event would remain in the event table.
That's where you'd use a cronjob that would get all of the users who wanted periodic notices, and use that period to mail all of their accumulated events together, and remove them from the events table.
That's how I'd do it at least.