{"id":96,"date":"2007-10-11T22:10:48","date_gmt":"2007-10-12T06:10:48","guid":{"rendered":"http:\/\/learnbyblogging.com\/?p=96"},"modified":"2007-10-11T22:32:42","modified_gmt":"2007-10-12T06:32:42","slug":"imap-email-statistics-with-php","status":"publish","type":"post","link":"https:\/\/learnbyblogging.com\/?p=96","title":{"rendered":"Imap Email Statistics with PHP"},"content":{"rendered":"<p>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: &#8220;php imap.php&#8221;)<\/p>\n<pre>\r\nDerek's INBOX Statistics:\r\nTotal number of mails = 2842\r\nMonth 2007-08 has an email count of       528   ( 15.9 MB)\r\nMonth 2007-09 has an email count of      1210   ( 76.7 MB)\r\nMonth 2007-10 has an email count of      1104   ( 79.7 MB)\r\nWeek[-8] has an email count of     81   (  3.1 MB)\r\nWeek[-7] has an email count of    186   (  5.8 MB)\r\nWeek[-6] has an email count of    200   (  5.6 MB)\r\nWeek[-5] has an email count of    198   (  6.8 MB)\r\nWeek[-4] has an email count of    298   ( 19.5 MB)\r\nWeek[-3] has an email count of    336   ( 21.4 MB)\r\nWeek[-2] has an email count of    339   ( 26.8 MB)\r\nWeek[-1] has an email count of    530   ( 33.6 MB)\r\nWeek[-0] has an email count of    674   ( 49.7 MB)\r\nTotal mail box size = 172.3 MB\r\nMaximum email size  =   2.9 MB\r\nEarliest email date = 08\/14\/2007 (58 days ago)\r\nElapsed time = 2 seconds\r\n\r\n================ imap.php ========================================\r\n&lt;?php\r\nglobal $tdate, $today;\r\n$tdate = getdate();\r\n$today = $tdate[0];\r\n$php_out = 0; \/* =1 if html format is desired. =0 for std text output *\/\r\n$max_msg_size = -10.0;\r\n$earliest_date = $tdate[0];\r\n\r\n$start_time = time(); \/* capture the start time for elapsed time calculation *\/\r\nif ($php_out) {\r\n   print \"<html><body>\\n\";\r\n   print \"&lt;h1&gt;Derek's INBOX Statistics:&lt;\/h1&gt;\\n\";\r\n}\r\nprint \"Derek's INBOX Statistics:\\n\";\r\n$total_size = 0.0;\r\n\/* Replace the following line for your application *\/\r\n$mbox = imap_open(\"{yourhost.com:993\/imap\/ssl\/novalidate-cert}INBOX\", \"username\", \"your_password\");\r\n$n_msg = imap_num_msg($mbox);\r\nif ($php_out)\r\n   echo \"&lt;h2&gt;Total number of mails = \".$n_msg.\"&lt;\/h2&gt;\\n\";\r\nelse\r\n   echo \"Total number of mails = \".$n_msg.\"\\n\";\r\n\r\nfor ($i=1; $i<=$n_msg; $i++) {\r\n  $header = imap_headerinfo($mbox,$i);\r\n  if ($header == false) {\r\n     echo \"Call failed<br>\\n\";\r\n     break;\r\n  }\r\n  else {\r\n          $size = (float) $header->Size;\r\n          if ($size > $max_msg_size)\r\n             $max_msg_size = $size ;\r\n          $msg_date=strtotime($header->Date);\r\n          if ($msg_date < $earliest_date)\r\n             $earliest_date = $msg_date;\r\n          $month_key = date('Y-m',$msg_date);\r\n          if ( is_null($msg_month[$month_key]))  {\r\n                  $msg_month[$month_key] = 0;\r\n                  \/* $msg_size[$month_key] = 0.0; *\/\r\n          }\r\n          $msg_month[$month_key] += 1;\r\n          $msg_week[getweektodate($msg_date)] += 1;\r\n          $msg_week_size[getweektodate($msg_date)] += $size;\r\n          $msg_size[$month_key] += $size;\r\n\r\n  }\r\n}\r\nforeach ($msg_month as $key => $mon ){\r\n        $size = $msg_size[$key]\/1e6;\r\n        $total_size += $size;\r\n        printf (\"Month %s has an email count of         %5d     (%5.1f MB)\\n\",$key, $mon, $size);\r\n}\r\nforeach ( $msg_week as $key => $num ) {\r\n   printf ( \"Week[-%d] has an email count of    %5d     (%5.1f MB)\\n\",$key,$num,$msg_week_size[$key]\/1e6);\r\n}\r\nprintf (\"Total mail box size = %5.1f MB\\n\",$total_size);\r\nunset ($key,$mon);\r\n$time_elapsed = time() - $start_time;\r\nprintf(\"Maximum email size  = %5.1f MB\\n\",$max_msg_size\/1e6);\r\nprintf(\"Earliest email date = %s (%d days ago)\\n\",date('m\/d\/Y', $earliest_date),($tdate[0]-$earliest_date)\/60\/60\/24)\r\n;\r\nprint (\"Elapsed time = $time_elapsed seconds\\n\");\r\nimap_close($mbox);\r\nif ($php_out)\r\n   print \"<\/body><\/html>\";\r\n\r\nfunction getdaystodate($indate) {\r\n  return ($tdate[0]-$indate)\/60\/60\/24;\r\n}\r\nfunction getweektodate($indate) {\r\n  global $today;\r\n  \/* echo  \"Indate= $indate, today = $today\\n\";  *\/\r\n  return ((int) ($today-$indate)\/60\/60\/24\/7);\r\n}\r\n?&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/learnbyblogging.com\/?p=96\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Imap Email Statistics with PHP<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-tips-for-computer"],"_links":{"self":[{"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":0,"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=\/wp\/v2\/posts\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnbyblogging.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}