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

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

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

Entry

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, http://starship.python.net/~arcege/modules/index.html


Use the email module that's available in Python 2.2 or later:
http://www.python.org/doc/current/lib/module-email.html
For Python 2.1, you'll need to install it as an extra package from
http://sourceforge.net/projects/mimelib
What follows is the old version of this answer that might apply to
ancient versions of Python ;-)
Goto
    http://sun.med.ru/~phd/Software/Python/misc.html
and download
    extract_mime.tgz
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
    mmsg.push(boundary)
    while 1:
        mmsg.next()
        if mmsg.last: 
            break
        mime_item = mimetools.Message(mmsg)
        decode_item(mime_item)
also check out http://starship.python.net/~arcege/modules/index.html
Mimecntl is a nice way to build and read MIME docs.