I have three mysql tables; Users, Tasks and Task_Users, where each user can create their tasks through a web application I'm developing in PHP.
The thing is, I want my application to send an email to the users one day before the task occurs. The first thing that came to my mind was to do a MySQL job or trigger to send the email but I dont have an idea of how to do id,
What would be the best way make this happen or how could I call a job through PHP and execute it every day and verify that there is just one day left before the taks ocurrs and then if that is true send the email?
Here's my table definition. Thanks in advance.
Users
----------
UserId,Names,email,pass
Tasks
------------
TaskId,TaskName,Descripcion,TaskDateTime
Tasks_Users
------------
TaskId,UserID
You can write a CRON Job for this.
What's CRON Job ?
Cron is the name of program that enables unix users to execute commands or
scripts (groups of commands) automatically at a specified time/date. It is
normally used for sys admin commands, like makewhatis, which builds a
search database for the man -k command, or for running a backup script,
but can be used for anything. A common use for it today is connecting to
the internet and downloading your email.
What do you have to do ?
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:
How to run a CRON Job after it's written ?
http://www.inmotionhosting.com/support/edu/cpanel/301-run-cron-job
If you have any issues let me know.
I'd make a cron job that regularly runs a script (say every 5min?) which checks for tasks that need emails sent. Then the script script sends the necessary emails.
*/5 * * * * php /path/to/script.php
Would call script.php, which checks and sends emails, every 5minutes.
Some info on cron is here:
http://www.pantz.org/software/cron/croninfo.html
Related
I'm developing website that will send email back to user automatically when they registered to my website. I have searched from internet, most of them said that i have to used cron jobs; the big problem foo me now is about cron jobs. I don't how to write it and also how to execute it. Can anyone gives me some example about it?
Thank in advance.
If we assume that the script that you want to run is located at /home/me/myscript.php then all you need to do is create a cron job that will run that script every minute.
Several hosting companies have an interface (cPanel for instance) that will allow you to add a cron task easily. You can also add the cron task by editing the relevant cron job and adding:
*/1 * * * * /usr/bin/php /home/me/myscript.php > /dev/null
If you are wanting to send a confirmation email to the user once they have registered, you can use php mail function to do this.
http://php.net/manual/en/function.mail.php
You can create a PHP file which has a heartbeat event. You put that to sleep using the usleep function with the desired time interval and you can have a list of tasks to be executed (heartbeat tasks), knowing the time rules (when they should run, how often, and so on), and your heartbeat.php will check after each usleep which tasks should be executed and execute it. You can run this heartbeat.php on your server.
However, I do not see the reason why do you need a cron job, heartbeat or any periodic solution. Why don't you send the emails using php functions exactly when you need to send them?
I am having some mail ids in my database. I have to send mail to those mail ids automatically on some specific dates that i have mentioned in my database.How to do that in php.
It depends on your server type. If you use linux you can use cronjobs to program the execution of a specific php file at a certain time.
If you are hosted, your host may offer a cronjob menu in their cpanel, else you would have to get a better hosting plan that offer that. Or at least access to crontab file where you program the different Cronjobs.
Executing a PHP script with a CRON Job
You need to write cron jobs for sending the automatic emails on a particular date. Without this it is not possible.
Syntax
Here is a simple cron job:
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script.
Timing Syntax
This is the first part of the cron job string, as mentioned above. It determines how often and when the cron job is going to run.
It consists of five parts:
minute
hour
day of month
month
day of week
Or
You can set the cron in you cpanel.
Here are a few general steps of the logic that you can follow:
The PHP script should:
extract the email addresses and the specific dates from your database
into arrays.
check whether
today's date is
in the array containing the dates. Be sure to format the date appropriately!
loop through the email addresses (preferably in a foreach() loop),
if the above check returns 'true'.
within this loop, use PHP's mail() function to send your email.
Your next step would be to set up a scheduled task that runs this script daily, usually by means of a cron job. This step depends on the server you've got.
NOTE: These steps illustrate the most basic way to do what you require. There may be other, more complex ways that would be faster, cleaner and less intensive on your server.
As answered by Albert James it depends on the system type on which your application is running. If it is Linux then you can get the php script which send mail executed by cron jobs and if you are using windows machine then you need to execute that php script with schedule task : How to run a PHP file in a scheduled task (Windows Task Scheduler)
Also here is the option if you don't want to use schedule task\cron jobs (Though I haven't used that): How to send schedule emails using php without cron job
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
example
I'm developing website that will send email back to user automatically when they registered to my website. I have searched from internet, most of them said that i have to used cron jobs; the big problem foo me now is about cron jobs. I don't how to write it and also how to execute it. Can anyone gives me some example about it?
Thank in advance.
If we assume that the script that you want to run is located at /home/me/myscript.php then all you need to do is create a cron job that will run that script every minute.
Several hosting companies have an interface (cPanel for instance) that will allow you to add a cron task easily. You can also add the cron task by editing the relevant cron job and adding:
*/1 * * * * /usr/bin/php /home/me/myscript.php > /dev/null
If you are wanting to send a confirmation email to the user once they have registered, you can use php mail function to do this.
http://php.net/manual/en/function.mail.php
You can create a PHP file which has a heartbeat event. You put that to sleep using the usleep function with the desired time interval and you can have a list of tasks to be executed (heartbeat tasks), knowing the time rules (when they should run, how often, and so on), and your heartbeat.php will check after each usleep which tasks should be executed and execute it. You can run this heartbeat.php on your server.
However, I do not see the reason why do you need a cron job, heartbeat or any periodic solution. Why don't you send the emails using php functions exactly when you need to send them?
I want to build an application that enables users to schedule emails to send any time. simply, write email message and schedule it so that the server sends it at time specified. I am using zend framework. How to do it in php? Can it be done with cron jobs? If yes, then what are the disadvantages of using cron?
can it be done with cron jobs?
Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time".1 Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.
http://ubuntuforums.org/showthread.php?t=586478
I would run a cron job every minute and check if there are any mails ready to be scheduled. The quote from the forum topic below instructs how to run cron every minute.
crontab -e
then set a tab like
* * * * * /command
The first star is the minute section,
so having a star there will execute
every minute
In case it makes it more clear if you
wanted every 5 mins then it would be
*/5 * * * * /command/to/execute
And the other stars are from left to
right
minute hour dayofmonth month
dayofweek*
*0=sunday
Disadvantages of cron?
if yes, then what are the
disadvantages of using cron
When doing a lot of cronjobs you will have to spawn a lot of processes(pay cost of spawning process which is expensive). In that case it would be better to have background process(es) running continually and fetch messages from message queue. But when you want to run a cronjob only ever minute than I assume this will not be a big case.
I would tackle this using a cron job.
Simply create a script that checks for messages to send at a certain time. The user schedules for say 1PM (using a database of course), the script runs every 5 min, or so, and it checks (the db), are there any messages to go out for the current time? If so, it sends out the emails, else it sleeps.
Clean and simple way of handling it.
Disadvantages?
I can't see any, this is what a cron is made for, running tasks at specific times.
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
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.
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.