email module in 2.2

Matthew Dixon Cowles matt at mondoinfo.com
Sat May 25 17:24:03 EDT 2002


On Sat, 25 May 2002 15:29:12 -0500, Alex Russell <alex at securepipe.com>
wrote:

Dear Alex,

>I'm re-writing an email script using the new email module introduced
>in 2.2, and I'm having some issues with multipart MIME messages.

[. . .]

>what bugs me most is that I can't seem to use get_payload to return
>JUST the text/plain sections of the message. Any pointers?

I think you're working too hard. The email module will do most of what
you want pretty painlessly. I wrote some code to do about what you
need, I'll fiddle it a bit and append it.

Regards,
Matt



import email.Parser
import os
import sys

def main():
  if len(sys.argv)<>2:
    print "Usage: %s filename" % os.path.basename(sys.argv[0])
    sys.exit(1)

  mailFile=open(sys.argv[1],"rb")
  p=email.Parser.Parser()
  msg=p.parse(mailFile)
  mailFile.close()

  for part in msg.walk():
    if part.get_type()=="text/plain":
      print part.get_payload(decode=1)
  return None

if __name__=="__main__":
  main()



More information about the Python-list mailing list