Showing the result according to loading and do not wait for the script to finish to display the results on the screen

Asked

Viewed 519 times

3

My question is the following: the code is working as expected the problem when I put a large list of email (100) the page gets all blank "Loading" and only returns me the result after the script finishes running, I need a solution that allows me to show the result as the loop and passing on the screen.

I thought of using jQuery to be able to automate sending 1 by 1 and show the result I think that way the server will not overload.

I don’t know what to do now, but I need a help from at least knowing what to search for and what function to use to load the data dynamically.

The code is this one below:

 $email = explode("\n", $to);
 $headers .= "From: ".$nome." <".$de.">\r\n";
 $message = stripslashes($message);

 $i = 0;
 $count = 1;
 while($email[$i]) {
     $ok = "Ok!";
     if(mail($email[$i], $subject, $message, $headers))
       echo "Aguarde: $count <b>".$email[$i]."</b> <font color=red>Email Enviado </font> <br><hr>";
     else
       echo "Aguarde!: $count <b>".$email[$i]."</b> <font color=red>Email não enviado. </font><br><hr>";
     $i++;
     $count++;
 }
 $count--;
 if($ok == "ok")
   echo "";
  • One simple way to solve it is to execute the sending of the emails in the background using cron/schtask and, at each sending, save the result in a database. Through another script would make the queries via ajax. You can see everything in "real time" with a small delay. The problem is the huge amount of requisitions. To reduce this consumption, long-Polling techniques are used but for this case where emails have to be sent as soon as possible, it will be the same. Maybe better a rule where you expect to send 20 or 50 and then return the result of that group in a single request.

2 answers

3

If the intention is to go seeing each mail that was sent on the client side then a solution is jQuery/Javascript using AJAX to control the sending speed. For example by sending a new email to each success of the previous AJAX order, or even by creating a short waiting time between each submission.

In this case it would be necessary to have the emails in an array for example on the client side, or have only the total number of mails and send orders ajax with the number that should be sent each time.

So in PHP it would be important to export a global variable with the mails or with the maximum number:

<?php echo '<script>var totalMails = '.$total_mails.';</script>'; ?>

and in a separate PHP file (which would be to handle ajax requests):

if (!isset($_POST['nr'])) die('Erro no pedido ajax');
$i = $_POST['nr'];
if(mail($email[$i], $subject, $message, $headers))
     echo "Aguarde: $count <b>".$email[$i]."</b> <font color=red>Email Enviado </font> <br><hr>";
 else
     echo "Aguarde!: $count <b>".$email[$i]."</b> <font color=red>Email não enviado. </font><br><hr>";

And the jQuery/Javascript part would be something like:

var totalMails = 100; // isto vem do PHP
function enviarPedido(nr) {
    $.ajax({
        type: "POST",
        url: "enviador.php",
        data: {
            nr: nr
        }
    }).done(function (msg) {
        $('#div_resultado').append(msg);
        if (totalMails > 0) enviarPedido(totalMails--);
    });
}
enviarPedido(totalMails--); // assim começa em 100 e vai diminuindo até 0

0

This is a very easy example of interpretation and translation. See:

<?php
    ob_implicit_flush(true);
    set_time_limit(0);
    $results=1200;
    for($i=0;$i<1200;$i++)
    {
        ob_start();
        echo $i;
        sleep(1);
        ob_end_flush();
        ob_flush();
    }
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.