ex05.py

ex05.py — Python Source, 0Kb

ファイルコンテンツ

#!/usr/bin/env python
# ex05.py
# parse multipart MIME message
from email import message_from_file
from email.Header import decode_header
import Image
import StringIO

msg = message_from_file(file('sample.msg'))
subject = decode_header(msg['subject'])
print 'Subject:',
for x in subject:
    print unicode(x[0], x[1] or 'us-ascii'),
print
for part in msg.walk():
    print part.get_content_type()
    if part.get_content_type() == 'text/plain':
        s = part.get_payload(decode=True)
        u = unicode(s, part.get_content_charset())
        print u
    if part.get_content_maintype() == 'image':
        s = StringIO.StringIO(part.get_payload(decode=True))
        img = Image.open(s)
        img.show()