Parsing mime multipart/alternative emails for the message body
sholden at holdenweb.com
Tue Jan 22 17:57:14 CET 2002
"Joao Prado Maia" <JMaia at lexgen.com> wrote ...
> I'm trying to parse mime based emails to get the message body, but I
> have much luck on trying to find a way. To give a little bit more detail,
> what I'm trying to do is from a mime multipart email message get only the
> message body. And by that I mean that I want only the message body, and
> the whole mime envelope for the other parts.
> Using the modules mimify and rfc822 I can get the body, but it contains
> other parts as well. If the message was multipart/alternative with a part
> text/plain and the other text/html, I would get both parts on the
> rfc8222.Message().fp.read() way of doing this.
You can use the multifile module to treat the message body as a sequence of
> What I want is either just the text/plain body message on a
> multipart/alternative or the text/html one if I don't have the choice.
> Anyway, I looked into the new email module of Python 2.2 but I couldn't
> figure out how to do this, if it is even possible.
> Any suggestions would be greatly appreciated.
This example (from p. 157 of "Python Web Programming") shows one way of
getting the attachements from a mail message. The mailhandler module was
just a way of getting around restrictions in older versions of the rfc822
and mailbox libraries.
import multifile, mimetools, sys
MFILE = "mailbox.txt"
def __init__(self, filename):
self.fp = open(filename, "r")
print "+++Opened", filename
sys.exit("Could not open mailfile '%s'" % filename)
self.mb = mailhandler.MimeMailbox(self.fp)
ptr = self.fp.tell() # save start point
msg = self.msg = self.mb.next() # read next from mailbox
atts = self.atts = 
boundary = msg.getparam("boundary")
mf = multifile.MultiFile(self.fp)
# create Multifile
mf.push(boundary) # save for recognition
self.fp.seek(ptr) # point to multifile start
while mf.next(): # each message
# read up to next boundary
mf.pop() # restore previous
return msg, atts # return message and attachments
return None, None # no message
m = 0
ms = mailStream(MFILE) # create the message stream
while 1: # forever
msg, atts = ms.next() # get next message
if msg is None: # quit if there's nothing
m += 1 # bump count
a = 0
print "Mail %d: multipart with %d attachments" % (m, len(atts))
for att in atts:
a += 1
print "Att", a, "Type: ", att.gettype(), \
print "File:", att.getparam("name")
print "Mail %d: plain message" % m, "from", msg['from']
Hope this helps.
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/
More information about the Python-list