Question

I have an agenda system where you can schedule events and appointments and choose a date and an hour for each appointment/event.

The user will choose to receive a reminder to its appointment, the reminder can be 5 minutes before the event starts, 1 hour before the event starts, 2 days before the event starts, etc...

So, if an user's appointment is scheduled to Today at 10:00 PM and the reminder is "10m before", I'll need to make php send an e-mail to the user at 09:50 PM. For now, I'll have to make reminders to be sent 5m, 10m, 30m, 1h, 2h, 5h, 12h, 1d and 2d before the event starts.

How can I make PHP send e-mails on users defined time?

Thanks a lot.

Was it helpful?

Solution

You'll want to separate the system into two applications:

  • The application users use to schedule things.
  • The back-end application that sends the emails.

The first application should write the scheduled items to a database and the back-end application would read from the database and send the emails.

One way you may be able to structure this is to have the scheduled items stored something like this:

ScheduledItems
--------------
ID (int)
Title (string)
EventTime (datetime)
ReminderType (int)
ReminderSent (bool)

The ReminderType might be a foreign key to a lookup list of the reminders you mentioned:

5m, 10m, 30m, 1h, 2h, 5h, 12h, 1d and 2d before the event starts

The second application would then be scheduled via cron to run probably every 5 minutes (since that's the most granular reminder time) and scan for ScheduledItems where ReminderSent is false (hasn't been sent yet) and EventTime minus some timespan value in the ReminderType lookup table is less than or equal to the current datetime.

For each record it finds, send the reminder and update ReminderSent to true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top