How do I download Word document attachments from gmail?

159 Views Asked by At

I'm trying to download a word document attachment from my Gmail account using imbox in Python, but when I decode the attachment I get a bunch of unreadable bytes (utf-8 won't decode them, and they are mostly null bytes). Does Imbox just not work with .doc files?

Here's my code:

# Downloads Word document schedule from my emails

# Imports
import os
from imbox import Imbox


# Logging in
host = "imap.gmail.com"  # Gmail
emailUser = "[email protected]"  # My account
emailPass = os.environ.get("MailPass")  # My password

mail = Imbox(host, username=emailUser, password=emailPass, ssl=True, ssl_context=None, starttls=False)  # Connecting
messages = mail.messages(sent_from="[email protected]")  # Getting messages from schedule sender

for uid, message in messages:

    if message.attachments:
        attachment = message.attachments
        for idx, att in enumerate(attachment):
            att.get('content').read()
        break

0

There are 0 best solutions below