I l@ve RuBoard Previous Section Next Section

6.5 The mailbox Module

The mailbox module contains code that deals with a number of different mailbox formats (mostly Unix formats), as shown in Example 6-6. Most mailbox formats simply store plain RFC 822�style messages in a long text file, using some kind of separator line to tell one message from another.

Example 6-6. Using the mailbox Module
File: mailbox-example-1.py

import mailbox

mb = mailbox.UnixMailbox(open("/var/spool/mail/effbot"))

while 1:
    msg = mb.next()
    if not msg:
        break
    for k, v in msg.items():
        print k, "=", v
    body = msg.fp.read()
    print len(body), "bytes in body"

subject = for he's a ...
message-id = <[email protected]>
received = (from [email protected])
 by spam.egg (8.8.7/8.8.5) id CAA03202
 for effbot; Fri, 15 Oct 1999 02:27:36 +0200
from = Fredrik Lundh <[email protected]>
date = Fri, 15 Oct 1999 12:35:36 +0200
to = [email protected]
1295 bytes in body
    I l@ve RuBoard Previous Section Next Section