Python script to download attachments block by Gmail

Anti-virus warning– 1 attachment contains a virus or blocked file. Downloading this attachment is disabled

Python  script to download attachments block by Gmail

A few days back I wanted to download code backup RAR files attached in one of my emails as it contains many scripts files Gmail block the file with bellow error.

Anti-virus warning– 1 attachment contains a virus or blocked file. Downloading this attachment is disabled

gmail_attachment_block.PNG

After searching on Google I found an easy way of doing it with python so thought of sharing it with everyone.

There is an option in Gmail for each email to Show the original which opened a text file with the original, MIME-encoded message. In this text file, all the attachments are encoded and we need to extract them and convert them to a binary file.

Warning: As Gmail says that an attachment is not safe it may not be actually safe. So before downloading you must be 100% sure.

Now Go to your emails, click the 3 dots in the top right, "Show original", then click on "Download Original". save the file with the name original_email_file.txt within the same folder where we will save our script

gmail-email-header.png

save the bellow code in file extract_attachments.py

#!/usr/bin/env python3
# use Python 3 and higher version
# Get your files that Gmail block. Warning message:
# "Anti-virus warning - 1 attachment contains a virus or blocked file. 
# Downloading this attachment is disabled."
# Go to your emails, click the 3 dots 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__':     
    #process all original email files in folder whare script is located if no argument pass
    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')] #get all files with txt extention in folder 
    else:
        files = sys.argv[1:] #process the file pass in argument

    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():
            file_name_header = pl.get_filename()
            if file_name_header:
                file_name_data = email.header.decode_header(file_name_header)
                (file_name_str, file_name_charset) = file_name_data[0]
                if isinstance(file_name_str, str):
                    fn = file_name_str
                else:
                    fn = file_name_str.decode(file_name_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))#create files or write to file 
        print()

Then run the below command.

python extract_attachments.py original_email_file.txt

The encoded content is get converted into an attachment from the original message of the Gmail text file.