sveti petar
sveti petar

Reputation: 3787

Schedule e-mail notification 24 hours before certain event?

I have a PHP-based site with bookings/appointments stored in a MySQL database.

I want to set an e-mail notification to be sent to each person who made a booking exactly 24 hours before their booking. Bookings are added through a regular PHP form.

I know I could add a script to, for instance, the index page that checks the database for any bookings that are in the final 24 hours, but that's unreliable since I won't be getting much traffic at first. So the index page could go hours without visits, and the notification would be hours late.

The other solution that came to mind is to set a cron job that runs every minute, calls a PHP script which checks whether any e-mails should be sent and sends them. But I'm not sure if this is overkill in a way; does anyone have a better solution than having something run in the background every minute?

To sum it up - is there a way to do this without cron?

Upvotes: 2

Views: 3277

Answers (4)

Aleks G
Aleks G

Reputation: 57306

Triggering the job from a web page is a very bad idea, for two reason: (1) if you don't get traffic to your site, the job doesn't run; (2) if you get a lot of notifications, the job will be slowing down the response to the web requests (assuming you invoke the job synchronously).

I would strongly discourage you from running a job every minute either - it definitely will be an overkill. Instead, think whether you really need "exactly 24 hours" as the interval or would "between 22 and 26 hours" be ok.

We have a similar requirements - and went about it by setting a job that runs every 4 hours and checks what notifications need to be sent for events starting between 22 and 26 hours form the time the script runs. This way, the script is only execute 6 times in a day and everything gets sent correctly.

If 4 hours approximation is not good enough, then think to the largest interval that's appropriate. I'm sure 1 hour should be sufficient. Have a script run once an hour (from cron) and check for events starting between 23 and 24 hours from the time of the run.

Remember that once your email is sent, it doesn't end up in the recipient's inbox immediately: sometimes it takes a few seconds, but sometimes it may take an hour or even more - so an extra hour difference in your script won't be a problem.

Upvotes: 1

Albi Patozi
Albi Patozi

Reputation: 1468

a cron job every minute has no sense ! but you can do a cron job every hour because i think it doesn't meter a hour difference or 2 hours . without cron it isn't any other way . it will take about 2 second (at max) to complete so it is worth

Upvotes: 0

jugnu
jugnu

Reputation: 139

there is no way other than setting cron or sending request to server through periodic calls...below is post similar to your question, you may get idea.

Live redirect based on periodic server calls with JSON or AJAX

Thanks

Upvotes: 0

Madara's Ghost
Madara's Ghost

Reputation: 174947

You don't need to use cron as an interval'd timer. You can set a very specific date and time when you want your job done.

Here's an article on the subject

For instance:

0 0 18 5 * <php command here>

Will run every May 18th at midnight. That's more than enough time to clear it before the next iteration (next year).

Upvotes: 0

Related Questions