[Tutor] Decoding MIME Attachments

jneiss at neisskids.org jneiss at neisskids.org
Fri Apr 2 00:47:42 CEST 2010

 Hi, all;

 I am a longtime linux sysadmin that is fairly new to Python. I've got a
project for which Python seems to be perfect (or, at least, I've found a
way to integrate my learning :-)

 I receive log files via email from an outside vendor. We use Splunk to
generate reports based on these and other logs. At the moment, pulling
the emailed logs into Splunk is done by hand--open the email, save the
attachment, read the file in. I'd like to automate it, and procmail + a
script is the easiest way. I've found perl scripts that supposedly pull
MIME-encoded attachments, but not being a perl guy, I'm trying to use
Python instead.

 I have the following script--cobbled together from Internet searches, the
Python cookbook, and hand-written lines:

#! /usr/bin/env python

import email.Parser
import os, sys
def main(argv = sys.argv):
      if not sys.stdin.isatty():
              for m in sys.stdin.readlines():
                p = email.Parser.Parser()
                msg = p.parse(m)
                partcounter = 1
                for part in msg.walk():
                        if part.get_content_maintype() == "multipart":
                        name = part.get_param("name")
                        if name == None:
                                name = "part-%i" % partcounter
                        partcounter += 1
                        print "partcounter = %s" % partcounter
                        if name != "part-1":
                                outfile = open(name, "wb")
if __name__=="__main__":
        except KeyboardInterrupt:

 The gist is: read whatever's piped to the script (from procmail or on the
command line), walk through the data looking for the second MIME header,
and write that part (the file attachment) to disk.

 What actually happens is this:

[user at server scripts]# cat /tmp/mail.txt | ./mail_extract.py
Traceback (most recent call last):
  File "./mail_extract.py", line 32, in ?
  File "./mail_extract.py", line 12, in main
    msg = p.parse(mailfile)
  File "/usr/lib64/python2.4/email/Parser.py", line 65, in parse
    data = fp.read(8192)
AttributeError: 'str' object has no attribute 'read'
[user at server scripts]#

f I change the three lines starting with "def main" to:

def main():
     if len(sys.argv) != 2:
             print "Usage: %s filename" % os.path.basename(sys.argv[0])

and remove the last five lines (if...pass), changing the command line to
"./mail_extract.py /tmp/mail.txt", it works like a champ. As is, it errors
whether through procmail or directly from the command line.

 Any ideas? What am I doing wrong?

 I'm using Python 2.4.3 on Red Hat ES 5.3, if it matters.


 jason at neisskids.org
 gentoo linux, as if you cared
 nac mac feegle!

More information about the Tutor mailing list