How can I reorder an mbox file chronologically?

可紊 提交于 2019-12-04 14:21:58

This is how you could do it in python:

#!/usr/bin/python2.5
from email.utils import parsedate
import mailbox

def extract_date(email):
    date = email.get('Date')
    return parsedate(date)

the_mailbox = mailbox.mbox('/path/to/mbox')
sorted_mails = sorted(the_mailbox, key=extract_date)
the_mailbox.update(enumerate(sorted_mails))
the_mailbox.flush()

Python solution wont work if mail messages was imported into mbox using Thunderbird's ImportExportTools addon. There are a bug: messages shall prefix with 'from' line in format:

From - Tue Apr 27 19:42:22 2010

but ImportExportTools prefix with such 'from' line:

From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time)

So there are two errors:

  1. sequence 'time year' broken into 'year time'
  2. extra trash with GMT info along with time zone name

Since Python's mailbox.py/UnixMailbox has hardcoded regexp for 'from' line matching, some of messages can't parsed.

I wrote the error message to the author, but there are many mistakenly imported messages :(.

What's the point in rewriting the mbox whereas you can reorder the mails in memory when loading up the mailbox? Which time is the one you want to order on? Receive date? Sent date? Anyway, all the Ruby/Python/Perl modules for playing with mboxes can do that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!