pythonencodingimaputf-7

IMAP folder path encoding (IMAP UTF-7) for Python


I would like to know if any "official" function/library existed in Python for IMAP4 UTF-7 folder path encoding.

In the imapInstance.list() I get the following path IMAP UTF-7 encoded :

'(\\HasNoChildren) "." "[Mails].Test&AOk-"',

If I do the following encoding :

(u"[Mails].Testé").encode('utf-7')

I get :

'[Mails].Test+AOk-'

Which is UTF-7 but not IMAP UTF-7 encoded. Test+AOk- instead of Test&AOk- I'd need an official function or library to get the IMAP UTF-7 encoded version.


Solution

  • The IMAPClient package has functionality for encoding and decoding using IMAP's modified UTF-7. Have a look in the IMAPClient.imap_utf7 module. This module could be used standalone or you could just use IMAPClient which handles doing the encoding and decoding of folder names transparently.

    The project's home page is: https://github.com/mjs/imapclient

    Example code:

    from imapclient import imap_utf7
    decoded = imap_utf7.decode(b'&BdAF6QXkBdQ-')
    

    Disclaimer: I'm the original author of the IMAPClient package.