I have a wordpress plugin that sends up to 3,200 newsletters each week to our subscribers. However, it freezes sometimes after 800, sometimes after 1,200 mails (it's always different, there is no certain pattern). We use turbo smtp as our host.
Here's how I do it:
I loop through the rows of a table, where all the mail addresses are stored, and trigger an ajax call for each row.
// *** massmailer.php ****
// get total count of subscribers
$sqlAll = "SELECT * FROM subscribers";
$resultAll = mysql_query($sqlAll);
$numrowsAll = mysql_num_rows($resultAll);
// send mail for each subscriber, increasing row pointer $i each time
$sql = "SELECT * FROM subscribers LIMIT $i, 1";
$result = mysql_query($sql);
while ($guy = mysql_fetch_array($result)):
$mail = new PHPMailer();
$mail->Host = 'pro.turbo-smtp.com';
.... (other smtp settings here)
$mail->Send();
$i ++; // increase the row pointer by 1
// if row is NOT the last in the table, repeat this step for the next row
if ($i < $numrowsAll) {
echo "<script>$('#placeholder_massmailer').append($('<div>').load('massmailer.php?i=<? echo $i ?>'))</script>";
}
endwhile;
It works perfectly, but sometimes, the $.load ajax call seams to freeze after a certain amount of ajax calls.
Should I use another architecture? Is $.post better for this? It works as part of a wordpress plugin; when it has freezed, I notice that wordpress tries so send a "heartbeat" ajax call?
thanks Matthias
As others suggested build a real program (java/c/etc) that can do this server side and update a record in the database for the current status of the run.
Then your front end site can just access that status record and verify to the user it is functioning. This should as others stated not be done in javascript and is the root of your issues. You could easily poll using Ajax every 30 seconds to check the record and return the status in near real time. If real time is needed an WebSocket could also be used to push the status back out to the web client.