I am making web application in PHP. I need to send mail to my users after every interval of time (say after every two days). How can i keep track of time intervals in php (assume i know the start time).
You could create a cron job that runs one of your php scripts locally, and have that script check against a database to see whether or not the time is appropriate to send the email. For example,
In your crontab:
0 0 */2 * * php /usr/bin/mailcheck.php
And in mailcheck.php, just have logic to check for entries in a database that are overdue for an email.
Or, if you just want to blindly send an email to everyone every interval, just have a sendmail() entry in mailcheck.php, and that'll do it too.
Related
After some research, I haven't found anwers for what I'm trying to achieve.
I found some information about the php sleep() function, and some inforamtions about CRON jobs, but none of these options seem to solve the problem.
Here is what I try to do : I have a php file which may receive webhooks. I need to wait some time (15 minutes for example) before "reacting" to this webhook. So, basically, my script should :
1 - receive the webhook (Already done with current code)
2 - wait some time
3 - do some actions (Already done)
I've already done what I needed without the wait part, and it works very well, bu now I don't know how to do with it...
If I understood it well, Cron Jobs are executed periodically, while I want to wait some time only when the webhook is received.
I thought about the sleep() function, but I'm afraid it may use to much ressources...
How can I do this ?
Update : It seems I can't use the sleep function more than 30 seconds (max execution time I guess ?)
If I was you, I'd build a queueing system so that when a webhook comes in, a row in a database is inserted. I'd add a column for 'execution_time' and set that to 15 mins from when the webhook came in. You can then setup a cron job that runs every minute but only fires where the execution_time is 'now'.
use cron
“At every 15th minute past every hour.”
Minute Hour Day Month Weekday Command
15 * * * * php /homelink/path/filename.php
Note:
You can set an email address for the cron. Every trigger it will send an email notification. If receive email means cron is working, If not then need to check your code. Also you can debug using the appropriate echo's.
I have a website in PHP and users can schedule message to be sent. I can sent message with command similar to this one:
php sendMsg.php 249
where number is ID of the message
Many people suggested to use cron jobs, but since I don't want to run this in interval cron is no option(only once - for example after 3 hours).
My idea was as follows:
$seconds = $hours*60*60;
exec('sleep '.$seconds.'; php sendMsg.php 249');
But this wont work because it will block php for further executing. What is the simplest way to achieve this?
You said you don't want to use a cron job because you only want the message sent once, but this is mis-understanding the way that a cron job would be written for this kind of task
Consider a situation where you have many users creating many messages to be sent at various given points in time.
You don't want to have a PHP program sitting running on your server all that time for each of those messages; it would be wasteful of server resources, even if they were all just sleep()ing for the duration.
Instead, one would use a cron job to run a short-lived PHP program once every minute (or whatever interval suits you).
Your message creation program would not be written to acually send the message; instead it would insert it into a database, along with the time it needs to be sent.
Meanwhile, the cronjob PHP program would scan this database every minute to see if there are any messages that are due to send but have not yet been sent. It would then send those messages and mark them as 'sent' on the DB.
This is the standard way to write this kind of thing, so it's not surprising that people are recommending it to you.
Doing it this way means that you never have a program running on your system for longer than necessary. Both PHP programs do their job quickly and exit, meaning that no-one is kept waiting for them.
It also makes it much more robust. Imagine if your server had to be rebooted. If you had a bunch of PHP programs running for hours waiting for their moment to send their message, they'd all be lost. On the other hand, if they had saved their message to a DB, the cron job would find them and send them correctly once the server was restarted.
Put the schedule in a database. Run a cronjob every minute or so, check the database if a message should be sent within this minute, and send it.
Is there a reason you don't want to use a cron job? That would be the simplest and most efficient way of sending the messages.
I would think that a cronjob ist still the right way
Create a table where the to be send messages are stored, with a timestamp when to be send and a flag for isSend
Create a cronjob - start php skript every 1 minute , which sends the messages with timestamp < current time and isSend = false
Ignore suggestions of cron, if you want to simply wait a period of time then use the at scheduler:
$hours = 2;
$command = sprintf('echo "php sendMsg.php 249" | at now + %d hours', $hours);
exec($command);
I want to schedule sending of email from php script. I want user to specify date and time and then on the specified date and time, I want those emails to be sent automatically. How do I implement it? I am working on codeigniter.
One way to do it would be to create a "scheduled_emails" database table. Put all the emails you want to queue in there, including columns such as, recipient, subject, message and optional headers.
You could then set up a script to look at that table and send any emails that have a "send_time" which is greater than the current time. You could then set up a cron job to run this script every.. 5 minutes for example.
PHP usually uses an external scheduler for this sort of thing. That means cron on *nix or Windows Task Scheduler on Windows.
If you want to set it up through a web interface, then you might consider storing your schedule in a database and having cron (etc) kick off a script that looks for overdue emails every 5 minutes.
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?
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.
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).
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
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
I'm working on a database that saves records for laborers details
such as name, nation, visa expiry date.
So, I want to create a little script that searches and matches expiry dates with today's date and send me a notification by email within 1 month before the visa expires.
A very rough outline, assuming you already have a database table.
Every day run a script that calculates todayminusonemonth (solved in Stratton's answer) and performs a select * from databasetable where expirydate = todayminusonemonth. Iterate over the result set, compose a message with the target's specific information and send it out using php's mail interface.
It's by far the easiest solution to make sure the script runs every day, if that's not an option a column should be added to the db to indicate that the mail has been sent. Or an extra table listing all days for which mails have been sent could be created...
See? This first decomposition of the problem was quite easy. Now you can start solving each of these partial problems, or look up information/howtos for each. Feel free to create a new question with more specific needs you may have.
One problem you may run into is that your webhost doesn't allow cron scripts to be executed. A very creative alternative solution using a website monitoring service is explained in I don't have cron jobs on my server. What is an alternative for sending emails without user input?
try something like this:
$monthAgo = date("y-m-d",strtotime('-1 month'));
$sql = ("SELECT * FROM tablename WHERE visa_expiry_date < '".$monthAgo ."'");
and you'll get all the visas that are 1 month away from expiration.
then you just send an email using mail
do this all in a cron job
You can try SQLyog's SQL Scheduler and Reporting Tool which has a Job Agent that allows you to generate, format and send personalized mails with results returned from a query. This allows you to schedule execution of query(s). The resultset(s) can be emailed to multiple recipients. You can specify an external file or enter query(s) that you want to execute using this tool.
Read this.
You need to look into cron jobs? Which will be executed at the specified time with given interval.