Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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:
# Instructions:
# 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.
import email
import sys
import os
if __name__ == '__main__':
if sys.version_info[0] < 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()
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
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)
open(fn, 'wb').write(pl.get_payload(decode=True))
Copy link

ntung commented Jun 10, 2015

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:

Copy link

ssivanatarajan commented Feb 7, 2016

wow works perfectly recovered my rar file . Thanks

Copy link

thiagoh commented Mar 10, 2016

man!! it works perfectly!! thank u!!

Copy link

ethanpayne commented Mar 24, 2016

Also you can download the attachment using another client such as

Copy link

zrod commented Mar 24, 2016

Great, worked just fine, thanks!

Copy link

huxingyi commented Apr 13, 2016

Thanks, but I found it failed to extract Chinese name attachments like this Content-Type: application/octet-stream; name="=?GB2312?B?c2RoKDUuMTcuMjMuNDbQ3tX9z9C80rP2wazM9LrzxuTL/M/QvNK15sXG?= =?GB2312?B?tO3O8ykucmFy?=" Content-Disposition: attachment; filename="=?GB2312?B?c2RoKDUuMTcuMjMuNDbQ3tX9z9C80rP2wazM9LrzxuTL/M/QvNK15sXG?= =?GB2312?B?tO3O8ykucmFy?=".

Do the follow changes can fix this.

            open(fn, 'wb').write(pl.get_payload(decode=True))
            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.

Copy link

stefansundin commented Apr 13, 2016

@huxingyi I updated the code so it should decode filenames properly now. Give it a try.

Copy link

nielsaust commented Apr 26, 2016

This is so awesome. I thank you!

Copy link

jeanadam commented Nov 10, 2016

Still working great! thanks!

Copy link

kavunshiva commented Feb 13, 2017

Dope! This worked like a charm. Yayyy, Python modules for everything, and thanks @stefansundin

Copy link

UdayKanike commented Mar 7, 2017


I am getting following error, can you please help me

udays-MacBook-Pro:mystuff udaykumarkanike$ python 0.txt
Files: 0.txt
Processing 0.txt
Subject: None
Traceback (most recent call last):
  File "", line 27, in <module>
    fn_header = pl.get_filename()
AttributeError: 'str' object has no attribute 'get_filename'
udays-MacBook-Pro:mystuff udaykumarkanike$ 


Copy link

stefansundin commented Mar 8, 2017

Hi @UdayKanike. I suspect your email is formatted in a way I have not encountered before. msg.get_payload() seems to have returned a string.

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")

Copy link

devlifealways commented Apr 20, 2017

Worked perfectly, thanks (y)

Copy link

me-suzy commented May 17, 2017

EOF problem on notepad, see this print screen:

Copy link

stefansundin commented May 17, 2017

@me-suzy it appears you are using Python 2.7. Please run this script with Python 3.

Copy link

tejasshah93 commented Jul 27, 2017

Works perfectly! Thanks 👍

Copy link

NadeemBaloch commented Aug 12, 2017

Where should i place that particular file ?

Copy link

stefansundin commented Aug 12, 2017

@NadeemBaloch Place the .py file and your emails (.txt files) in the same directory. Then run the python script.

Copy link

wdlv commented Sep 20, 2017

Thanks a lot! It works like a charm!

Copy link

guimeira commented Sep 25, 2017

For all the python noobs like me out there: do not save the script as

Copy link

tolew1 commented Oct 8, 2017

Genius, this is great and works perfectly. Google and their omnipotence blocks common extensions like JS in zip files now and don't provide an override option. time to for another email.

Copy link

riavalon commented Nov 21, 2017

Really terrific, thanks for this! Had some files from old personal website I saved in my email a loooong time ago and couldn't download them cause of this "antivirus" thing. This script worked perfectly, though

Copy link

bekharsky commented Dec 9, 2017

Works like a charm. Thank you!

Copy link

TonyStark commented Dec 23, 2017

no words man
very very thank you :)

Copy link

ZenMagus commented Feb 20, 2018

this worked awesome -- thank you -- I'm currently studying Python so am super happy with this.

Copy link

dan400man commented Mar 20, 2018

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.

Copy link

heiniovason commented Jul 4, 2018

This script saved my ass. Thanks a bunch!

Copy link

akhileshdarjee commented Sep 27, 2018

Awesome, it works :)

Copy link

s-steephan commented Apr 5, 2019

Working fine. Thanks. !!

Copy link

Sneidon commented Jun 18, 2019

Thanks, code works. If you are the one who sent the email, you can save the email and open using Outlook etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment