Search code examples
phpimap

How to get imap body text with german locale in php?


Facing the issue that when I try to get the body of an email through imap in php, the content of the variable $body is

Jääätzt Kööönmän umläute mit ÄnhÄÄÄng

instead of

Jääätzt Kööönmän umläute mit ÄnhÄÄÄng.

I do not understand where the problem is, all other characters like ?,!,$ are working fine, just german characters like ä,ö are making those problems.

The code-block in php I am using is

$body = imap_fetchbody($mbox, $overview->msgno, 1.2);
    if (!strlen($body) > 0) {
        $body = imap_fetchbody($mbox, $overview->msgno, 1);
}

Do you have any ideas? I am really desperate.


Solution

  • The Mail you try to show is not in the same encoding as the headers of your website. That's why your browser can't show the special chars properly, like the Umlaut in this case.

    You can for example convert them to utf-8 with imap_utf8

    $body = imap_fetchbody($mbox, $overview->msgno, 1.2);
    if (!strlen($body) > 0) {
        $body = imap_fetchbody($mbox, $overview->msgno, 1);
    }
    $body = imap_utf8($body);
    

    http://php.net/manual/de/function.imap-utf8.php