unmimify or ...................

Michael P. Reilly arcege at shore.net
Thu Oct 21 16:51:52 EDT 1999


Fredrik Lundh <fredrik at pythonware.com> wrote:
: tester89 at hotmail.com wrote:
:> Does anyone know if a module exists that would take an email message
:> with or without attachments and create objects for each portion of the
:> message?

: not really, but the multifile and mimetools/rfc822
: modules (all in the standard library) will take you
: a bit closer to a solution.

There is also my mimecntl module which creates consistant read/write
MIME objects.

  http://starship.python.net/~arcege/modules/mimecntl-1.2.py

For example:
  mimeobj = mimecntl.MIME_recoder(open('mailFromGuido'))
  if mimeobj.multipart:
    for attachment in mimeobj:
      attachment = attachment.decode()
      if attachment.multipart:
        print 'Warning: sorry, this program does not recurse into ' \
              'multiparts.. yet'
      else:
        if hasattr(attachment.ctype, 'name'):
          filename = attachment.ctype.name
        elif attachment.has_key('content-disposition') and \
             hasattr(attachment['content-disposition'], 'filename'):
          filename = attachment['content-disposition'].filename
        else:
          filename = None
        if filename:
          open(filename, 'wb').write(attachment.read())
          print 'Writing attachment %s' % filename
        else:
          print '-' * 40
          print attachment.read()
          print '-' * 40
  else:
    mimeobj.decode()
    print '-' * 40
    print mimeobj.read()
    print '-' * 40

In addition, there is the standard module "mailbox" which bases its
content of rfc822.Message but could be modified to return
mimetools.Message instead (unfortunately, it wasn't designed to allow a
subclass to easily change what kind of "Message" class to use).

Hope this helps.

  -Arcege





More information about the Python-list mailing list