views:

483

answers:

3

Hello,

I have a single spool mbox file that was created with evolution, containing a selection of emails that I wish to print. My problem is that the emails are not placed into the mbox file chronologically. I would like to know the best way to place order the files from first to last using bash, perl or python. I would like to oder by received for files addressed to me, and sent for files sent by me. Would it perhaps be easier to use maildir files or such?

The emails currently exist in the format:

From [email protected] Fri Aug 12 09:34:09 2005
Message-ID: <[email protected]>
Date: Fri, 12 Aug 2005 09:34:09 +0900
From: me <[email protected]>
User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: someone <[email protected]>
Subject: Re: (no subject)
References: <[email protected]>
In-Reply-To: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Status: RO
X-Status: 
X-Keywords:                 
X-UID: 371
X-Evolution-Source: imap://[email protected]/
X-Evolution: 00000002-0010

Hey

the actual content of the email

someone wrote:

> lines of quotedtext

I am wondering if there is a way to use this information to easily reorganize the file, perhaps with perl or such.

A: 

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.

Keltia
Because I am wanting the file for printing, not viewing on a computer
Joshxtothe4
You want to print the mailbox as if it was a single file? If this is the intend, I'd encourage you do write a script that read every message in the mbox and print them (maybe using a template) with something like enscript or a2ps. Makes it easier and prettier.
Keltia
The mbox file already is a single file, the problem is that the emails within the single file are not in chronological order. I would prefer to keep it text based, at the moment I could send it straight to lpr: except it is not in chronological order.
Joshxtothe4
+3  A: 

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()
hop
Delivery-Date: may not be available. Date: is still the official "date" of a given mail according to the RFCs.
Keltia
you are right. i prefer to us it, because i don't want the sender's broken clock to determine my sorting order.
hop
thankyou for your reply. Is there a short hand way to print the entire message instead of just the subject? I had trouble finding information on email.get
Joshxtothe4
There also seems to be issues with unicode or \n characters not being interpreted, is there a way to set a charset or similar?
Joshxtothe4
that's what i mean with "making us do your work". you can find the documentation here: http://docs.python.org/library/mailbox.html
hop
there... i'm in a good mood today
hop
A: 

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 :(.

Denis Barmenkov