UTF-8 character encoding for email headers using PHP

I am trying to encode โ†’ (right arrow, → or unicode 2192 hex) into the subject line of an email message.

When I use php mb_encode_mimeheader() , I get a different value when I do the same with Thunderbird or Gmail. But when the message generated by php arrives, the character is not displayed properly. In addition, PHP mb_decode_mimeheader() works with exiting PHP, but not for decoding content from other email sources.

As a hex dump, I designed the UTF-8 arrow representation

 <?php $rarr = "\xe2\x86\x92"; mb_encode_mimeheader($rarr, 'UTF-8'); // =?UTF-8?B?w6LChsKS?= // whereas Tbird and Gmail produce: =?UTF-8?B?4oaS?= // and more manually: '=?UTF-8?B?' . base64_encode($rarr).'?='; // =?UTF-8?B?4oaS?= 

PHP coding is output in Thunderbird and Gmail as: รข

I am completely confused by the behavior of PHP, as it does not seem to give standard results.

How can I get PHP to encode the message header value of UTF-8 so that it is properly decoded by email clients?

+4
source share
1 answer

There seems to be an error that ignores the second parameter, I get the correct result when I add the internal encoding:

 <?php $rarr = "\xe2\x86\x92"; mb_internal_encoding( "UTF-8"); echo mb_encode_mimeheader($rarr, 'UTF-8'); //=?UTF-8?B?4oaS?= 

But

 <?php $rarr = "\xe2\x86\x92"; mb_encode_mimeheader($rarr, 'UTF-8'); //=?UTF-8?B?w6LChsKS?= 

Just setting the internal encoding is enough:

 <?php $rarr = "\xe2\x86\x92"; mb_internal_encoding( "UTF-8"); echo mb_encode_mimeheader($rarr); //=?UTF-8?B?4oaS?= 
+8
source

Source: https://habr.com/ru/post/1446443/


All Articles