Jump to content

Execute scripts even if browser is closed


tsangaris

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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! :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.