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:
# 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.")
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))

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


@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


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:


wow works perfectly recovered my rar file . Thanks

thiagoh commented Mar 10, 2016

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


Also you can download the attachment using another client such as

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.

            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.


@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



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
fn_header = pl.get_filename()
AttributeError: 'str' object has no attribute 'get_filename'
udays-MacBook-Pro:mystuff udaykumarkanike$



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

Worked perfectly, thanks (y)

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