Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decode UTF8 charset of a subject line of a received email [duplicate]

Tags:

php

While working with IMAP email headers in PHP , I encountered an issue which is related to encoding of subject line. When I am try mb_decode_mimeheader (as per the duplicate issue) then I am not getting desired result:

$subject = "=?UTF-8?Q?=e2=99=a3?= Your winning day =?UTF-8?Q?=e2=99=a3?=";
echo mb_decode_mimeheader($subject);

Output:

? Your winning day ?
like image 781
abhinav dixit Avatar asked Sep 15 '25 19:09

abhinav dixit


1 Answers

To fix the example in the comments:

mb_internal_encoding('UTF-8');
$subject = "=?UTF-8?Q?=e2=99=a3?= Your winning day =?UTF-8?Q?=e2=99=a3?=";
echo mb_decode_mimeheader($subject);

Outputs "♣ Your winning day ♣".

It's undocumented that mb_internal_encoding influences the operation of mb_decode_mimeheader, but it does. mb_internal_encoding is taken as the target charset that should be returned from mb_decode_mimeheader. If that's set to Latin-1, then characters like "♣" cannot be decoded into this target charset and will fail.

You may want to look at imap_utf8 for a less finicky function.

like image 76
deceze Avatar answered Sep 18 '25 10:09

deceze