From 490c178d5446ebba95fb820b71ebd9acf7130ca7 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Wed, 20 Jun 2012 14:12:45 +0300 Subject: [PATCH] use correct q-encoding for email subject --- ipf/mail.php | 7 +++++-- ipf/mime.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ipf/mail.php b/ipf/mail.php index 1887d9d..98f6d2b 100644 --- a/ipf/mail.php +++ b/ipf/mail.php @@ -259,8 +259,11 @@ class IPF_Mail extends IPF_Mime_Message public function setSubject($subject) { if ($this->_subject === null) { - $subject = strtr($subject,"\r\n\t",'???'); - $this->_subject = $this->_encodeHeader($subject); + $subject = strtr($subject,"\r\n\t", '???'); + if (IPF_Mime::isPrintable($subject)) + $this->_subject = $subject; + else + $this->_subject = IPF_Mime::encodeQ($subject); $this->_storeHeader('Subject', $this->_subject); } else { throw new IPF_Exception_Mail('Subject set twice'); diff --git a/ipf/mime.php b/ipf/mime.php index ce6543a..a9c23c3 100644 --- a/ipf/mime.php +++ b/ipf/mime.php @@ -114,6 +114,46 @@ class IPF_Mime return $out; } + public static function encodeQ($str) + { + $str = str_replace('=', '=3D', $str); + $str = str_replace(self::$qpKeys, self::$qpReplaceValues, $str); + $str = str_replace(array('?', '_'), array('=3F', '=5F'), $str); + $str = str_replace(' ', '_', $str); + + $result = array(); + + // Split encoded text into separate lines + $lineLength = 75 - 12; + $pos = 0; + $length = strlen($str); + while ($pos < $length) + { + if ($length - $pos > $lineLength) + { + $ptr = $lineLength; + + // Ensure we are not splitting across an encoded character + if (substr($str, $pos + $ptr - 2) == '=') + $ptr -= 2; + elseif (substr($str, $pos + $ptr - 1) == '=') + $ptr -= 1; + + // Add string and continue + $out = substr($str, $pos, $ptr); + $pos += $ptr; + } + else + { + $out = substr($str, $pos); + $pos = $length; + } + $result[] = '=?utf-8?Q?' . $out . '?='; + } + + return implode("\r\n ", $result); + } + public static function encodeBase64($str, $lineLength = self::LINELENGTH, $lineEnd = self::LINEEND) -- 2.49.0