#!/usr/bin/python # written 2003 by Ortwin Glück # This is free software. Free as in "freedom" and "free beer". # # Strips all attachments from an email message # Expects the message on stdin and writes the # output to stdout import email import sys # recursively strips non-text mime parts from a multipart message # returns true if message is multipart and does not contain any parts def strip(msg): # prerequisite if not msg.is_multipart(): return False todelete = [] # will hold indices to delete # enumerate the parts parts = msg.get_payload() i = 0 for part in parts : ct = part.get_content_type() # check for unsafe content types if not ct.startswith('text/') and \ not ct.startswith('message/') and \ not part.is_multipart() : # mark for deletion (to avoid iterator mess) todelete.append(i) # recurse if strip(part) : todelete.append(i) i = i + 1 # now do the actual delete for i in todelete : del parts[i] return (len(parts) == 0) # -- main msg = email.message_from_file(sys.stdin) # read from stdin strip(msg) print msg.as_string(True);