3

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 ?
Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
abhinav dixit
  • 241
  • 3
  • 14
  • @peter when i am trying mb_decode_mimeheader than I am not getting desired result code i am using is : $subject="=?UTF-8?Q?=e2=99=a3?= Your winning day =?UTF-8?Q?=e2=99=a3?="; echo $sub=mb_decode_mimeheader($subject);output:? Your winning day ? – abhinav dixit May 01 '14 at 15:02
  • Voting to reopen, the linked duplicate does not really solve this problem. @abhinav Please post your code into the question next time to make the problem more specific. – deceze May 02 '14 at 08:26
  • Please update with two more data points, so that question has all 3 -- expected output, output using answer accepted in duplicate, output using the other answer in duplicate. – Dima Tisnek May 02 '14 at 08:51
  • Testing with Python OP string evaluates to U+2663 (black spaces suit) + text + same unicode char, i.e. `♣Your winning day♣`. I suppose OP issue is missing unicode. Perhaps all OP needs is echo utf8_encode(mb_....); – Dima Tisnek May 02 '14 at 08:57

1 Answers1

10

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    when i am trying to execute snippet of code provided by you i am getting outout: ♣ Your winning day ♣ is there any way to overcome this.Please it might be low level question for you , but i am bit new with IMAP so i am facing these trouble. Thanks in advance for your help and guidance.But still i am searching to find a way how can i accomplish my requirement. – abhinav dixit May 01 '14 at 17:53
  • 1
    yes @abhinav you are right i too getting same output deceze how you got your output as "♣ Your winning day ♣".Though I too trying same code and getting same output as mentioned by abhinav. Few days back I too stuck at same problem. – Deepak May 01 '14 at 18:05
  • @abhinav That means everything works fine, but you're not displaying the result as UTF-8. Where are you displaying this? Also see http://kunststube.net/frontback. – deceze May 01 '14 at 18:06
  • I am just trying to display it on browser using xampp, is there any setting required to modify in php ini file. – abhinav dixit May 01 '14 at 18:17
  • @abhinav See http://stackoverflow.com/questions/279170/utf-8-all-the-way-through in addition to the link I gave above. – deceze May 01 '14 at 18:21
  • 1
    Hey deceze you are rock star bro it working now your link kunststube.net/frontback solved my problem thank you very much.In addition to your snippet of code i added header('Content-Type: text/html; charset=utf-8'); and with it it works fine. – abhinav dixit May 01 '14 at 18:23