I have a php file that send emails to our clients who are expecting it and to ease the load on the server and to eliminate it looking like a spam server we reduce the flow to 40 emails every 20 seconds. This is done by using javascript to rerun the page every 20 seconds. The page itself reads from a data base and gets the next 40 email addresses, sends them, and then recycles until all are sent. Then the email list is reloaded from a duplicate table for the next cycle three weeks out.
Here is the javascript code that seems to not run when initiated by a cron job
<script language="JavaScript" type="text/javascript">
setTimeout("location.href = \'PT_enrollment.php?sendDate='.$sendDate.'\'",20000); // milliseconds, so 10 seconds = 10000ms
</script>
When I run this from my browser it does run correctly so I guess it is in the Cron Job functionality.
What am I missing? Is there a setting or parameter I need to set in the crontab or on the server so it runs correctly?
You could just have your PHP process sleep for those 20 seconds:
$emails = // retrieve all emails from database
$done = 0;
foreach ($emails as $email) {
// send email to $email
$done++;
if ($done % 40 == 0) { // every 40 emails
sleep(20); // wait 20 seconds
}
}
Ordinarily, cron jobs are run server-side via php-cli, which by default doesn't have a timeout. Some hosting providers run cron jobs by actually retrieving a URL via wget or curl, in which case you do have to account for a possible timeout. In that case, you can add set_time_limit(0);
to your script to work around that.
I don't Know how you are running This .. The server will not run JavaScript if you added your JavaScript to file test.php you can run something like
file_get_contents('https://yourdomain.com/test.php');
This will hit the file like you run on browser
or rewrite your Task on PHP language like
$url = 'https://yourdomain.com/PT_enrollment.php?sendDate='.$sendDate;
file_get_contents($url);