Mailing

Overview

This example will display a progress bar while sending huge amount of mail
It's a simulation: 5 of 500 total mails sent each page is reloaded; see lines 42,43.

This example will not have timeout. Page is reloaded between each mail process.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

And also but optional :

Explains step by step

Each page is reloaded by form (lines 118-123) submitted on body page load (lines 98-113,117). You can wait between each page reloads to let SMTP server time to do its job (see line 62).

We used the absolute positionning pattern (lines 14-18).

There are 10 cells 32x32 (lines 19-24):

Property Value Default
active-color #000084 #006600
inactive-color #3A6EA5 #CCCCCC
width 32 15
height 32 20
spacing   2
See also :

The percent label added by default is pct1 and is right side aligned on the progress bar (lines 25-31), with 'top' and 'left' attributes. Because we used the absolute position pattern (see line 15):

Property Value Default
left 350 5
top 10 5
width 0 50
height   0
align   right
valign   right
background-color   
font-size 16 11
font-family  Verdana, Tahoma, Arial
font-weight bold normal
color  #000000

While the simple text label legend (lines 34-35) is top (left) side aligned on the progress bar (lines 36-40):

Property Value Default
left 0 5
top -16 5
width   50
height   0
align   right
valign   right
background-color   
font-size  11
font-family  Verdana, Tahoma, Arial
font-weight  normal
color red #000000
See also :

Source Code

  1. <?php
  2. /**
  3. * Mailer no timeout pattern with HTML_Progress2.
  4. *
  5. * @version    $Id: mailer1.php,v 1.5 2006/05/24 08:40:35 farell Exp $
  6. * @author     Laurent Laville <pear@laurent-laville.org>
  7. * @package    HTML_Progress2
  8. * @subpackage Examples
  9. * @access     public
  10. */
  11. require_once 'HTML/Progress2.php';
  12.  
  13. $pb = new HTML_Progress2();
  14. $pb->setProgressAttributes(array(
  15.     'position' => 'absolute',
  16.     'left' => 200,
  17.     'top' => 100
  18. ));
  19. $pb->setCellAttributes(array(
  20.     'active-color' => '#000084',
  21.     'inactive-color' => '#3A6EA5',
  22.     'width' => 32,
  23.     'height' => 32
  24. ));
  25. $pb->setLabelAttributes('pct1', array(
  26.     'width' => 0,
  27.     'left' => 350,
  28.     'top' => 10,
  29.     'font-size' => 16,
  30.     'font-weight' => 'bold'
  31. ));
  32.  
  33. // Adds additional text label for process legend
  34. $labelTxtID = 'legend';
  35. $pb->addLabel(HTML_PROGRESS2_LABEL_TEXT, $labelTxtID);
  36. $pb->setLabelAttributes($labelTxtID, array(
  37.     'left' => 0,
  38.     'top' => -16,
  39.     'color' => 'red'
  40. ));
  41.  
  42. $maximum_send = 5;           // max number of emails to send each page load
  43. $total_subscribers = 500;    // total of subscribers of your newsletter
  44.  
  45. // step to advance on each page load
  46. $inc = intval($total_subscribers / $maximum_send * 0.01);
  47. $pb->setIncrement($inc);
  48.  
  49. $post = ($_SERVER['REQUEST_METHOD'] == 'POST');
  50. if ($post) {
  51.     $start_with  = (int)$_POST["start_with"];
  52.     $error_count = (int)$_POST["error_count"];
  53. } else {
  54.     $start_with  = 0;
  55.     $error_count = 0;
  56. }
  57.  
  58. $sent = 0;
  59. if ($total_subscribers >= $start_with)
  60. {
  61.     // retrieve all necessary data in the database
  62.     $pb->sleep();          // process simulation
  63.  
  64.     // if new data are available, then ...
  65.     $sent = $maximum_send;
  66.     // else, $error_count++;
  67. }
  68. $start_with += $sent;
  69.  
  70. // set the new progress value
  71. $complete = round($start_with / $total_subscribers * 100);
  72. $pb->setValue(intval($complete));
  73.  
  74. $pb->setLabelAttributes($labelTxtID, array(
  75.     'value' => sprintf('Mails sent: %s/%s', $start_with, $total_subscribers))
  76.     );
  77. ?>
  78. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  79.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  80. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  81. <head>
  82. <title>Mailer no timeout Progress2 example</title>
  83. <style type="text/css">
  84. <!--
  85. <?php echo $pb->getStyle(); ?>
  86.  
  87. body {
  88.     background-color: #E0E0E0;
  89.     color: #000000;
  90.     font-family: Verdana, Arial;
  91. }
  92.  -->
  93. </style>
  94. <script type="text/javascript">
  95. <!--
  96. <?php echo $pb->getScript(); ?>
  97.  
  98. var wait = 0// wait one second = 1000
  99.  
  100. // Pause for N milliseconds to display the progress meter
  101. function pause()
  102. {
  103.     setTimeout("submitForm();", wait);
  104. }
  105.  
  106. // Submit the form with the new value range
  107. function submitForm()
  108. {
  109.     var complete = parseInt(document.forms[0].complete.value);
  110.     if (complete < 100) {   // re-submit the form if the job is not done
  111.         document.forms[0].submit();
  112.     }
  113. }
  114. //-->
  115. </script>
  116. </head>
  117. <body onLoad="pause();">
  118. <form name="form" method="post"
  119.       action="<?php echo basename($_SERVER['PHP_SELF']) ?>">
  120. <input type="hidden" name="start_with" value="<?php echo $start_with; ?>"/>
  121. <input type="hidden" name="error_count" value="<?php echo $error_count; ?>"/>
  122. <input type="hidden" name="complete" value="<?php echo $complete; ?>"/>
  123. </form>
  124.  
  125. <?php
  126. if ($complete < 100) {
  127.     $pb->display();
  128.     $pb->moveNext();
  129. } else {
  130.     $pb->hide();
  131.     printf('<p>Mailing Process Ended with %d error(s)</p>', $error_count);
  132. }
  133. ?>
  134.  
  135. </body>
  136. </html>