Create a gist now

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: 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()

Pls how do I retrieve my file because am having the same problem

Owner

stefansundin commented Jan 3, 2015

@Aniagohuche what part of the instructions are you having problems with?

renema commented Mar 17, 2015

Thank you so much, worked perfectly!

Thank you.

i have done all the process as u told above but after step 32 also my files are not yet extracted and i am not getting what should i do further, so please help me out, in what way i could get my files

Owner

stefansundin commented Apr 12, 2015

For those of you who can't get this working, there is an alternative way posted here.

Update, 12 January 2015: Ivana (at the comments section) proposed a different solution that may work for some files: Use a mobile Gmail client (I tested it with Android) and "Save to Drive" your attachment. You’ll then be able to download it from the Google Drive!

Thank you

Many thanks mate

Works perfectly with python 2.7, I recover my rar archive. Thanks !

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:
http://spapas.github.io/2014/10/23/retrieve-gmail-blocked-attachments/

wow works perfectly recovered my rar file . Thanks

thiagoh commented Mar 10, 2016

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

Thanks!
Also you can download the attachment using another client such as Mail.app

zrod commented Mar 24, 2016

Great, worked just fine, thanks!

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.

        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.

Owner

stefansundin commented Apr 13, 2016

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

This is so awesome. I thank you!

Still working great! thanks!

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

Hi,

I am getting following error, can you please help me

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

Thanks
Uday

Owner

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

Worked perfectly, thanks (y)

me-suzy commented May 17, 2017

EOF problem on notepad, see this print screen:

https://snag.gy/GFLYU6.jpg

Owner

stefansundin commented May 17, 2017

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

Works perfectly! Thanks 👍

Where should i place that particular file ?

Owner

stefansundin commented Aug 12, 2017

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

Thanks a lot! It works like a charm!

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

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.

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