phpsymfonyimaputf-7

PHP, IMAP and Outlook 2010 - folder names encoding is different?


Im developing e-mail client in php (with symfony2) and i have problem with folders with non-ascii characters in name.

Folder created in php app is visible in same app correctly. Same in Outlook, created in outlook looks good in outlook. In other cases not. Folder created in outlook is not displayed correctly in php and vice-versa.

Im using utf-7 to encode folder names in php. Which encoding uses Outlook?

Example: Folder named "Wysłąne" (misspelled polish word meaning "sent"), first one is encoded in utf7 by php, and second created in Outlook:

PHP:

Wys&xYLEhQ-ne

Outlook:

Wys&AUIBBQ-ne

Why it differs? How to make it in same encoding?


Solution

  • There seems to be a mixup in your source character encoding. imap_utf7_encode (and similar) expect your string in ISO-8859-1 encoding.

    AFAICT there is no way to represent Wysłąne in ISO-8859-1. "Wysłąne" represented as UTF-8 becomes (hex bytes)

    byte value (hex)    57, 79, 73, C5 82, C4 85, 6E 65
    unicode character   W   y   s   ł      ą      n  e
    

    The PHP result Wys&xYLEhQ-ne when decoded is "Wys얂쒅ne". The the two special characters therein are Korean characters with code points U+C582 and U+C485 respectively. So it appears a character-per-character translation is somehow attempted, where the UTF-8 representation of two of the characters is interpreted as Unicode code points instead.

    The simplest way to fix this is to use the mbstring extension which has the mb_convert_encoding function.

    $utf7encoded = mb_convert_encoding($utf8SourceString, "UTF7-IMAP","UTF-8")
    $decodedAsUTF8 = mb_convert_encoding($utf7String,"UTF-8", "UTF7-IMAP")