How to send emails via cron job usng PHP mysql

46,827

Solution 1

Just write a normal PHP script -- make one that will work if it's launched directly from the browser. Then schedule that very same PHP file to run in cron, using this as a guide:

http://www.unixgeeks.org/security/newbie/unix/cron-1.html

Basically, using the values at the beginning, specify the schedule (minute, hour, day of week, day of month, etc.). Then set the user it runs as, which is likely to be "apache" or whatever your web server daemon is running under. Then set the "command" that cron is running to be php php_email_script.php (where "php_email_script.php" is the name of your PHP file.

Solution 2

30 minutes and still no answer, here's a few open doors:

  • cron reads it's rules from system-wide /etc/crontab, or from you personal crontab which you edit with crontab -e
  • cron takes a format where you say on which minute / hour / day / month things should happen, use google or man crontab for the format
  • cron has the amazing side effect of mailing the output of the command to the user owning the crontab

Now you are stating that you're using php. The easiest way to get some php running from cron, is to issue a wget -O - -q http://yoursite.com/yourprocessingscript.php?verysecret=123123 and have an appropriate processing script on yoursite.com. (you may want to let that script check $_SERVER['REMOTE_ADDR'])

So in short, if you just put the right magic in /etc/crontab, like

0 0 * * * jay wget -q -O - "http://yoursite.com/processmidnight.php?secret=yes_very"

and have your script produce some sensible output, you will get a mail delivered to local user jay, which you may want to forward.

Share:
46,827
JDesigns
Author by

JDesigns

Updated on November 15, 2020

Comments

  • JDesigns
    JDesigns over 3 years

    i managed to send multiple emails (check here).i am stuck with sending automated emails via cron.

    This is what i need - while the admin send emails, i store the message, emails, event date in the database. now i am trying to set a cron job to send emails to all these ids from the table with the message i have as a reminder. i am not familiar with cron job scripting, can anyone help in guiding me the right way to write script that i can place in cron tab. I am planning to send two mails - one day exactly before the event and on the day of event.thanks

  • mvds
    mvds almost 14 years
    I used to do it like this as well, but in my experience the wget method is far more easy to maintain, since you still have only one entry point. Of course you need to be aware of the security implications, so to keep it on the safe side, the script should not take arguments.
  • JDesigns
    JDesigns almost 14 years
    Hi, thanks, i followed your instruction and i did that. it works fine! I am going to post the answer (simple query and mail function) as answer. Should i post that under "answer your question"?
  • JDesigns
    JDesigns almost 14 years
    hi, thanks, your explanation and the below answer helped me in going the right direction. i will post what i have soon. it works fine. thanks. i tried the curl method but i did by calling the php script in the cron tab.
  • JDesigns
    JDesigns almost 14 years
    hi, i was quick in replying, if my query returns 3 records for which the date is after current date, it is just sending mail for the first one and not for the others. I tried while loop but that doesn't help. any suggestions?
  • Nathan Loding
    Nathan Loding almost 14 years
    I'd post this as another question and get some help that way. Sounds like either your query or your while loop is wrong.