从gmail下载谷歌驱动器附件

时间:2018-01-15 03:29:47

标签: python gmail

我尝试使用python imaplib.IMAP4_SSL从gmail下载附件。但问题是如果附件大于25M,它将自动转换为谷歌驱动器中的文件。通过使用下面的代码,我找不到附件。有谁知道如何访问和下载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

0 个答案:

没有答案