The mimify Module

The mimify module converts MIME-encoded text messages from encoded formats to plain text (typically ISO Latin 1), and back. It can be used as a command-line tool and as a conversion filter for certain mail agents:

$ mimify.py -e raw-message mime-message
$ mimify.py -d mime-message raw-message

It can also be used as a module, as shown in Example 6-12.

Example 6-12. Using the mimify Module to Decode a Message

File: mimify-example-1.py

import mimify
import sys

mimify.unmimify("samples/sample.msg", sys.stdout, 1)

Here’s a MIME message containing two parts, one encoded as quoted-printable and the other as base64. The third argument to unmimify controls whether base64-encoded parts should be decoded or not:

MIME-Version: 1.0
Content-Type: multipart/mixed; boundary='boundary'

this is a multipart sample file.  the two
parts both contain ISO Latin 1 text, with
different encoding techniques.

--boundary
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable

sillmj=F6lke! blindstyre! medisterkorv!

--boundary
Content-Type: text/plain
Content-Transfer-Encoding: base64

a29tIG5lciBiYXJhLCBvbSBkdSB09nJzIQ==

--boundary--

Here’s the decoded result (much more readable, at least if you know the language):

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary= 'boundary' this is a multipart sample file. the two parts both contain ISO Latin 1 text, with different encoding techniques. --boundary Content-Type: text/plain sillmjölke! blindstyre! medisterkorv! --boundary Content-Type: ...

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.