Hedam Hedam - 5 months ago 56
PHP Question

Fetch email from imap and convert to UTF-8

I'm having some trouble fetching emails with php and imap.
Each sender sends emails encoded in different charset encodings, making it hard to make a universal solution for me.

If i wanna fetch the from, to, subject and body/html into UTF-8 strings without loosing data, how is this done easiest?

I've tried finding a solution for hours and my current code does not solve the problem.

Greetings.

Answer

You can try using the mb_detect_encoding() function: http://php.net/manual/en/function.mb-detect-encoding.php

And then mb_convert_encoding(): http://php.net/manual/en/function.mb-convert-encoding.php to UTF-8.

You can also inspect the email headers, look for the Content-Type header, see example:

Content-Type: text/html; charset="UTF-8"
Comments