4

I'm trying to use python imaplib.IMAP4_SSL to download attachments from gmail. But the problem is if the attachment is larger than 25M, it will be automatically converted to a file in google drive. By using the code below, I cannot find the attachment. Does anyone know how to access and download the file in google drive attached in gmail?

conn = imaplib.IMAP4_SSL("imap.gmail.com")
conn.login(config.username, config.password)
conn.select("Inbox")

resp, items = conn.search(None, '(FROM "xx@xx.com")')
items = items[0].split()

flag = False

emailid = items[-1]
resp, data = conn.fetch(emailid, "(RFC822)")
email_body = data[0][1]
mail = email.message_from_string(email_body)

msg = email.message_from_string(data[0][1])

 for part in mail.walk():
    if (part.get_content_maintype() == 'multipart'):
        continue

    if (part.get('Content-Disposition') is None):
        continue

    attachment_name = part.get_filename()

    filename = "attachment_trial.xlsx"

    fp = open(filename, 'wb')
    fp.write(part.get_payload(decode = True))
    fp.close()

    flag = True

return flag
Natsu
  • 41
  • 1

0 Answers0