2

I am working on my first PHP code and i want to display some email subjects.

header('content-type: text/html; charset=utf-8');
...
$header = imap_headerinfo($imap, $i);
$raw_body = imap_body($imap, $i);
$subject = utf8_encode($header->subject);

echo $subject;
echo "<br>";
...

But for the Subject "ää üü öö ß" the output looks like that:

=?ISO-8859-1?B?5OQg/Pwg9vYg3w==?=

regards

Solution:

I found a very helpful function on the web (http://php.net/manual/de/function.imap-mime-header-decode.php), it has two little syntax errors but after a little rework it worked fine for me.

In the End the Solution looks like that:

//return supported encodings in lowercase.
function mb_list_lowerencodings() { $r=mb_list_encodings();
  for ($n=sizeOf($r); $n--; ) { $r[$n]=strtolower($r[$n]); } return $r;
}

//  Receive a string with a mail header and returns it
// decoded to a specified charset.
// If the charset specified into a piece of text from header
// isn't supported by "mb", the "fallbackCharset" will be
// used to try to decode it.
function decodeMimeString($mimeStr, $inputCharset='utf-8',     
$targetCharset='utf-8',$fallbackCharset='iso-8859-1') {
$encodings=mb_list_lowerencodings();
$inputCharset=strtolower($inputCharset);
$targetCharset=strtolower($targetCharset);
$fallbackCharset=strtolower($fallbackCharset);

$decodedStr='';
$mimeStrs=imap_mime_header_decode($mimeStr);
for ($n=sizeOf($mimeStrs), $i=0; $i<$n; $i++) {
  $mimeStr=$mimeStrs[$i];
  $mimeStr->charset=strtolower($mimeStr->charset);
if (($mimeStr == 'default' && $inputCharset == $targetCharset)
  || $mimeStr->charset == $targetCharset) {
  $decodedStr.=$mimStr->text;
} else {
  $decodedStr.=mb_convert_encoding(
    $mimeStr->text, $targetCharset,
    (in_array($mimeStr->charset, $encodings) ?
      $mimeStr->charset : $fallbackCharset)

  );
}
} return $decodedStr;
}

...

$header = imap_headerinfo($imap, $i);
$raw_body = imap_body($imap, $i);
$sub = decodeMimeString($header->subject);
echo $sub;
...

I want to point out, that the two functions are created by the author @http://php.net/manual/de/function.imap-mime-header-decode.php and I just removed two syntax errors.

Thank you for you replies

Quick n Dirty
  • 569
  • 2
  • 7
  • 15

2 Answers2

1

This is a common format for mails, called "quoted printable". All non ascii characters are encoded. (See http://en.wikipedia.org/wiki/Quoted-printable)

The string is encapsulated by

=?<encoding>?Q?<string>?=

<encoding> describes the encoding. Here: ISO8859-1

<string> is the string itself

Please use imap_mime_header_decode() to decode the string ( before using utf8_encode() )!

freytag
  • 4,769
  • 2
  • 27
  • 32
  • ok i added the command as you discribed, but the output looks the same – Quick n Dirty Jul 21 '12 at 19:09
  • Have you modified the string? What does the raw mail data of the subject look like? BTW: utf8_encode(quoted_printable_decode($string)) can't work, quoted_printable_decode() returns an array! – freytag Jul 21 '12 at 19:19
0

If Muhammad's answer won't be enough, you can use iconv function that allows you to change encoding of string

iconv("ISO-8859-1", "UTF-8", $your_string);
cyborg86pl
  • 2,597
  • 2
  • 26
  • 43
  • This would be an awesome opportunity to take advantage of StackOverflow's community editing features. Click the edit button on Muhammad's answer and append this to his answer so ifelseforeach (and anybody who ends up on this page with the same question in the future) can benefit from one well crafted response that is clearly marked as best as opposed to two good answers which have to be read independently and could be lost among future answers. – retrohacker Jul 21 '12 at 18:53
  • i tried this before, but it does not change anything.. is there maybe another place where i got to declare the used encoding? – Quick n Dirty Jul 21 '12 at 18:56
  • it may be important how your php file is encoded (may depend on code editor). i had an issue with that and that's messy thing. Maybe you put some functions i wrong order? i really have no idea what it may be. – cyborg86pl Jul 21 '12 at 19:05
  • 1
    @Crackers: Unfortunately an edit of this type would probably be considered too drastic, putting words in the author's mouth. It's totally fine to have multiple similar or complimentary answers. If you stop reading at the accepted answer, you are missing a lot. – Wesley Murch Jul 21 '12 at 19:10
  • @Wesley Murch: I hadn't considered that, I'm still relatively new and trying to figure out how the system works. Thanks for the guidance! – retrohacker Jul 21 '12 at 19:19