Issues

ZF-6069: Wrong header encoding

Description

Zend_Mail::_encodeHeader() does not work correctly for some combinations of charset and string length.

As described in RFC 2047: http://www.faqs.org/rfcs/rfc2047.html An 'encoded-word' may not be more than 75 characters long, including 'charset', 'encoding', 'encoded-text', and delimiters. If it is desirable to encode more text than will fit in an 'encoded-word' of 75 characters, multiple 'encoded-word's (separated by CRLF SPACE) may be used.

This CRLF SPACE was added only for the message itself, but not including meta prefix and postfix (charset, encoding) as it should. Try to send a mail using charset="utf-8" (1. constructor parameter for Zend_Mail) and a subject like this:

Neue Nachricht im Stat@las: RE: RE: Nachrichten im Postausgang löschen

You'll get


=?utf-8?Q?Neue=20Nachricht=20im=20Stat@las:=20RE=5F=20RE=5F=20Nachrichten=20im=20Postausgang=20l=C3=B6sche= n?=

As you can see, there's a whitespace at the end of this string. This is in fact a CRLF created by Zend_Mime::encodeQuotedPrintable($value) in _encodeHeader() after exactly 76 chars:

Neue Nachricht im Stat@las: RE: RE: Nachrichten im Postausgang l=C3=B6sche= n

The following bugfix works, but I think it's not that beautiful:


    protected function _encodeHeader($value)
    {
        if (Zend_Mime::isPrintable($value)) {
            return $value;
        } else {
            $quotedValue  = Zend_Mime::encodeQuotedPrintable($value, 9999); // do NOT add CRLF now
            $quotedValue  = str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $quotedValue);
            $encodedValue = '=?' . $this->_charset . '?Q?' . $quotedValue . '?=';
            $encodedValue = wordwrap($encodedValue); // add CRLF now
            return $encodedValue;
        }
    }

(sorry, missing new lines here in code, no idea how to fix that)

wordwrap() is used instead of chunk_split(), because it may destroy an entity occurence at position 76 of the string.

Edit: See a discussion here (German): http://phpforum.de/forum/showthread.php?p=1297240

Comments

add code tag in the discription

Which version was used to get this bug?

It seems it is not 1.8, which includes a bugfix for these problems. Can you verify this problem still occurs?

Veryfing the code in the bug comment I saw that this snippet is from a version < 1.8 of Zend Framework, however the Mail HEader encoding Bug was fixed with 1.8.2 or something, so please upgrade to that specific version and re-open the issue if this is still occurring.