2
votes

I have a function which is meant to move a mail from one folder to another on a gmail account. The function is fully functional when it comes to moving the mail. Tho my problem appears when working with utf-8 encoded mailboxes. I decode the IMAP folder list response but the dump of both values gives different results.

// Getting the folders
$folders = imap_list(CONNECTION, MAILBOX, PATTERN);

// After a foreach, stripping slash, prefix and such
// $folder is the raw mailbox name from the IMAP list
$mailbox = utf8_encode(imap_utf7_decode($folder)); // = string(12) "Tæstbåks"

// The entered search from the client
$search_for = "Tæstbåks"; // = string(10) "Tæstbåks"

if($search_for == $mailbox)
    print "Yeah!";
else
    print "Noo!";

I do not know why those two strings do not match, that is my problem.

1
Print the bin2hex() of both. Also, why are you using utf8_encode? - Esailija
I am using the utf8_encode() to format it correct for viewing. Tryed dosins of different encodings and this were the only method returning the mailbox in a right format. Regarding the bin2hex() function the return value of both is as follows: IMAP Mailbox = string(24) "5400c3a673746200c3a56b73" Serach_for = string(20) "54c3a6737462c3a56b73" So they still don't match - Mikkel Nordskjold
But it just converts from ISO-8859-1 to UTF-8. If your input is not ISO-8859-1, then it will fail as you can see. - Esailija
Yes but at least you can see why they don't match, the first one has 2 NULs - Esailija
I see what you mean, but how do i make them match? - Mikkel Nordskjold

1 Answers

5
votes

PHP's function imap_utf7_decode($folder) is documented to return a string in ISO-8859-1 encoding. Given that IMAP's modified UTF-7 scheme can encode the whole range of Unicode (which means "a lot") and that ISO-8859-1 can only represent 256 individual characters, you cannot possibly use that function in this context. I would go as far as to suggest that the PHP developer who decided to offer such a useless function was not in his best shape the day he designed it.

It looks like the mbstring extension can do what you really want to do here -- use something like $mailbox = mb_convert_encoding($folder, "UTF-8", "UTF7-IMAP"), as suggested in the comments below the PHP's docs.