Advertisement
teknoraver

strip attachments

Aug 4th, 2015
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. def sanitise(msg):
  4.     fn = msg.get_filename()
  5.     if fn:
  6.         msg.set_payload("Attachment stripped"
  7.                 "\nFilename: " + fn +
  8.                 "\nContent-Type: " + msg.get_content_type())
  9.         for k, v in msg.get_params()[1:]:
  10.             msg.del_param(k)
  11.         msg.set_type('text/plain')
  12.         del msg['Content-Transfer-Encoding']
  13.         del msg['Content-Disposition']
  14.     else:
  15.         if msg.is_multipart():
  16.             msg.set_payload([ sanitise(x) for x in msg.get_payload() ])
  17.     return msg
  18.  
  19. import mailbox, email, sys
  20.  
  21. reload(sys)
  22. sys.setdefaultencoding('utf-8')
  23.  
  24. mbox = mailbox.mbox(sys.argv[1], factory=None, create=False)
  25. for k, m in mbox.iteritems():
  26.     print(sanitise(m).as_string())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement