ZF-3588: Incorrect encoding of unicode subjects

Description

When using Zend_Mail with utf-8 encoding and hebrew characters, the subject is incorrectly encoded causing artifacts to appear. I've tracked this to the protected method _encodeHeader(), line 393 in Zend_Mail:


protected function _encodeHeader($value)
    {
      if (Zend_Mime::isPrintable($value)) {
          return $value;
      } else {
          $quotedValue = Zend_Mime::encodeQuotedPrintable($value);
          $quotedValue = str_replace(array('?', ' '), array('=3F', '=20'), $quotedValue);
          return '=?' . $this->_charset . '?Q?' . $quotedValue . '?=';
      }
    }

Replacing the quoting method to encodeBase64() of Zend_Mime seems to fix the issue. Meaning:


protected function _encodeHeader($value)
    {
      if (Zend_Mime::isPrintable($value)) {
          return $value;
      } else {
            return '=?' . $this->_charset . '?B?' . Zend_Mime::encodeBase64($value) . '?=';
      }
    }

Will return a correctly formatted utf-8 subject. I'm not an expert on Mime so I can't tell if this is the best solution. However it should be fixed to correctly format utf-8 subjects

Comments

That issue is also present in 1.5.3 and 1.6.0 rc1.

Here is another fix without using the base64 encoding. It's not perfect either but it corrects the issue.

 
    protected function _encodeHeader($value)
    {
      if (Zend_Mime::isPrintable($value)) {
          return $value;
      } else {
          $quotedValue = Zend_Mime::encodeQuotedPrintable($value);
          $quotedValue = str_replace(array('?', ' ', '_', '=' . Zend_Mime::LINEEND),
              array('=3F', '=20', '=5F', ''), $quotedValue);
          return '=?' . $this->_charset . '?Q?' . $quotedValue . '?=';
      }
    }

It is still not working with some hebrew letters / or combinations of letters in ZF 1.7.1. I am using Zend_Mail from ZF 1.7.1. I had to apply the above given patch, and now it is working. When are you going to update ZF with this patch?