Question

I'm using Zend Framework 1's IMAP server connector and I'm trying to fetch an email from server with Unicode characters in its subject. Here's how I do it:

$message = $imapServer->getMessage($message_number);
echo $message->getHeader('subject');

The problem is that it comes out encoded:

=?UTF-8?B?2KjYp9uM?=

I can find the encoding function within Zend_Mail class named _encodeHeader but I can not find the decoding pair! Does anyone know how to decode this string?

And here's the encoder function:

protected function _encodeHeader($value)
{
    if (Zend_Mime::isPrintable($value) === false) {
        if ($this->getHeaderEncoding() === Zend_Mime::ENCODING_QUOTEDPRINTABLE) {
            $value = Zend_Mime::encodeQuotedPrintableHeader($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
        } else {
            $value = Zend_Mime::encodeBase64Header($value, $this->getCharset(), Zend_Mime::LINELENGTH, Zend_Mime::LINEEND);
        }
    }

    return $value;
}
Était-ce utile?

La solution

Search for a "RFC2047 decoder" and pick one of the existing libraries which does just that. If nothing is usable, roll your own.

Autres conseils

Here's how I solved it:

switch (strtolower($encoding)) {
    case \Zend_Mime::ENCODING_QUOTEDPRINTABLE:
        if (preg_match('/^\s?=\?([^\?]+)\?Q\?/', $str, $matches) === 1) {
            $str = preg_replace('/\s?=\?'.preg_quote($matches[1]).'\?Q\?/', ' ', $str);
            $str = strtr($str, array('?=' => ''));
            $str = trim($str);
        }
        return \Zend_Mime_Decode::decodeQuotedPrintable($str);

    case \Zend_Mime::ENCODING_BASE64:
        return base64_decode($encodedText);

    case \Zend_Mime::ENCODING_7BIT:
    case \Zend_Mime::ENCODING_8BIT:
    default:
        return $encodedText;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top