Skip to content

Instantly share code, notes, and snippets.

@spapas
Created December 22, 2014 18:37
Show Gist options
  • Save spapas/d7182a98374bf14e9632 to your computer and use it in GitHub Desktop.
Save spapas/d7182a98374bf14e9632 to your computer and use it in GitHub Desktop.
Extract attachments from text mail messages
import email
import sys
if __name__=='__main__':
if len(sys.argv)<2:
print "Please enter a file to extract attachments from"
sys.exit(1)
msg = email.message_from_file(open(sys.argv[1]))
for pl in msg.get_payload():
if pl.get_filename(): # if it is an attachment
open(pl.get_filename(), 'wb').write(pl.get_payload(decode=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment