import sys
import os
import time
import getopt
import shutil
INDENT = \' \'
help_message = \'\'\'
./mail_info.py --maildir <mail dir>
Looks at OSX local users mail store and reports some stats
--maildir - point to an imap folder for your email account
i.e.
./mail_info.py --maildir "/Users/rob/Library/Mail/IMAP-rob@foo.com"
\'\'\'
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def splitthousands(s, sep=\',\'):
if len(s) <= 3:
return s
return splitthousands(s[:-3], sep) + sep + s[-3:]
def main(argv=None):
version = None
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hm:", ["help", "maildir="])
except getopt.error, msg:
raise Usage(msg)
maildir = None
# option processing
for option, value in opts:
if option in ("-h", "--help"):
raise Usage(help_message)
if option in ("-m", "--maildir"):
maildir = value
except Usage, err:
print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> sys.stderr, "\\t for help use --help"
return 2
if not maildir:
raise Usage("--maildir required")
emails_processed = 0
iphone_emails = 0
mime_version = 0
email_to = 0
for root, dirs, files in os.walk(os.path.join(maildir, "Sent Messages.imapmbox","Messages")):
for afile in files:
emails_processed += 1
email_to_found = False
for line in open(os.path.join(root, afile), \'r\'):
if not email_to_found:
if line.find("To: ") != -1:
email_to += 1
email_to_found = True
if line.find("Mime-Version") != -1:
mime_version += 1
if line.find(\'iPhone Mail\') != -1:
iphone_emails += 1
continue
print "Emails Sent:",splitthousands(str(emails_processed))
print "iPhone Emails:",splitthousands(str(iphone_emails)),"(%.2f%%)" % ((100.0*iphone_emails)/emails_processed)
if __name__ == \'__main__\':
main()