ZF-11977: Zend_Mail::addHeader() UNfolds long headers, then throws exception
If you pass a properly folded mail header to Zend_Mail::addHeader(), it strips all CRLF when it calls _filterOther(). It *sometimes* encodes the header value (as either QP or Base64), but only if it contains non-printable characters. So there's a case where you have a long, but properly-folded header, with no special characters, but when you add it to your Zend_Mail and send, it throws an exception, because after stripping all the CRLF, the header (now a single line) is > 998 chars.
Code to reproduce:<?php require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance(); $mail = new Zend_Mail(); $foldedHeader = implode("\r\n ", array_fill(0, 100, "a line that is not, by itself, too long")); $mail->addHeader('X-Folded-Header-Test', $foldedHeader); $mail->setFrom('firstname.lastname@example.org') ->addTo('email@example.com') ->setSubject('Folded header test') ->setBodyText('This will throw a long header exception'); $mail->send(); ?>
For me, this became an issue because I'm using SendGrid's SMTP API, which accepts a special header containing parameters in JSON format, which can get quite long and needs to be folded -- but then Zend_Mail UNfolds it.
There are a number of temporary workarounds possible by subclassing Zend_Mail, but it seems that if it really needs to strip the CRLF (I assume there's a good reason), then _encodeHeader() should look at the header length, and *always* encode long values, even if they don't contain non-printable characters. At least, this was the response I got from my question here: http://stackoverflow.com/questions/8719322/…