The mimetools Module

The Multipurpose Internet Mail Extensions (MIME) standard defines how to store non-ASCII text, images, and other data in RFC 822-style messages.

The mimetools module, shown in Example 6-3, contains a number of tools for writing programs that read or write MIME messages. Among other things, it contains a version of the rfc822 module’s Message class, which knows a bit more about MIME encoded messages.

Example 6-3. Using the mimetools Module

File: mimetools-example-1.py

import mimetools

file = open("samples/sample.msg")

msg = mimetools.Message(file)

print "type", "=>", msg.gettype()
print "encoding", "=>", msg.getencoding()
print "plist", "=>", msg.getplist()

print "header", "=>"
for k, v in msg.items():
    print "  ", k, "=", v

type => text/plain
encoding => 7bit
plist => ['charset="iso-8859-1"']
header =>
   mime-version = 1.0
   content-type = text/plain;
 charset="iso-8859-1"
   to = effbot@spam.egg
   date = Fri, 15 Oct 1999 03:21:15 -0400
   content-transfer-encoding = 7bit
   from = "Fredrik Lundh" <fredrik@pythonware.com>
   subject = By the way...
...

Get Python Standard Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.