Last active
January 12, 2024 10:18
-
-
Save x011/d6c346debe22f5fec9e4499643ae1050 to your computer and use it in GitHub Desktop.
Gmail Attachment Downloader 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Made for: https://stackoverflow.com/questions/61366836/download-attachment-from-mail-using-python/ | |
import os | |
from imbox import Imbox # pip install imbox | |
import traceback | |
# enable less secure apps on your google account | |
# https://myaccount.google.com/lesssecureapps | |
host = "imap.gmail.com" | |
username = "username" | |
password = 'password' | |
download_folder = "/path/to/download/folder" | |
if not os.path.isdir(download_folder): | |
os.makedirs(download_folder, exist_ok=True) | |
mail = Imbox(host, username=username, password=password, ssl=True, ssl_context=None, starttls=False) | |
messages = mail.messages() # defaults to inbox | |
for (uid, message) in messages: | |
mail.mark_seen(uid) # optional, marks message as read | |
for idx, attachment in enumerate(message.attachments): | |
try: | |
att_fn = attachment.get('filename') | |
download_path = f"{download_folder}/{att_fn}" | |
print(download_path) | |
with open(download_path, "wb") as fp: | |
fp.write(attachment.get('content').read()) | |
except: | |
pass | |
print(traceback.print_exc()) | |
mail.logout() | |
""" | |
Available Message filters: | |
# Gets all messages from the inbox | |
messages = mail.messages() | |
# Unread messages | |
messages = mail.messages(unread=True) | |
# Flagged messages | |
messages = mail.messages(flagged=True) | |
# Un-flagged messages | |
messages = mail.messages(unflagged=True) | |
# Messages sent FROM | |
messages = mail.messages(sent_from='[email protected]') | |
# Messages sent TO | |
messages = mail.messages(sent_to='[email protected]') | |
# Messages received before specific date | |
messages = mail.messages(date__lt=datetime.date(2018, 7, 31)) | |
# Messages received after specific date | |
messages = mail.messages(date__gt=datetime.date(2018, 7, 30)) | |
# Messages received on a specific date | |
messages = mail.messages(date__on=datetime.date(2018, 7, 30)) | |
# Messages whose subjects contain a string | |
messages = mail.messages(subject='Christmas') | |
# Messages from a specific folder | |
messages = mail.messages(folder='Social') | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AttributeError: 'GmailMessages' object has no attribute 'attachments'