Newsletter send using AJAX to avoid PHP timeout
Posted
by
simPod
on Stack Overflow
See other posts from Stack Overflow
or by simPod
Published on 2011-11-29T00:20:37Z
Indexed on
2011/11/29
1:50 UTC
Read the original article
Hit count: 281
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.
© Stack Overflow or respective owner