Results 1 to 3 of 3

Thread: PHP send emails according to date and time in database

  1. #1
    Join Date
    Nov 2010
    Thanked 0 Times in 0 Posts

    Default PHP send emails according to date and time in database

    Hi All,

    I know that using cron jobs we can send an automated emails at a particular time. But my requirement is, if a user saves his remainder in database, he should be mailed at that particular time.Can anyone help me how to achieve this.

  2. #2
    Join Date
    Apr 2008
    Thanked 516 Times in 502 Posts
    Blog Entries


    use a cron job to check the database for reminders that need to be sent.

    you could limit the server load by limiting reminders to specific intervals, say, every 15 minutes.

  3. #3
    Join Date
    Mar 2006
    Illinois, USA
    Thanked 690 Times in 678 Posts


    There are two ways to do this:

    1) Create a single cron job that is constantly running (not literally-- every 15 minutes is a good idea) and checking to see if any messages should be sent. You'll need some way to mark them as "sent" so that you don't do extra emails.

    2) When a user sets a reminder, you could specifically create a new cron job (a one time thing? one that repeats every day/week?), just for them. This is logically a little simpler and maybe more accurate. It might also be more efficient if you only have a few users (so you don't really need to check every 15 minutes) but will be much less efficient for many users (because then it would run MORE than every 15 minutes).
    However, the biggest problem with that is that I don't know how to actually dynamically create cron jobs using PHP. I think you'd need to use exec() to do some fairly low-level commands on your server, and that might be a little dangerous (not to mention difficult).

    I'd definitely recommend (1), and in fact it's what I do on my site. I simply have a "shell" cron job that runs every few minutes and looks for anything new it should do. There are a few tasks. For example, it looks at any new email messages that have been sent to the admin account and if something needs to be done with an attachment, it saves those, and for others it adds a note for the administrators that there is a new email (since that account is not very active). And it can potentially do anything I want-- I'll just need to save another command somewhere that the cron job will notice as it runs.

    Equally possible is having one cron job per type of task (for all reminders, or for handling all emails), but in my case I find it easiest to just have everything in one place. And that way it's almost like the server is "alive" because it's always running just waiting for new stuff to do.

    Two notes:
    1. Having an "infinite" cron job like I explained is a little risky. You want to be very careful that there are no infinite loops or errors in the code or it might mess with your server. Checking it's activity is also a good idea-- my cron job ads a log entry in the database every time it runs so I can check easily to see if there's a problem. Similarly, it's worth checking that the cron job is still running every few days-- and if your server ever crashes, make sure it's running again once the server starts up again. I've found that the cron job was somehow turned off for a while and I didn't notice it. Just had to start it again and it was fine.
    2. You probably should tell your users that the email might not be received at exactly the moment they set. There are a couple reasons: first, if you do have a 15 minute loop, it could be up to 15 minutes late; even if you don't, there might still be some processing delay (or the server might be down at some point). And most automated emails are said to arrive "within 24 hours"-- that's an exaggeration, but some mail servers might have a little delay once in a while, or, more likely, the message can just take a while to arrive. For example, some mail clients don't register new emails immediately. I've found that some are slower than others, so it's easy to wait for 5 minutes if even if you're sending an email to yourself. (However, I think that newer email systems might have fixed this. I haven't noticed that problem for a while. Recently the delays are usually not more than 1 minute.)
    3. So, if you want this to be a "reminder" for them to do something, make sure it's sent a little early rather than at the exact moment they should do it. Using email as an alarm clock, for example, is not a good idea.
    Last edited by djr33; 11-30-2011 at 09:23 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts