tsangaris Posted February 18, 2015 Share Posted February 18, 2015 Hi, I have a PHP script sendEmail.php that given some data (receiver, content, etc), sends an email to the receiver. When i click the Send button i fire the sendEmail.php script using AJAX: $('#sendbutton').on('click', function(){ $.ajax({ type: "POST", url: "js/ajax/sendEmail.php", data: {receiver:receiver, content: content} }); }); I want to add a feature call "Schedule send" where the user can select in how many minutes the email will be send. What i did, was to take the minutes value, insert it into a countDown timer, and as soon as the time is Zero, to fire the same AJAX call. This is working only if the browser is open. And i can understand this, since it is Javascript's responsibility to fire the AJAX call since its the one on client site. QUESTION: Is there a way to fire this sendEmail.php scipt even if the web browser is closed?Using PHP/MYSQL of course.. For example the user will select the recipient, write the email, schedule it to be sent 20 minutes later, and close the browser. If not, what are your suggestions? Thanks in advance, Christos Quote Link to comment https://forums.phpfreaks.com/topic/294695-execute-scripts-even-if-browser-is-closed/ Share on other sites More sharing options...
maxxd Posted February 18, 2015 Share Posted February 18, 2015 You'll want to set up a CRON job to take care of this. Basically, you save the e-mail information and the time the user wants to send the email into a database table, write a script that pulls all the messages that haven't been sent yet but are scheduled to send now(ish), and send them. You then schedule a CRON on the server to run every 15 minutes or so that runs this script. I'm not sure I'm explaining this terribly well (sorry), but Google CRON jobs and that should get you moving in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/294695-execute-scripts-even-if-browser-is-closed/#findComment-1505990 Share on other sites More sharing options...
tsangaris Posted February 18, 2015 Author Share Posted February 18, 2015 Thanks maxxd! I will use Google for the rest! Just a question: If the task i schedule is time-sensitive (if it runs 2 minutes after schedule it may cause problems), will this be something i cannot handle with CRON jobs? I am referring to the now(ish) part of your response! Quote Link to comment https://forums.phpfreaks.com/topic/294695-execute-scripts-even-if-browser-is-closed/#findComment-1505992 Share on other sites More sharing options...
maxxd Posted February 18, 2015 Share Posted February 18, 2015 CRON jobs are set to run at set times of day (for instance, 15 minutes after every hour, or 7 minutes after every third hour. or 12:15), so when your user creates the e-mail and sets up the send time you can either present them with a drop-down of acceptable times, or set up your select in the script that's going to be run via CRON with a time range. Obviously the first suggestion is going to be more specific and truthful to your end user - if a user schedules a message to be sent in 10 minutes, but they schedule it at the beginning of the 15-minute select range, the CRON script will pick it up and send the message immediately, which is not the desired action. Please note that this is a ridiculously simplified 'explanation' of how CRON works and what type of schedules you can create with it. Quote Link to comment https://forums.phpfreaks.com/topic/294695-execute-scripts-even-if-browser-is-closed/#findComment-1505994 Share on other sites More sharing options...
tsangaris Posted February 18, 2015 Author Share Posted February 18, 2015 I understand! Again thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/294695-execute-scripts-even-if-browser-is-closed/#findComment-1506008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.