Python imaplib selecting folders

梦想的初衷 提交于 2019-12-20 04:37:30

问题


I am bulding a mail client using Django and for extracting emails i'm using imaplib. So far, i can select inbox folder cause in every imap server it's name is "INBOX". But when it comes to selecting other folders like Spam, Sent and others i have troubles because their name come based on account language. For exemple my account is set to russian language and listing mails like this:

mail = imaplib.IMAP4_SSL(IMAP)
mail.login(USERNAME, PASSWORD)

for i in mail.list()[1]:
    print(i)

gives me the following output (utf-7):

b'(\\Inbox) "/" "INBOX"'
b'(\\Spam) "/" "&BCEEPwQwBDw-"'
b'(\\Sent) "/" "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-"'
b'(\\Drafts) "/" "&BCcENQRABD0EPgQyBDgEOgQ4-"'
b'(\\Trash) "/" "&BBoEPgRABDcEOAQ9BDA-"'

How can i select folders despite the account's selected language? If i use:

mail.select("&BBoEPgRABDcEOAQ9BDA-")

it works but my mail client is useless like this.


回答1:


I decided to split the output and use folder names as they are, in the language they came:

for i in mail.list()[1]:
    l = i.decode().split(' "/" ')
    print(l[0] + " = " + l[1])


来源:https://stackoverflow.com/questions/44230855/python-imaplib-selecting-folders

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