Skip to content

Instantly share code, notes, and snippets.

@cazacugmihai
Forked from stefansundin/extract-attachments.py
Created September 16, 2017 13:39
Show Gist options
  • Save cazacugmihai/f9493426c1fb48e2f8a61ad82d615f88 to your computer and use it in GitHub Desktop.
Save cazacugmihai/f9493426c1fb48e2f8a61ad82d615f88 to your computer and use it in GitHub Desktop.
Extract attachments from emails that Gmail doesn't allow you to download. This is dumb. Please use Python >= 3.4.
#!/usr/bin/env python3
# Get your files that Gmail block. Warning message:
# "Anti-virus warning - 1 attachment contains a virus or blocked file. Downloading this attachment is disabled."
# Based on: http://spapas.github.io/2014/10/23/retrieve-gmail-blocked-attachments/
# Go to your emails, click the arrow button in the top right, "Show original", then "Download Original". Move the file to the same directory as this script.
import email
import sys
import os
if __name__ == '__main__':
if sys.version_info[0] < 3:
print("Please use Python 3.")
sys.exit()
if len(sys.argv) < 2:
print("Press enter to process all files with .txt extension.")
input()
files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.txt')]
else:
files = sys.argv[1:]
print("Files: %s" % ', '.join(files))
print()
for f in files:
msg = email.message_from_file(open(f))
print("Processing %s" % f)
print("Subject: %s" % msg['Subject'])
for pl in msg.get_payload():
fn_header = pl.get_filename()
if fn_header:
fn_data = email.header.decode_header(fn_header)
(fn_str, fn_charset) = fn_data[0]
if isinstance(fn_str, str):
fn = fn_str
else:
fn = fn_str.decode(fn_charset)
print("Found %s" % fn)
if os.path.isfile(fn):
print("The file %s already exists! Press enter to overwrite." % fn)
input()
open(fn, 'wb').write(pl.get_payload(decode=True))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment