Error on mimetools?

Steve Holden sholden at holdenweb.com
Mon Aug 6 07:52:57 EDT 2001


"Arnulf Heimsbakk" <arnulf at spirea.net> wrote in message
news:9k368v$a8q$1 at news.uit.no...
> I'm trying to decode a mime64 message in python with mimetools. I have
> tested the "body" with windows winzip. It decodes correctly. When I try to
> decode it in python, it do not decode correctly. I get a file with
slightly
> increased size. The heading of the file seems the same, but when I test
this
> with a jpg file - the file gets corrupted when decoded with python's
> mimetools.
>
> Is there a error in mimetools? Or is my approach entirly incorrect. My
code
> is below:
>
> out = StringIO()
> mimetools.decode(StringIO(self.body), out,  'base64')
> return out.getvalue()
>
> I preciate any advise or help I can get.

The body of an RFC822/2822 message with attachments is a compound object.
Have you first extracted the particular component you want from the body, or
are you applying the .decode() method to the whole body?

Here's some code (using a non-standard mailhandler module, but you should be
able to use mailbox for your purposes) that shows you how it's done:

import mailhandler
import multifile, mimetools, sys

MFILE = "mailbox.txt"


class mailStream:

    def __init__(self, filename):
        try:
            self.fp = open(filename, "r")
            print "+++Opened", filename
        except IOError:
            sys.exit("Could not open mailfile '%s'" % filename)
        self.mb = mailhandler.MimeMailbox(self.fp)

    def next(self):
        ptr = self.fp.tell()        # save start point
        msg = self.msg = self.mb.next()   # read next from mailbox
        atts = self.atts = []
        if msg:
            boundary = msg.getparam("boundary")
            if 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
                    atts.append(mimetools.Message(mf))
                                    # read up to next boundary
                mf.pop()            # restore previous
            return msg, atts        # return message and attachments
        else:
            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
        break
    m += 1                  # bump count
    if atts:
        a = 0
        print "Mail %d: multipart with %d attachments" % (m, len(atts))
        for att in atts:
            a += 1
            print "Att", a, "Type: ", att.gettype(), \
                    "encoding:", att.getencoding(),
            print "File:", att.getparam("name")
    else:
        print "Mail %d: plain message" % m, "from", msg['from']
    print "---------------------------------------------"

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list