[Tutor] How to get new messages from maildir?

Alan Plum alan.plum at uni-koeln.de
Wed Nov 25 09:02:29 CET 2009


Ahoy!

On Di, 2009-11-24 at 23:47 +0000, chombee wrote:
> I'm using the standard mailbox module to read a maildir, but it seems to 
> be quite difficult to do some simple things. Is there any way to 
> identify a message as new, unread, unseen or something similar? What 
> about finding the most recent message?

AFAIR, the new (i.e. unread or unseen -- not sure which) messages should
be in Maildir/new rather than Maildir/cur. Flags are a bit trickier,
because they're simply appended to the file name, so you'd have to check
each name in the directory to figure that out. Don't get me started on
how subfolders are implemented in Maildir.

> My aim is to write a program that will print out the From: and Subject: 
> headers of new (or unread, or unseen, whatever I can get) messages, in 
> chronological order. Or failing that, just print out all messages in 
> chronological order.

The easiest(!) way would be to iterate over the folder, read the headers
(i.e. everything up to \r\n\r\n) of each message and parse them. MUAs
(Thunderbird, Evolution, Outlook etc) often don't rely on maildirs to
store that kind of meta data, they actually parse the maildir once and
then store the parsed data locally in a more usable format (i.e. some
kind of flat file database).

> As far as I can tell there's no way to do the first, and to do the 
> second you would have to use the date strings in the messages, 
> converting them to datetimes with strptime first, although on my system 
> there doesn't seem to be a valid strftime format for python that matches 
> the date strings in my emails. They end like "+0000 (GMT)", which I 
> believe is "%z (%Z)" in strftime, but python will not accept the %z in 
> the strftime pattern.

Dare I say "regular expressions"? Normally, %z should work in strftime,
though. Maybe the problem is the " (%Z)" bit -- Python might be using
different names than the MDA.


Cheers,

Alan



More information about the Tutor mailing list