Skip to content

Instantly share code, notes, and snippets.

@edwardbadboy
Created September 4, 2014 09:27
Show Gist options
  • Save edwardbadboy/273f0b165cac04b4c57f to your computer and use it in GitHub Desktop.
Save edwardbadboy/273f0b165cac04b4c57f to your computer and use it in GitHub Desktop.
Ginger Scripts
#!/usr/bin/python
import os
import mailbox
import sys
def get_plaintext_messages(mbox):
r = []
for msg in mbox.values():
txt_msg = mailbox.mboxMessage(msg)
if txt_msg.get_content_type() == 'text/plain':
r.append(txt_msg)
continue
if txt_msg.is_multipart():
sub_txt_msgs = []
for sub_msg in txt_msg.get_payload():
if sub_msg.get_content_type() == 'text/plain':
sub_txt_msgs.append(sub_msg)
if not sub_txt_msgs:
continue
txt_msg.set_payload(sub_txt_msgs)
r.append(txt_msg)
return r
if __name__ == "__main__":
if not os.path.exists(sys.argv[1]):
print 'Error:', sys.argv[1], 'does not exist.'
inbox = mailbox.mbox(sys.argv[1])
messages = get_plaintext_messages(inbox)
inbox.close()
outf = sys.argv[1] + '.clean'
if os.path.exists(outf):
print 'Error:', outf, 'already exists.'
outbox = mailbox.mbox(outf, create=True)
outbox.lock()
for msg in messages:
outbox.add(msg)
outbox.unlock()
outbox.flush()
outbox.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment