|# 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: https://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 files to the same directory as this program, then run it.|
|if __name__ == '__main__':|
|if sys.version_info < 3:|
|print("Please use Python 3.")|
|if len(sys.argv) < 2:|
|print("Press enter to process all files with .txt extension.")|
|files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.txt')]|
|files = sys.argv[1:]|
|print("Files: %s" % ', '.join(files))|
|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()|
|fn_data = email.header.decode_header(fn_header)|
|(fn_str, fn_charset) = fn_data|
|if isinstance(fn_str, str):|
|fn = fn_str|
|fn = fn_str.decode(fn_charset)|
|print("Found %s" % fn)|
|print("The file %s already exists! Press enter to overwrite." % fn)|
For those of you who can't get this working, there is an alternative way posted here.
Your code really hepls me a lot. I just download all my documents from Gmail. But I have not seen the instructions for the code. You can see the origin article here:
Thanks, but I found it failed to extract Chinese name attachments like this
Do the follow changes can fix this.
try: open(fn, 'wb').write(pl.get_payload(decode=True)) except: fn = "test.rar" print("Invalid filename, just redirect to write %s" % fn) open(fn, 'wb').write(pl.get_payload(decode=True))
Hope this trick can help other guys who encounter the same issue.
I am getting following error, can you please help me
Hi @UdayKanike. I suspect your email is formatted in a way I have not encountered before.
Try adding this (use the first line to see where to put it):
for pl in msg.get_payload(): if not isinstance(pl, email.message.Message): print("Unexpected payload") continue
I was finally able to retrieve my 3-year old attachment by using Gmail's "Save to Drive", as mentioned above. Interestingly, I couldn't download it from the Drive page (it just opened a blank page with a cryptic URL) in Firefox. I tried "sharing" it, sending a link to the file to my work Outlook email. Outlook opens IE here at work to navigate to the link and, after the obligatory advice to move to Chrome, I was able to download the file, an encrypted .7z file. It was all source code. At first, I couldn't figure out why I chose to encrypt the archive, since it was just source code. But then I saw that some of the file extensions were .CMD, which I seem to recall Gmail screaming about and not allowing.
7zip allows the file names to be encrypted, so that's how I got around that. Apparently, Google has decided that any archive whose file names are encrypted must be malware designed by Russian, Chinese, or North Korean hackers.