Question

I'm trying to build an email client using Python and imaplib.

Currently my problem is: I want to do searches on the folder that contains all the email. Usually called "[GMail]/All email".

My problem is, my GMail is natively Portuguese. Therefore, in my case, doing a select on "[Gmail]/Todo o correio", which is the portuguese equivalent, works, but selecting "[Gmail/All email" does not work.

This is a problem, because naturally I want a universal way to access my "All mail" folder in any Gmail account regardless of country.

And of course I could follow a naive solution such as doing the search on all folders and returning the aggregate results, but I definitely do not want to go down that path.

Is the a universal fix for this? Thank you

Was it helpful?

Solution

Found a solution shortly after. Will mention for convenience.

When you run mail_instance.list() you obtain a list of folders, such as:

(\Noselect \HasChildren) "/" "[Gmail]"
(\HasChildren \HasNoChildren \Trash) "/" "[Gmail]/Caixote do Lixo"
(\HasNoChildren \Flagged) "/" "[Gmail]/Com estrela"
(\HasChildren \HasNoChildren \Sent) "/" "[Gmail]/Correio enviado"
(\HasChildren \HasNoChildren \Important) "/" "[Gmail]/Importante"
(\HasChildren \HasNoChildren \Drafts) "/" "[Gmail]/Rascunhos"
(\HasNoChildren \Junk) "/" "[Gmail]/Spam"
(\HasChildren \HasNoChildren \All) "/" "[Gmail]/Todo o correio"

(these are in Portuguese)

The name of the "all mail" folder always contains the tag "\All" in the string, so just do a search for "\All" in all folder names that are returned by .list().

Then just extract the substring from the index of "[Gmail]" to the index of the first double quote after that, and you obtain the "all mail" folder name.

OTHER TIPS

See this answer: https://stackoverflow.com/a/3846248/2131094

"Google and Apple developed a special IMAP command XLIST to address this issue."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top