Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to utf-8 - from email

Tags:

php

email

encode

Im using Zend_Mail_Storage_Pop3 to retrieve mail messages.

My subject on a mail is Foo/æøå

$message->getHeader('content-type') gives me text/plain; charset=ISO-8859-1; format=flowed

Before any encoding my $message->subject looks like this

Foo/µ°Õ - 2h - comment

Then I try to do a iconv on the subject

$message->subject = iconv('ISO-8859-1','UTF-8', $message->subject);

Now my subject looks like this

Foo/├ª├©├Ñ - 2h - comment

Which is not utf-8 :)

So what should I do? I also tried with utf8_encode and mb_convert_encoding but these gives the same result

Well I got it - but its a bit messy, but it works

$this->mails = new Zend_Mail_Storage_Pop3(...);
$currentMessageId = $this->mails->getNumberByUniqueId($this->mails->getUniqueId($messageId));
$raw = $this->mails->getRawHeader($currentMessageId);
$l = explode("\n", $raw);
foreach($l AS $m) {
    if (strpos($m, 'Subject: ') === 0) {
        $subject = trim(str_replace('Subject: ', '', $m));
        break;
    }
}

$subject = str_replace("_"," ", mb_decode_mimeheader($subject));
like image 772
Martin- Avatar asked Oct 17 '25 18:10

Martin-


1 Answers

The content-type-field usually holds the encoding for the message body, not for the header. Can you have a look at the message in it's raw format? A field in ISO 8859-1 should look like this:

=?ISO-8859-1?Q?Graphgr=F6=DFen?=

while an UTF8 encoded header should look like this:

=?UTF-8?B?w5xtbMOkdXRlIGluIFVURjg=?=
like image 148
Nico Haase Avatar answered Oct 20 '25 06:10

Nico Haase