Imap Email Statistics with PHP

Below is a PHP script that I run daily (via a cron job) to check my imap email statistics. It generates a report like below. I use this statistic report to tell me how behind I am on my emails. It also reports a distribution of the emails for the last few weeks. You can run it on the web browser or run it on php command line (command: “php imap.php”)

Derek's INBOX Statistics:
Total number of mails = 2842
Month 2007-08 has an email count of       528   ( 15.9 MB)
Month 2007-09 has an email count of      1210   ( 76.7 MB)
Month 2007-10 has an email count of      1104   ( 79.7 MB)
Week[-8] has an email count of     81   (  3.1 MB)
Week[-7] has an email count of    186   (  5.8 MB)
Week[-6] has an email count of    200   (  5.6 MB)
Week[-5] has an email count of    198   (  6.8 MB)
Week[-4] has an email count of    298   ( 19.5 MB)
Week[-3] has an email count of    336   ( 21.4 MB)
Week[-2] has an email count of    339   ( 26.8 MB)
Week[-1] has an email count of    530   ( 33.6 MB)
Week[-0] has an email count of    674   ( 49.7 MB)
Total mail box size = 172.3 MB
Maximum email size  =   2.9 MB
Earliest email date = 08/14/2007 (58 days ago)
Elapsed time = 2 seconds

================ imap.php ========================================
<?php
global $tdate, $today;
$tdate = getdate();
$today = $tdate[0];
$php_out = 0; /* =1 if html format is desired. =0 for std text output */
$max_msg_size = -10.0;
$earliest_date = $tdate[0];

$start_time = time(); /* capture the start time for elapsed time calculation */
if ($php_out) {
   print "\n";
   print "<h1>Derek's INBOX Statistics:</h1>\n";
}
print "Derek's INBOX Statistics:\n";
$total_size = 0.0;
/* Replace the following line for your application */
$mbox = imap_open("{yourhost.com:993/imap/ssl/novalidate-cert}INBOX", "username", "your_password");
$n_msg = imap_num_msg($mbox);
if ($php_out)
   echo "<h2>Total number of mails = ".$n_msg."</h2>\n";
else
   echo "Total number of mails = ".$n_msg."\n";

for ($i=1; $i<=$n_msg; $i++) {
  $header = imap_headerinfo($mbox,$i);
  if ($header == false) {
     echo "Call failed
\n"; break; } else { $size = (float) $header->Size; if ($size > $max_msg_size) $max_msg_size = $size ; $msg_date=strtotime($header->Date); if ($msg_date < $earliest_date) $earliest_date = $msg_date; $month_key = date('Y-m',$msg_date); if ( is_null($msg_month[$month_key])) { $msg_month[$month_key] = 0; /* $msg_size[$month_key] = 0.0; */ } $msg_month[$month_key] += 1; $msg_week[getweektodate($msg_date)] += 1; $msg_week_size[getweektodate($msg_date)] += $size; $msg_size[$month_key] += $size; } } foreach ($msg_month as $key => $mon ){ $size = $msg_size[$key]/1e6; $total_size += $size; printf ("Month %s has an email count of %5d (%5.1f MB)\n",$key, $mon, $size); } foreach ( $msg_week as $key => $num ) { printf ( "Week[-%d] has an email count of %5d (%5.1f MB)\n",$key,$num,$msg_week_size[$key]/1e6); } printf ("Total mail box size = %5.1f MB\n",$total_size); unset ($key,$mon); $time_elapsed = time() - $start_time; printf("Maximum email size = %5.1f MB\n",$max_msg_size/1e6); printf("Earliest email date = %s (%d days ago)\n",date('m/d/Y', $earliest_date),($tdate[0]-$earliest_date)/60/60/24) ; print ("Elapsed time = $time_elapsed seconds\n"); imap_close($mbox); if ($php_out) print ""; function getdaystodate($indate) { return ($tdate[0]-$indate)/60/60/24; } function getweektodate($indate) { global $today; /* echo "Indate= $indate, today = $today\n"; */ return ((int) ($today-$indate)/60/60/24/7); } ?>

2 thoughts on “Imap Email Statistics with PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.