I need to send newsletters. I have already a PHP script that sends mass emails but it won’t work for long as email database is growing because of PHP max script run time.
So, to avoid it I came up with a solution: I would call my PHP script using AJAX in javascript and I will give it $_GET parameter with a count 20 so the script would sent only 20 emails. Than AJAX would receive success response, and call my script again and again till all emails are send.
Is it possible? I’m asking because I have never seen such a solution so I’m wondering if it is real (It’s kinda hard to implement this into my PHP framework so I’m asking experts here first)
To sum it up here’s a code skeleton:
<script>
var emailCount = 1000; //would get this from DB
var runCount = 20; //number of emails sent in one cycle
var from = 0; //start number
function sendMail(){
if(from<emailCount){
jQuery.ajaxfunction({
path: 'script.php?from='+from+'&count='+runCount
successFc: function(){
from+=runCount;
sendMail();
}
})
}
}
sendMail();
</script>
So, are there any obstacles? Thanks a lot.
Using AJAX
Of course you can do it using AJAX, but in this case you should also make sure
errorparameter is also defined for.ajax()function in jQuery (see documentation). It will ensure the script will be invoked even if the previous call returned some error (such as error in your PHP script or something).Using Cron jobs
Another idea is to use CRON jobs, which will call your script on constant interval and you won’t need your browser to be running all the time. See more about Cron. If you need solution for Windows (Cron is Unix-based), you can use for example Cron for Windows.