user3702861
user3702861

Reputation: 747

PHP imap weird encoding characters

I'm using PHP to get my gmail email messages. It gives me email titles which look like this:

=?ISO-8859-13?Q?Darba_s=E2k=F0ana_ar_Gmail?= 

It should actually look like this (those are Latvian characters normally available in utf8):

Darba sākšana ar Gmail

I tried:

utf8_encode(quoted_printable_decode(
   "=?ISO-8859-13?Q?Darba_s=E2k=F0ana_ar_Gmail?="
));

And it gives me the following, which is not correct:

=?ISO-8859-13?Q?Darba_sâkðana_ar_Gmail? 

How do I get this - Darba sākšana ar Gmail

Upvotes: 1

Views: 892

Answers (2)

François Cright
François Cright

Reputation: 21

As Piotr answered, you need to use imap_mim_header_decode. I'm assuming that you are using the imap_headerinfo with fromaddress. This is what I did :

if (strpos($header->fromaddress,'?=')) {
    $wrong_charset = imap_mime_header_decode($header->fromaddress);

    $corrected_charset = '';

    for ($i=0; $i<count($value); $i++) {
        $corrected_charset .= "{$wrong_charset[$i]->text} ";
    }

}
else{
    $corrected_charset = $header->fromaddress;
}

Upvotes: 0

Piotr Olaszewski
Piotr Olaszewski

Reputation: 6204

You must use imap_mime_header_decode function:

$text = "=?ISO-8859-13?Q?Darba_s=E2k=F0ana_ar_Gmail?=";

$elements = imap_mime_header_decode($text);
foreach ($elements as $element) {
    echo "Charset: $element->charset\n";
    echo "Text: $element->text\n\n";
}

And you can use iconv function to convert:

iconv($element->charset, 'utf-8', $element->text);

Upvotes: 2

Related Questions