Unable to retrieve gmail messages from any folder other than inbox (Python3 issue)

筅森魡賤 提交于 2019-12-03 02:08:09
Ben

As it's mentioned in this answer:

Try using m.select('"[Gmail]/All Mail"'), so that the double quotes get transmitted. I suspect imaplib is not properly quoting the string, so the server gets what looks like two arguments: [Gmail]/All, and Mail.

And it works in python v3.4.1

import imaplib
import email

m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
m.login("myemail@gmail.com","mypassword")
m.select('"[Gmail]/All Mail"')

result, data = m.uid('search', None, "ALL") # search all email and return uids
if result == 'OK':
    for num in data[0].split():
    result, data = m.uid('fetch', num, '(RFC822)')
    if result == 'OK':
        email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
        print('From:' + email_message['From'])

m.close()
m.logout()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!