Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python imaplib selecting folders

Tags:

python

imaplib

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.

like image 755
Гриша Кушнир Avatar asked May 28 '17 18:05

Гриша Кушнир


1 Answers

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])
like image 118
Гриша Кушнир Avatar answered Oct 14 '22 14:10

Гриша Кушнир