jeudi 26 mai 2016

mass mailing: loop with ajax calls freezes randomly

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.

// 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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire