citadel

My dotfiles, scripts and nix configs
git clone git://jb55.com/citadel
Log | Files | Refs | README | LICENSE

sortmail (472B)


      1 #!/usr/bin/env python
      2 from email.utils import parsedate, localtime, formatdate
      3 import sys
      4 import mailbox
      5 import time
      6 
      7 def extract_date(email):
      8     date = email.get('Date')
      9     parsed = parsedate(date)
     10     tval = time.mktime(parsed)
     11     email['DateLocal'] = formatdate(timeval=tval, localtime=True)
     12     return tval
     13 
     14 the_mailbox = mailbox.mbox(sys.argv[1])
     15 sorted_mails = sorted(the_mailbox, key=extract_date)
     16 the_mailbox.update(enumerate(sorted_mails))
     17 the_mailbox.flush()