Online Shopping : Computers : Programming : Languages : PHP : Common Problems : Files : Tips and Tricks

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

21 of 25 people (84%) answered Yes
Recently 4 of 8 people (50%) answered Yes


How can I read and interpret email attachments using Python?
How can I read and interpret email attachments using Python?

Jul 18th, 2002 08:12
Gerhard Haering, Shae Erisson, Sean Blakey, Nathan Wallace, unknown unknown,

Use the email module that's available in Python 2.2 or later:
For Python 2.1, you'll need to install it as an extra package from
What follows is the old version of this answer that might apply to
ancient versions of Python ;-)
and download
There you'll find an example and some docs.  Just feed a MIME message
into the program...
Assuming you have a file pointer to the message, you can create a 
mimetools.Message object to access a multipart mime message.  Here is a 
snippet of code:
def decode_item(item):
    Decode an indevidual mime item
    item should be a mimetools.Message object.
    encoding = item.getencoding()
    name = item.getparam('name')
    print 'Decoding to', name
    outfile = open(name, 'wb')                # Write the decoded
                                              # Attatchment to a file
    mimetools.decode(item.fp, outfile, encoding)
def decode_multipart(datafile):
    Decode a multipart MIME message.
    datafile is the filepointer to a MIME mesage.
    msg = mimetools.Message(datafile)
    boundary =  msg.getparam('boundary')      # The boundry between
                                              # Message parts
    encoding = msg.getencoding()
    if encoding != '7bit':
        raise 'Unknown encoding!', encoding
    if msg.gettype() != 'multipart/mixed':
        raise 'Message is not multipart!'     # This could probably be
                                              # made more robust
    mmsg = multifile.MultiFile(msg.fp)        # Abstract the message as
                                              # A multifile object
    while 1:
        if mmsg.last: 
        mime_item = mimetools.Message(mmsg)
also check out
Mimecntl is a nice way to build and read MIME docs.