Search code examples
phputf-8email-headers

Decoding UTF-8 Encoded Header


I'm using PHP imap to read emails out of an inbox. It extracts some information from headers. One of the headers looks like this:

X-My-Custom-Header: =?UTF-8?B?RXVnZW4gQmFiacSH?=

The original value of that encoded string is Eugen Babić.

When I try to decode that string using PHP, I can't get it quite right, the ć always comes back messed up.

I've tried imap_utf8, imap_mime_header_decode and a bunch of others I can't quite recall. They either don't return anything at all, or they mess up the ć as I mentioned before.

What is the correct way to decode this?


Solution

  • Here's what you're doing wrong: You're HTML (as generated by the PHP) is not UTF-8 encoded. So even though it's returning the accented c, the page isn't displaying it correctly.

    To fix it, add this in your <head> tag:

    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>