Skip to content

Instantly share code, notes, and snippets.

@wiesson
Created May 12, 2015 12:06
Show Gist options
  • Save wiesson/3a849a7de620b1eccf4e to your computer and use it in GitHub Desktop.
Save wiesson/3a849a7de620b1eccf4e to your computer and use it in GitHub Desktop.
Separate attachments from mails files (*.eml) with Python 2/3
import glob
import email
if __name__ == '__main__':
files = glob.glob("/Users/wiesson/Downloads/mails/*.eml")
for each in files:
msg = email.message_from_file(open(each))
attachments = msg.get_payload()
for attachment in attachments:
try:
fnam = attachment.get_filename()
f = open(fnam, 'wb').write(attachment.get_payload(decode=True,))
f.close()
except Exception as detail:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment