#!/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: https://spapas.github.io/2014/10/23/retrieve-gmail-blocked-attachments/ | |
# 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.") | |
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() |
This comment has been minimized.
This comment has been minimized.
Thank you. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
For those of you who can't get this working, there is an alternative way posted here.
|
This comment has been minimized.
This comment has been minimized.
Thank you |
This comment has been minimized.
This comment has been minimized.
Many thanks mate |
This comment has been minimized.
This comment has been minimized.
Works perfectly with python 2.7, I recover my rar archive. Thanks ! |
This comment has been minimized.
This comment has been minimized.
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: |
This comment has been minimized.
This comment has been minimized.
wow works perfectly recovered my rar file . Thanks |
This comment has been minimized.
This comment has been minimized.
man!! it works perfectly!! thank u!! |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Great, worked just fine, thanks! |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
@huxingyi I updated the code so it should decode filenames properly now. Give it a try. |
This comment has been minimized.
This comment has been minimized.
This is so awesome. I thank you! |
This comment has been minimized.
This comment has been minimized.
Still working great! thanks! |
This comment has been minimized.
This comment has been minimized.
Dope! This worked like a charm. Yayyy, Python modules for everything, and thanks @stefansundin |
This comment has been minimized.
This comment has been minimized.
Hi, I am getting following error, can you please help me
Thanks |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Worked perfectly, thanks (y) |
This comment has been minimized.
This comment has been minimized.
EOF problem on notepad, see this print screen: |
This comment has been minimized.
This comment has been minimized.
@me-suzy it appears you are using Python 2.7. Please run this script with Python 3. |
This comment has been minimized.
This comment has been minimized.
Works perfectly! Thanks |
This comment has been minimized.
This comment has been minimized.
Where should i place that particular file ? |
This comment has been minimized.
This comment has been minimized.
@NadeemBaloch Place the .py file and your emails (.txt files) in the same directory. Then run the python script. |
This comment has been minimized.
This comment has been minimized.
Thanks a lot! It works like a charm! |
This comment has been minimized.
This comment has been minimized.
For all the python noobs like me out there: do not save the script as email.py. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
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 |
This comment has been minimized.
This comment has been minimized.
Works like a charm. Thank you! |
This comment has been minimized.
This comment has been minimized.
no words man |
This comment has been minimized.
This comment has been minimized.
this worked awesome -- thank you -- I'm currently studying Python so am super happy with this. |
This comment has been minimized.
This comment has been minimized.
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. |
This comment has been minimized.
This comment has been minimized.
This script saved my ass. Thanks a bunch! |
This comment has been minimized.
This comment has been minimized.
Awesome, it works :) |
This comment has been minimized.
This comment has been minimized.
Working fine. Thanks. !! |
This comment has been minimized.
This comment has been minimized.
Thanks, code works. If you are the one who sent the email, you can save the email and open using Outlook etc. |
This comment has been minimized.
Thank you so much, worked perfectly!