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. (Only tested with 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", save to the same directory as this script.
import email
import sys
import os
if __name__ == '__main__':
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()
@Aniagohuche

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

@stefansundin
Owner

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

@renema
renema commented Mar 17, 2015

Thank you so much, worked perfectly!

@olexandr-maltsev

Thank you.

@reema123

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

@stefansundin
Owner

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!

@omerbuyuksar

Thank you

@mohdazlan

Many thanks mate

@tymoreau

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

@ntung
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/

@ssivanatarajan

wow works perfectly recovered my rar file . Thanks

@thiagoh
thiagoh commented Mar 10, 2016

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

@ethanpayne

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

@zrod
zrod commented Mar 24, 2016

Great, worked just fine, thanks!

@huxingyi

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.

@stefansundin
Owner

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

@nielsaust

This is so awesome. I thank you!

@jeanadam

Still working great! thanks!

@kavunshiva

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

@UdayKanike

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

@stefansundin
Owner

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
@devlifealways

Worked perfectly, thanks (y)

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