Python imaplib selecting folders

Гриша Кушнир picture Гриша Кушнир · May 28, 2017 · Viewed 11.8k times · Source

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.

Answer

Гриша Кушнир picture Гриша Кушнир · May 28, 2017

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])