<?php
 require("dbase.inc");
 $lnk = mysql_connect("$hostname", "$user", "$password");
 $All = mysql_db_query ($db, "
          SELECT         
             DATE_FORMAT(t_stamp, 'm/d/Y h:i:s') as frmt_stamp, 
             DATE_FORMAT(NOW(), 'm/d/Y') as frmmaxt_stamp,
             DATE_FORMAT(min(t_stamp), 'm/d/Y') as frmmint_stamp,
             TO_DAYS(NOW())+1-TO_DAYS(min(t_stamp)) as totaldays
          FROM
             $table
          GROUP BY 
             t_stamp
          ORDER
             BY t_stamp", $lnk);
 $totalmails = mysql_num_rows($All);
 $Entries = mysql_db_query($db, "
          SELECT 
             *,
             DATE_FORMAT(t_stamp, 'm/d/Y h:i:s') as frmt_stamp
          FROM 
             maillog
          ORDER BY
             t_stamp
             DESC
          LIMIT 75", $lnk);
 $Recipient = mysql_db_query($db, "
          SELECT 
             recipient,
             count(recipient) as qnt 
          FROM
             maillog
          GROUP BY
             recipient 
          ORDER BY
             qnt", $lnk);
?>
<head>
<title>phpEasyMail Statistics</title>
<link rel="stylesheet" href="styles.css"></head>

<body bgcolor="#FFFFFF">
<div align="center"> 
  <h1>phpEasyMail</h1>
  <?php $rowAll = mysql_fetch_array($All);?>

  <p align="center"><b>Statistics for <?php echo $rowAll[frmmint_stamp];?> to <?php echo $rowAll[frmmaxt_stamp];?>:</b></p>
  <table border="0" align="center">
    <tr> 
      <td>Total mails sent: </td>
      <td><?php echo $totalmails;?></td>
    </tr>
    <tr> 
      <td>Avg. mails per day:</td>
      <td><?php 
          $totaldays = $rowAll[totaldays];
          echo number_format($totalmails/$totaldays, 2);
          ?></td>
    </tr>
       <?php 
        while ($rowRecipient = mysql_fetch_array($Recipient))
              {
              echo " <tr>\n";
              $percent = 100 * $rowRecipient[qnt] / $totalmails;
              $percent = number_format($percent,2);
              $percentoff = 100 - $percent;
              echo "  <td>$rowRecipient[recipient]</td><td><img src=\"percent.gif\" height=\"4\" width=\"$percent\"><img src=\"percentoff.gif\" height=\"4\" width=\"$percentoff\"> $percent% ($rowRecipient[qnt])</td>\n";
              echo " </tr>\n";
              }
       ?> 
  </table>
  <table border="1" align="center">
  <tr>
   <th>Time</th><th>Remote Host</th><th>Sender</th><th>Recipient</th><th>Subject</th>
  </tr>
  <?
    while ($row = mysql_fetch_array($Entries))
          {
          print "   <tr>\n";
          print "    <td>$row[frmt_stamp]</td><td>$row[host]</td><td>$row[sender]</td><td>$row[recipient]</td><td>$row[subject]</td>\n";
          print "   </tr>\n";
          }
  ?>
  </table>
</div>
</body>
</html>
