Mailer

Overview

This example will display a progress bar while sending huge amount of mail
Its a simulation: 5 of 500 total mails sent each page is reloaded; see lines 58,59.

Screenshot

sample screenshot

Demonstration

Give it a try

Dependencies

This example requires mandatory resources :

Explains step by step

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

Each page is reloaded by form (lines 127-132) submitted on body page load (lines 110-122,126). You can wait between each page reloads to let SMTP server time to do its job (see line 107 or also lines 18-25,74).

We used the absolute position pattern (default option; lines 27-33).

The plain bar is rendered by options (lines 34-37):

Property Value Default
color #FF6633 #0033FF
background-color yellow #C0C0C0
See also :

The percent label added is pct1 and is right side aligned on the progress bar (lines 38,39-46):

Property Value Default
left 570 5
top 250 5
width 0 50
height   0
align   right
background-color   
font-size 24 11
font-family  Verdana, Tahoma, Arial
font-weight normalnormal
color navy #000000

While the simple text label legend (lines 49-50) is top (left) side aligned on the progress bar (lines 51-56):

Property Value Default
left   0
top 220 -16
width   0
height   0
align   right
background-color   
font-size 14 11
font-family  Verdana, Tahoma, Arial
font-weight bold normal
color navy #000000
See also :

Source Code

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