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: text/plain kom ner bara, om du törs!
Example 6-13 demonstrates that encoding messages is just as easy.
Example 6-13. Using the mimify Module to Encode a Message
File: mimify-example-2.py
import mimify
import StringIO, sys
#
# decode message into a string buffer
file = StringIO.StringIO()
mimify.unmimify("samples/sample.msg", file, 1)
#
# encode message from string buffer
file.seek(0) # rewind
mimify.mimify(file, sys.stdout)
Категории