Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert mail body to UTF-8

I have string "test+=EC=B9=E8=F8=BE=FD=E1=ED=E9+test", it is an email body retrieved using imap_fetchbody(). I want to convert it to proper UTF-8 string "test+ěščřžýáíé+test". I have tried functions imap_utf7_decode, imap_8bit, base64_decode, quoted_printable_decode with no success. Can you please advise me a function which will convert mentioned string to UTF-8?

I am using iconv_mime_decode($str, 0, "UTF-8"); for mail headers, but it does not work for mail body.

Thank you

The answer is in the accepted answer's comments!

like image 837
A123321 Avatar asked Jan 29 '26 14:01

A123321


1 Answers

Your input string appears to be ISO-8859-2, so you can use this function I have adapted from the comments in the PHP documentation.

function decode_qprint($str) {
    $str = preg_replace("/\=([A-F][A-F0-9])/", "%$1", $str);
    $str = urldecode($str);
    $str = iconv("ISO-8859-2", "UTF-8", $str);
    return $str;
}

Edit: Updated function per the comments:

function decode_qprint($str) {
    $str = quoted_printable_decode($str);
    $str = iconv('ISO-8859-2', 'UTF-8', $str);
    return $str;
}
like image 196
spencercw Avatar answered Feb 01 '26 04:02

spencercw