Run function few hours before event occurs - php

I have one problem. I am learning cakephp 3 and I want to do the folowing:
User enters date and time (this is saved into mysql as datetime format) and system needs to send email few hours before this datetime that he enters (for example, 1 hour before)
However, I want to do this without cronjob.
So user enters 2019-05-12 12:00 and I want immediately to schedule task to send email on 2019-05-12 at 11:00.
Is this possible and yes, can you please give me any pointers? I also checked cakephp tasks, but it seems that I don’t see the whole picture yet

create Shell
bin/cake bake shell CronJobs
open created shell and create method to send email, for example:
public function sendEmal()
{
}
Inside this method add your code to select data and Mailer to send mail.
now in your cron tab set task and point to this shell script, example
* 1 * * * bin/cake CronJob sendEmail
Please read:
https://book.cakephp.org/3.0/en/console-and-shells/shells.html
https://book.cakephp.org/3.0/en/console-and-shells/cron-jobs.html
https://book.cakephp.org/3.0/en/core-libraries/email.html
Update:
However, I want to do this without cronjob.
Other way is to implement some Queue system.
Read here for more information

Related

Scheduling emails with cron jobs from a mysql table

I am trying to set up a system where a user enters some information in a form and an email will be constructed where the information is saved into mysql.
I am trying to figure out how to make it so the email will be sent, for example, 20 minutes after the user makes their input. (Without the user staying on the browser).
I need this delay as I need the ability for an admin to log on to a page to look at the email and possibly edit it before it sends.
Is this possible through a cron job. Am I able to set one up that automatically checks sql table for an update and then sends the email after a certain time?
Or is it possible to delay a php script with the sleep function and then send the email. Can I make the PHP script still run when user has closed site and left?
you can use mySQL to store the data sent by the user. this data will be accessed later using another script triggered by a cron Job: if you have the ability to set cron jobs in the control panel or via access to the server, go ahead, use cron tab syntax to define when the job will be triggered, this website may help you:
https://crontab-generator.org/
another approach is to use external service to trigger an event every interval, the event could be accessing the cron job script via HTTP.
if you want your email to be sent exactly after 20 mins, please add a field to your mysql table indicating the desired send date(beware of timezones).
you may also want to add a flag indicating if the email is sent, so you do not send the same email twice.
You can't (easily) have a PHP script stay alive that long.
Your best strategy, IMO, would be to have the PHP script create the email file, and notify the human.
Then you can have PHP run a shell script which uses the "at" program to schedule a task to happen in 20 minutes. At is a cousin of cron, but is better suited for this job.
That scheduled task will be to take the e-mail message, move it some place else (like a "done" directory), and pipe it through your mailer. tip: /usr/sbin/sendmail -t < myEmailFile will work on most Linux boxen.

Schedule alarm notification system php

I need some help regarding the implementation of the following alarm.. Here's the flow of the program, user login to the system and then they can click the hyperlink create schedule and then from there they can create schedules using the form. After which, user can choose to start this schedules that they created and they can allocate a timeframe to it. For eg, if the user assign the schedule to run at 2pm the system will have a pop up to notify and inform the user to run this schedule probably 15 mins before 2pm..
I would like to know what are the ways to implement this in php and if possible is there any reference i can use on the website? I've tried to find but apparently most scripts are paid etc.
I think you are looking for scheduler kind of thing, to do this in php
you can use scheduler for windows os
cron job for *nix based os
Yes the answer to this question is pretty much related to CRON job. Take a look at this answer
Execute Query on a Specific Date and Time
You need to write a PHP script that scans the db and sees for the user who should be notified (i.e their deadline has arrived). Run this script in the scheduler (maybe every 1 hour) and it will do the magic for you. Good luck with your notifications :)
No cron job is needed if you only notify user with popup on the web site, however, you will need a cronjob if you want to send emails.
You will have to store users schedules (possibly in a db table)
When the user logs in php should check if there's a task due in the schedules table for that user: if yes show the pop up.
You will need to create an ajax call (with users id) that will call a certain php (say ajax.php) file every minute. You can help yourself with jquery.
The ajax.php should check if there was a task due in the past. If yes it returns job details (as json or html, you chose) else it just returns that there are no jobs.
When the calling ajax code recieves an answer from ajax.php, it does nothing if the answer is that there are no jobs or displays a popup with job details recieved from ajax.php.
The user can dismiss the call (delete it from the schedules db table) snooze or reschedule it (update the due date in the schedules db table).
Don't forget about security especially in the ajax.php: it has to check if the user is logged in.
Only if you want to prompt non logged in users by email you will need to set up a cron jobs, that will ping the ajax.php file periodically.

PHP automated email script

Id like to generate an automatic email at certain times of the week or daily from my website to certain users. i.e at 12am, or 5pm .I'd like an email reminder sent to user example#example.com. Can anyone point me in the right direction. I have read about cronjob but didn't got much information.
You are looking in the right direction... A cronjob would suffice.
This would allow you to run a php script on a schedule
Start by looking here
Do you have shell access to your host? Or does the host have CPanel installed?
If you have shell access, you can run the following command:
crontab -e
Then insert a new line like this:
* 0,17 * * * /path/to/php/executable /path/to/script/which/sends/emails.php
This will call your PHP script every day at hours 0 and 17 (12AM and 5 PM). The email sending should be done in /path/to/script/which/sends/emails.php
You need to create php script which will do all work (get data to send, get emails list, send data). After that You need to assign this script to schedule in crontab

Delete user from database if email not confirmed

I am currently working on a registration based website, and I have the server sending an activation email to the user upon registration. This is all done in PHP so, as you can imagine, I am using the mail() function.
This is all fine and dandy, once the user gets the email and clicks the activation link, the 'active' field that is in the 'Users' table is set to true. Here's the problem though, in the case that a user does not confirm their email address, what am I to do?
I have thought of holding details like the date and time the user registers but I don't know how to proceed with this data. How do I have the server automatically delete the user from the database after a set amount of time?
That's what I think I should be asking, but in all honesty I don't know the usual protocol...
Conclusion: Since Cron is for Unix based servers I've had to pass on it, but I found it very interesting that I could just use the Windows Task Scheduler that is built into Windows. This at least means I can test it on my PC before any server hosting. Thank you all
You should definitely store the date and time that the activation link was sent.
There isn't really a way to tell the server to automatically delete stale user data, but it's easy enough to code up yourself. Assuming you have access to cron on your server, you can set up a cronjob to run (for example) every night at 2am and execute a PHP script that searches the database for users who were sent a link more than X days ago but never confirmed it
i think the solution would be Storing the timestamp while sending the mail.
now run a cron every 15minutes which would check that which values are having timestamp more than 24hrs or any timelimit you want and then delete it from db
Just call in your index.php file the following code. (Why index.php ? - because it is requested every time and can "act" as a cronjob.)
(Just Pseudo Code - might need some tweaks!)
mysql_query("DELETE FROM user WHERE active = 'false' AND registerTime < (NOW-60*60*24*7)")
This will delete all Users which have not been activated within 7 Days.
It's just a concept idea i think you can build on.
You should look into cronjobs that you run daily. Simply put in a field in your database with the time your user registered.

User-specific timer actions in a MySQL database

I have a database full of users and each user has timestamps for a specific events they've executed. I would like to fire off an action for each individual user based on their timestamps. So for example: user1 hasn't logged in for 5 days, send her an email or put her user status to "idle" mode.
What would be the best way about doing this?
I don't know if it's the best way but usually you setup cron jobs to that kind of things.
Your cron job would be a simple PHP script, it would query the database to find all the users that the system should process and send emails etc ...
If you are on windows the scheduled task should be a drop in replacement for cron jobs.

Categories