1

My script does not display Polish characters. How to fix it? Thank you.

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>

        <?php
        header('Content-Type: text/html; charset=utf-8');

        $hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
        $username = 'user@gmail.com';
        $password = 'pass';

        $inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());


        $emails = imap_search($inbox, 'ALL');

        if ($emails) {
            $output = '';

            foreach ($emails as $email_number) {
                $overview = imap_fetch_overview($inbox, $email_number, 0);
                $message = imap_fetchbody($inbox, $email_number, 2);

                $output.= '<div class="toggler ' . (imap_utf8($overview[0]->seen) ? 'read' : 'unread') . '">';
                $output.= '<span class="subject">' . imap_utf8($overview[0]->subject) . '</span> ';

                $output.= '<span class="from">' . imap_utf8($overview[0]->from) . '</span>';
                $output.= '<span class="date">on ' . imap_utf8($overview[0]->date) . '</span>';
                $output.= '</div>';

                /* output the email body */
                $output.= '<div class="body">' . imap_utf8($message) . '</div>';
            }

            echo $output;
        }
        imap_close($inbox);
        ?>
    </body>
</html>

Example output:

Je=B6li chcesz uatrakcyjni=E6 wygl=B1d swojej skrzynki odbiorczej za pom= oc=B1

I expect:

Jeśli chcesz uatrakcyjnić wygląd swojej skrzynki odbiorczej za pomocą

query_gif
  • 97
  • 3
  • 11

2 Answers2

1

The email is not in UTF-8 (quoted), so imap_utf8 does not work here.

But a mistake earlier on is that you don't check which encoding is used with the email.

How to fix?

  1. Check the encoding of the email / body.
  2. Convert that encoding to UTF-8 for your displaying purposes then.
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Encoding of the email: Content-Type: text/plain; charset=ISO-8859-2; format=flowed; delsp=yes Content-Transfer-Encoding: base64 – query_gif Jul 11 '12 at 08:01
  • convert from ISO-8859-2 to UTF-8 then. That will turn the `=B6` into `ś`, see http://de.wikipedia.org/wiki/ISO_8859-2 - Also see the [`iconv_mime_decode`](http://php.net/iconv_mime_decode) function – hakre Jul 11 '12 at 08:16
  • I try: mb_convert_encoding($message, "UTF-8", "ISO-8859-2"); and my output is something like this: R3J6ZWe/87NrYSBsYXRhIGkgcLNhY3plIPOzYW5pILFja2kK – query_gif Jul 11 '12 at 10:13
  • because it's still base64 encoded. When you decode the transfer encoding (base64) you will get as string in ISO-8859-2. You will then need to re-encode to UTF-8 and you're done. – hakre Jul 11 '12 at 10:31
0

This looks like Quoted-Printable encoding.

Simply replace your imap_utf8 with quoted_printable_decode and use iconv to convert from the (presumably) polish charset to utf8.

skabbes
  • 890
  • 7
  • 17