Reputation: 2249
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.
Upvotes: 0
Views: 7556
Reputation: 2647
You can not just say "fetch email from imap and convert it to utf-8". You need to fetch email parts with this:
$struct = $this->imap_fetchstructure($this->mbox, $mId, FT_UID);
Then to choose what you what from that email. And after you decided what you need then say converto to that and that like this:
if($partStruct->encoding == 1) {
$data = $this->imap_utf8($data);
}
elseif($partStruct->encoding == 2) {
$data = $this->imap_binary($data);
}
elseif($partStruct->encoding == 3) {
$data = $this->imap_base64($data);
}
elseif($partStruct->encoding == 4) {
$data = $this->imap_qprint($data);
}
//etc.....
I just copy/paste from my class other part you wil have to figure outh on your own! If you are looking for easy solution for this, let me tell you there is none. You can always download some imap php classes from internet.
Upvotes: 1
Reputation: 2733
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"
Upvotes: 0