Special Characters in IMAP Folders
I was always wondering, why folders on an Exchange server, containing special
characters in the name, show up that different in the IMAP protocol (e.g. using
Mail::IMAPClient
in Perl scripts).
But now I understand, this is just the result of the IMAP specification, that uses a modified UTF-7 encoding for non-ASCII characters (see RFC 2060, section 5.1.3).
If you ever need to automatically convert folder names into that modified
UTF-7, the Perl module Encode::IMAPUTF7
may render useful.
The German Umlauts are encoded in the following way:
ISO-8859-15 | IMAP-UTF-7 |
---|---|
ä | &AOQ- |
ö | &APY- |
ü | &APw- |
Ä | &AMQ- |
Ö | &ARN- |
Ü | &ANw- |
ß | &AN8- |
Review 2024
Revisiting this topic in 2024, I first tried to get a Perl with that Module running in a container. And it was easier than expected.
-
Open Alpine Linux:
docker run -it --rm docker.io/library/alpine
-
Install the Perl module:
apk install perl-encode-imaputf7
-
To encode into IMAP-UTF-7 in a UTF-8 enabled shell:
perl -MEncode::IMAPUTF7 \ -e ' print Encode::IMAPUTF7::encode( "IMAP-UTF-7", Encode::decode("UTF-8", "ä ö ü Ä Ö Ü ß") ), "\n" ' # Output: &AOQ- &APY- &APw- &AMQ- &ANY- &ANw- &AN8-
-
Have fun with even more diacritics.
perl -MEncode::IMAPUTF7 \ -e ' print Encode::IMAPUTF7::encode( "IMAP-UTF-7", Encode::decode("UTF-8", "á à á à â é è ø") ), "\n" ' # Output: &AOE- &AOA- &AOE- &AOA- &AOI- &AOk- &AOg- &APg-
-
It’s interesting how the
&
is handled, just the prefix&
and suffix-
with nothing inbetween.perl -MEncode::IMAPUTF7 \ -e ' print Encode::IMAPUTF7::encode( "IMAP-UTF-7", Encode::decode("UTF-8", "& a&b") ), "\n" ' # Output: &- a&-b