Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Screen does not refresh until all emails sent

Wes.

Active Coder
I am sending the same emails to a number of members. As there are quite a lot of them the process takes a while and I want to show some sort of progress indicator on the screen while it is going on.
I have devised a very simple indicator in which another dot is added to the text every time an email is sent. I thought this would work OK but there is a problem in that it seems the screen does not refresh until all the emails are sent, so all that happens is that the screen hangs until the process is completed and then the indicator appears with all its dots in place.
Any ideas why this is happening?

PHP:
           while ($row = mysqli_fetch_array($result)) {                                                //start of 'while' loop

                If ($row['SENDEMAIL'] == "Y") {                                                                    //only send to regular members
            
                    $dot = $dot . " . ";                                                                                           //dot for progress indicator
                    echo "<div class='thankyou' id='progress'>";
                        echo "<h style='font-size:125%; font-weight:bold;'>Emailing " . $dot;           
                    echo "</div>";
                    //die;

                    $MSGBODY = "<font face='verdana' size='2'>Hi " . $row['NAME'] . "<br><br>" . $BODY;

                    //$toaddress = $row['E_MAILADDRESS'];

                    $toaddress = "[email protected]";        //for testing

                    mail($toaddress, $subject, $MSGBODY, $headers);
                    //die;

                    }                //If ($row['SendEmail'] == "Y")

                }                //while ($row = mysqli_fetch_array($result))
 
Thanks but that seems like a big sledgehammer to crack a very small nut? 😅
I'm intrigued as to why the code seems unwilling to display anything until it has completed ALL the emailing?
 
It only displays once because PHP only outputs to the browser once. Think of an envelope and when you echo it puts a new letter in the envelope. The envelope is only delivered once, at the end of the request and contains all of the letters put inside of it. A new envelope doesn't get generated for each new letter.

If you want to show progress when sending a lot of emails, your best bet is to send say 10 at a time, then echo something like "sent emails 1 - 10", then use Javascript to do a page redirect after 5 seconds, which passes a parameter to your script which lets it know to start with email 11, then when those are sent show a new message "Sent emails 11 - 20" and then the process repeats until all emails are sent.
 
It only displays once because PHP only outputs to the browser once. Think of an envelope and when you echo it puts a new letter in the envelope. The envelope is only delivered once, at the end of the request and contains all of the letters put inside of it. A new envelope doesn't get generated for each new letter.

If you want to show progress when sending a lot of emails, your best bet is to send say 10 at a time, then echo something like "sent emails 1 - 10", then use Javascript to do a page redirect after 5 seconds, which passes a parameter to your script which lets it know to start with email 11, then when those are sent show a new message "Sent emails 11 - 20" and then the process repeats until all emails are sent.
Thanks for that very clear reply, I can understand whats happening now. I had no idea it would be that complicated so I think I will just forget it. Thanks!
 
Hello,

it is not good practice to send batch emails on single form POST.

You could try to use email_queue option.

For example, store all emails with required information's to database when sending emails (do not send them yet).

Using CRON fetch your emails in chunk (5 by 5 for example) and send them. After sending update their status in DB that email is sent.

Then, in some kind of email preview page you can refresh content each 5 seconds to see sent emails.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom