File: class/Workbook/Examples/Lecture14/decodeAll.py

#!/usr/bin/env python

#####################################################
# Decode mail attachments sent in base64 encoded
# form (and others). Rather than editing by hand,
# this version reads the entire mail message
# and fetches, decodes and writes each individual
# part to its own file.  It doesn't assume that
# the base64 encoded data has been extracted into
# a separate file.  Decodes uuencoded attachments 
# too.  mhlib uses mimetools.Message, multifile,
# and mimetools.decode.
#
# To use, copy mail message to mailfile and run:
#    % python ..\decodeAll.py mailfile
# which makes one or more mailfile.part* outputs.
# Also supports nested multipart encoding (parts
# within parts, e.g., text and html alternatives).
# On some platforms you can also simply point an 
# email client like Outlook at the file (e,g., on
# Windows, give the file a .eml extension and click)
# but this script is more portable, and may be more
# convenient especially if there are many parts.
#####################################################

import sys, mhlib
from types import *
iname = 'mailmessage.txt'

if len(sys.argv) == 3:
    iname, oname = sys.argv[1:]        # % python prog [iname [oname]?]?
elif len(sys.argv) == 2:
    iname = sys.argv[1]
    oname = iname + '.part'

def writeparts(part, oname):
    global partnum
    content = part.getbody()                   # decoded content or list
    if type(content) == ListType:              # multiparts: recur for each
        for subpart in content:
            writeparts(subpart, oname) 
    else:                                      # else single decoded part
        assert type(content) == StringType     # use filename if in headers
        print; print part.getparamnames()      # else make one with counter
        fmode = 'wb'
        fname = part.getparam('name')
        if not fname:
            fmode = 'w'
            fname = oname + str(partnum)
            if part.gettype() == 'text/plain':
                fname = fname + '.txt'
            elif part.gettype() == 'text/html':
                fname = fname + '.html'
        output = open(fname, fmode)            # mode must be 'wb' on windows
        print 'writing:', output.name          # for word doc files, not 'w'
        output.write(content)
        partnum = partnum + 1

partnum = 0
input   = open(iname, 'r')                     # open mail file
message = mhlib.Message('.', 0, input)         # folder, number args ignored
writeparts(message, oname)
print 'done: wrote %s parts' % partnum



[Home page] Books Code Blog Python Author Train Find ©M.Lutz