]> git.andy128k.dev Git - ipf-mail.git/commitdiff
use correct q-encoding for email subject
authorAndrey Kutejko <andy128k@gmail.com>
Wed, 20 Jun 2012 11:12:45 +0000 (14:12 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Wed, 20 Jun 2012 11:12:45 +0000 (14:12 +0300)
ipf/mail.php
ipf/mime.php

index 1887d9dff2a59b0dd38d6544fd56d94ecd221209..98f6d2bbed6fc20c98154237803eda02569b9cbf 100644 (file)
@@ -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');
index ce6543a59dde020037cb5207a5719990a78b4cba..a9c23c36cd8b64f6068f233a4b7fb06c242123cd 100644 (file)
@@ -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)