roundcubemail-0.2.2-ja4.patch [ダウンロード]

diff -Nur roundcubemail-0.2.2/config/main.inc.php.dist roundcubemail-0.2.2-ja4/config/main.inc.php.dist
--- roundcubemail-0.2.2/config/main.inc.php.dist	2009-05-15 19:22:29.000000000 +0900
+++ roundcubemail-0.2.2-ja4/config/main.inc.php.dist	2009-06-02 11:24:47.000000000 +0900
@@ -192,6 +192,15 @@
 // Use this charset as fallback for message decoding
 $rcmail_config['default_charset'] = 'ISO-8859-1';
 
+// Use this message charset as compose message
+// Configure as a PHP style hash array: array('ja_JP' => 'ISO-2022-JP');
+$rcmail_config['message_charset_languages'] = array('en_US' => 'ISO-8859-1', 'ISO-8859-1' => 'ASCII', 'ko_KR' => 'EUC-KR', 'zh_TW' => 'BIG5', 'zh_CN' => 'GB2312', 'ja_JP' => 'ISO-2022-JP');
+
+// Use this message transfer encoding as compose message
+// Configure as a PHP style hash array: array('ja_JP' => 'base64');
+// transfer_encoding is 'base64' or 'quoted-printable'
+$rcmail_config['transfer_encoding_languages'] = array('en_US' => 'quoted-printable', 'en_GB' => 'quoted-printable', 'ko_KR' => 'base64', 'zh_TW' => 'base64', 'zh_CN' => 'base64', 'ja_JP' => 'base64');
+
 // Make use of the built-in spell checker. It is based on GoogieSpell.
 // Since Google only accepts connections over https your PHP installatation
 // requires to be compiled with Open SSL support
diff -Nur roundcubemail-0.2.2/installer/rcube_install.php roundcubemail-0.2.2-ja4/installer/rcube_install.php
--- roundcubemail-0.2.2/installer/rcube_install.php	2009-01-17 10:55:39.000000000 +0900
+++ roundcubemail-0.2.2-ja4/installer/rcube_install.php	2009-06-02 11:24:47.000000000 +0900
@@ -176,6 +176,18 @@
       else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
         $value = '%p';
       }
+      else if ($prop == 'default_imap_folders'){
+	$value = Array();
+	foreach($this->config['default_imap_folders'] as $_folders){
+	  switch($_folders){
+	  case 'Drafts': $_folders = $this->config['drafts_mbox']; break;
+	  case 'Sent':   $_folders = $this->config['sent_mbox']; break;
+	  case 'Junk':   $_folders = $this->config['junk_mbox']; break;
+	  case 'Trash':  $_folders = $this->config['trash_mbox']; break;
+          }
+	  if (!in_array($_folders, $value))  $value[] = $_folders;
+        }
+      }
       else if (is_bool($default)) {
         $value = (bool)$value;
       }
diff -Nur roundcubemail-0.2.2/program/include/rcube_imap.php roundcubemail-0.2.2-ja4/program/include/rcube_imap.php
--- roundcubemail-0.2.2/program/include/rcube_imap.php	2009-05-15 19:22:29.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/include/rcube_imap.php	2009-09-29 18:07:36.000000000 +0900
@@ -1403,6 +1403,11 @@
       {
       $mode = $o_part->encoding == 'base64' ? 3 : ($o_part->encoding == 'quoted-printable' ? 1 : 2);
       $body = iil_C_HandlePartBody($this->conn, $this->mailbox, $msg_id, $part, $mode);
+
+      // detect encoding if no charset specified (if text or message part)
+      if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
+        if (empty($o_part->charset))
+          $o_part->charset = rc_detect_encoding($body, $this->default_charset);
       
       // we have to decode the part manually before printing
       if ($mode == 1)
@@ -1425,9 +1430,9 @@
       // convert charset (if text or message part)
       if ($o_part->ctype_primary=='text' || $o_part->ctype_primary=='message')
         {
-        // assume default if no charset specified
+        // detect encoding if no charset specified
         if (empty($o_part->charset))
-          $o_part->charset = $this->default_charset;
+          $o_part->charset = rc_detect_encoding($body, $this->default_charset);
 
         $body = rcube_charset_convert($body, $o_part->charset);
         }
@@ -2573,7 +2578,12 @@
       // Loop through the string to decode all occurences of =? ?= into the variable $out 
       while(($pos = strpos($input, '=?')) !== false) {
         // Append everything that is before the text to be decoded
-        $out .= substr($input, 0, $pos);
+	$_before = substr($input, 0, $pos);
+	$_enc = rc_detect_encoding($_before, "ASCII");
+	if ($_enc != "ASCII"){
+		$_before = rcube_charset_convert($_before, $_enc);
+	}
+	$out .= $_before;
 
         // Get the location of the text to decode
         $end_cs_pos = strpos($input, "?", $pos+2);
diff -Nur roundcubemail-0.2.2/program/include/rcube_message.php roundcubemail-0.2.2-ja4/program/include/rcube_message.php
--- roundcubemail-0.2.2/program/include/rcube_message.php	2009-02-14 03:44:03.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/include/rcube_message.php	2009-06-02 11:24:47.000000000 +0900
@@ -67,6 +67,11 @@
     $this->uid = $uid;
     $this->headers = $this->imap->get_headers($uid, NULL, true, true);
 
+    $_encoding = "";
+    if (empty($this->headers->charset)){
+      $_encoding = rc_detect_encoding($this->headers->subject, "ASCII");
+      if ($_encoding != "ASCII")  $this->headers->charset = $_encoding;
+    }
     $this->subject = rcube_imap::decode_mime_string($this->headers->subject, $this->headers->charset);
     list(, $this->sender) = each($this->imap->decode_address_list($this->headers->from));
     
diff -Nur roundcubemail-0.2.2/program/include/rcube_shared.inc roundcubemail-0.2.2-ja4/program/include/rcube_shared.inc
--- roundcubemail-0.2.2/program/include/rcube_shared.inc	2009-05-15 19:22:29.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/include/rcube_shared.inc	2009-06-02 11:24:47.000000000 +0900
@@ -641,18 +641,57 @@
         return $failover;
     }
 
-    // FIXME: the order is important, because sometimes 
-    // iso string is detected as euc-jp and etc.
-    $enc = array(
-      'UTF-8', 'SJIS', 'BIG5', 'GB2312',
-      'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4',
-      'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
-      'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
-      'WINDOWS-1252', 'WINDOWS-1251', 'EUC-JP', 'EUC-TW', 'KOI8-R', 
-      'ISO-2022-KR', 'ISO-2022-JP'
-    );
+    switch ($_SESSION['language']){
+    case 'ja_JP': // for Japanese
+	$enc = array(
+	    'ASCII', 'ISO-2022-JP', 'JIS',
+	    'UTF-8', 
+	    'EUC-JP', 'eucJP-win', 'SJIS', 'SJIS-win',
+	);
+        break;
+
+    case 'zh_CN': // for Chinese (Simplified)
+    case 'zh_TW': // for Chinese (Traditional)',
+
+	$enc = array(
+	    'ASCII',
+	    'UTF-8', 
+	    'BIG5', 'GB2312', 'EUC-TW',
+	);
+        break;
+
+    case  'ko_KR': // for Korean
+	$enc = array(
+	    'ASCII',
+	    'UTF-8', 
+	    'EUC-KR', 'ISO-2022-KR',
+	);
+        break;
+
+    case 'ru_RU': // for Russian
+	$enc = array(
+	    'ASCII',
+	    'UTF-8', 
+	    'WINDOWS-1251', 'KOI8-R',
+	);
+        break;
+
+    default:
+	$enc = array(
+	    'ASCII',
+	    'UTF-8',
+	    'WINDOWS-1252', 
+	);
+        break;
+    }
 
     $result = mb_detect_encoding($string, join(',', $enc));
+    switch(strtoupper($result)) {
+    	case 'JIS'      : $result = "ISO-2022-JP"; break;
+    	case 'EUCJP-WIN': $result = "EUC-JP";      break;
+    	case 'SJIS' :
+    	case 'SJIS-WIN' : $result = "Shift_JIS";   break;
+    }
 
     return $result ? $result : $failover;
 }
diff -Nur roundcubemail-0.2.2/program/lib/Mail/mimePart.php roundcubemail-0.2.2-ja4/program/lib/Mail/mimePart.php
--- roundcubemail-0.2.2/program/lib/Mail/mimePart.php	2009-02-09 02:05:23.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/lib/Mail/mimePart.php	2009-06-24 17:40:58.000000000 +0900
@@ -47,7 +47,7 @@
  * @author     Sean Coates 
  * @copyright  2003-2006 PEAR 
  * @license    http://www.opensource.org/licenses/bsd-license.php BSD License
- * @version    CVS: $Id: mimePart.php 2281 2009-02-08 17:05:23Z alec $
+ * @version    CVS: $Id: mimePart.php 2663 2009-06-20 06:55:17Z alec $
  * @link       http://pear.php.net/package/Mail_mime
  */
 
@@ -185,8 +185,8 @@
 	
         if (isset($contentType['type'])) {
             $headers['Content-Type'] = $contentType['type'];
-	    if (isset($contentType['charset'])) {
-                $headers['Content-Type'] .= "; charset=\"{$contentType['charset']}\"";
+	    if (isset($contentType['charset']) && !preg_match("|charset|i", $contentType['type'])) {
+                $headers['Content-Type'] .= "; charset={$contentType['charset']}";
             }
             if (isset($contentType['name'])) {
                 $headers['Content-Type'] .= ';' . MAIL_MIMEPART_CRLF;
@@ -397,29 +397,36 @@
      */
     function _buildHeaderParam($name, $value, $charset=NULL, $language=NULL, $paramEnc=NULL, $maxLength=78)
     {
-        // RFC 2183/2184/2822: 
+        // RFC 2045: 
 	// value needs encoding if contains non-ASCII chars or is longer than 78 chars
         if (!preg_match('#[^\x20-\x7E]#', $value)) { // ASCII
-	    $quoted = addcslashes($value, '\\"');
-	    if (strlen($name) + strlen($quoted) + 6 <= $maxLength)
-		return " {$name}=\"{$quoted}\"; ";
+	    // token
+    	    if (!preg_match('#([^\x21,\x23-\x27,\x2A,\x2B,\x2D,\x2E,\x30-\x39,\x41-\x5A,\x5E-\x7E])#', $value)) {
+		if (strlen($name) + strlen($value) + 3 <= $maxLength)
+		    return " {$name}={$value};";
+	    } else { // quoted-string
+		$quoted = addcslashes($value, '\\"');
+		if (strlen($name) + strlen($quoted) + 5 <= $maxLength)
+		    return " {$name}=\"{$quoted}\";";
+	    }
 	}
 
-	// use quoted-printable/base64 encoding (RFC2047)
+	// RFC2047: use quoted-printable/base64 encoding
 	if ($paramEnc == 'quoted-printable' || $paramEnc == 'base64')
 	    return $this->_buildRFC2047Param($name, $value, $charset, $paramEnc);
 
-        $encValue = preg_replace('#([^\x20-\x7E])#e', '"%" . strtoupper(dechex(ord("\1")))', $value);
+	// RFC2231:
+        $encValue = preg_replace('#([^\x21,\x23,\x24,\x26,\x2B,\x2D,\x2E,\x30-\x39,\x41-\x5A,\x5E-\x7E])#e',
+			'"%" . __dechex_ord("\1")', $value);
         $value = "$charset'$language'$encValue";
 
-        $header = " {$name}*=\"{$value}\"; ";
+        $header = " {$name}*={$value};";
         if (strlen($header) <= $maxLength) {
             return $header;
         }
 
-        $preLength = strlen(" {$name}*0*=\"");
-        $sufLength = strlen("\";");
-        $maxLength = max(16, $maxLength - $preLength - $sufLength - 2);
+        $preLength = strlen(" {$name}*0*=");
+        $maxLength = max(16, $maxLength - $preLength - 3);
         $maxLengthReg = "|(.{0,$maxLength}[^\%][^\%])|";
 
         $headers = array();
@@ -428,11 +435,11 @@
             $matches = array();
             $found = preg_match($maxLengthReg, $value, $matches);
             if ($found) {
-                $headers[] = " {$name}*{$headCount}*=\"{$matches[0]}\"";
+                $headers[] = " {$name}*{$headCount}*={$matches[0]}";
                 $value = substr($value, strlen($matches[0]));
             } else {
-                $headers[] = " {$name}*{$headCount}*=\"{$value}\"";
-                $value = "";
+                $headers[] = " {$name}*{$headCount}*={$value}";
+                $value = '';
             }
             $headCount++;
         }
@@ -468,6 +475,11 @@
 		$quoted .= $ext;
 	    }
 	}
+        else if (function_exists("mb_encode_mimeheader")){
+            mb_internal_encoding($charset);
+            $quoted = mb_encode_mimeheader($value, $charset, ($encoding == 'base64' ? 'B' : 'Q'), MAIL_MIMEPART_CRLF);
+            mb_internal_encoding(RCMAIL_CHARSET);
+        }
 	else if ($encoding == 'base64')
 	{
 	    $ext = strrchr($value, '.');
@@ -499,9 +511,9 @@
 	    // Replace all extended characters (\x80-xFF) with their
 	    // ASCII values.
 	    $ext = preg_replace('/([\x80-\xFF])/e', 
-		'"=" . strtoupper(dechex(ord("\1")))', $ext);
+		'"=" . __dechex_ord("\1")', $ext);
 	    $value = preg_replace('/([\x80-\xFF])/e', 
-		'"=" . strtoupper(dechex(ord("\1")))', $value);
+		'"=" . __dechex_ord("\1")', $value);
 
             $prefix = '=?' . $charset . '?Q?';
             $suffix = '?=';
@@ -520,3 +532,9 @@
     }
 
 } // End of class
+
+// helper function
+function __dechex_ord( $c ){
+    return strtoupper(dechex(ord(stripslashes( $c ))));
+}
+
diff -Nur roundcubemail-0.2.2/program/lib/Mail/mime.php roundcubemail-0.2.2-ja4/program/lib/Mail/mime.php
--- roundcubemail-0.2.2/program/lib/Mail/mime.php	2008-10-25 09:01:45.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/lib/Mail/mime.php	2009-06-24 17:41:27.000000000 +0900
@@ -358,6 +358,12 @@
         if (PEAR::isError($filedata)) {
             return $filedata;
         }
+        if (preg_match("|^text/plain|i", $c_type) && !preg_match("|charset|i", $c_type)){
+            $_e = rc_detect_encoding($filedata, "");
+            if (!empty($_e)){
+                $c_type = "text/plain; charset=${_e}";
+            }
+        }
 
         $this->_parts[] = array(
                                 'body'        => $filedata,
diff -Nur roundcubemail-0.2.2/program/steps/mail/func.inc roundcubemail-0.2.2-ja4/program/steps/mail/func.inc
--- roundcubemail-0.2.2/program/steps/mail/func.inc	2009-05-15 19:22:29.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/steps/mail/func.inc	2009-06-02 11:24:47.000000000 +0900
@@ -402,6 +402,10 @@
     if (empty($header))
       continue;
 
+    if (empty($header->charset)){
+      $_encoding = rc_detect_encoding($header->subject, "ASCII");
+      if ($_encoding != "ASCII")  $header->charset = $_encoding;
+    }
     $IMAP->set_charset(!empty($header->charset) ? $header->charset : $CONFIG['default_charset']);
 
     // remove 'attachment' and 'flag' columns, we don't need them here
diff -Nur roundcubemail-0.2.2/program/steps/mail/get.inc roundcubemail-0.2.2-ja4/program/steps/mail/get.inc
--- roundcubemail-0.2.2/program/steps/mail/get.inc	2009-02-09 05:38:54.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/steps/mail/get.inc	2009-06-02 11:24:47.000000000 +0900
@@ -74,6 +74,11 @@
         header("Content-Type: application/force-download");
     }
     else if ($ctype_primary == 'text') {
+      if (empty($part->charset)){
+        // get part body if not available and detect part charset
+        if (!$part->body)
+          $part->body = $MESSAGE->get_part_content($part->mime_id);
+      }
       header("Content-Type: text/$ctype_secondary; charset=" . ($part->charset ? $part->charset : RCMAIL_CHARSET));
     }
     else {
diff -Nur roundcubemail-0.2.2/program/steps/mail/sendmail.inc roundcubemail-0.2.2-ja4/program/steps/mail/sendmail.inc
--- roundcubemail-0.2.2/program/steps/mail/sendmail.inc	2009-05-15 19:22:29.000000000 +0900
+++ roundcubemail-0.2.2-ja4/program/steps/mail/sendmail.inc	2009-10-13 16:58:59.000000000 +0900
@@ -138,12 +138,13 @@
 }
 
 // parse email address input
-function rcmail_mailto_format($mailto)
+function rcmail_mailto_format($mailto, $message_charset = RCMAIL_CHARSET, $message_transfer_encoding = 'quoted-printable')
 {
   $regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/', '/(\S{1})(<\S+@\S+>)/U');
   $replace = array(', ', ', ', '', ',', '\\1 \\2');
 
   // replace new lines and strip ending ', ', make address input more valid
+  $mailto = rcube_charset_convert($mailto, $message_charset, 'UTF-8');
   $mailto = trim(preg_replace($regexp, $replace, $mailto));
 
   $result = array();
@@ -153,10 +154,10 @@
     $item = trim($item);
     // address in brackets without name (do nothing)
     if (preg_match('/^<\S+@\S+>$/', $item)) {
-      $result[] = $item;
+      $result[] = Array('', $item);
     // address without brackets and without name (add brackets)
     } else if (preg_match('/^\S+@\S+$/', $item)) {
-      $result[] = '<'.$item.'>';
+      $result[] = Array('', '<'.$item.'>');
     // address with name (handle name)
     } else if (preg_match('/\S+@\S+>*$/', $item, $matches)) {
       $address = $matches[0];
@@ -169,15 +170,53 @@
       if (!preg_match('/^<\S+@\S+>$/', $address))
         $address = '<'.$address.'>';
 
-      $result[] = $name.' '.$address;
+      $result[] = Array($name, $address);
+
     } else if (trim($item)) {
       // @TODO: handle errors
     }
   }
 
-  return implode(', ', $result);
+  $mailto = array();
+  if (function_exists("mb_encode_mimeheader")) {
+    mb_internal_encoding('UTF-8');
+    foreach($result as $item){
+      if (empty($item[0])){
+	$mailto[] = $item[1];
+      } else {
+        $mailto[] = mb_encode_mimeheader($item[0], $message_charset, ($message_transfer_encoding == 'base64' ? 'B' : 'Q')) .' '.$item[1];
+      }
+    }
+    mb_internal_encoding(RCMAIL_CHARSET);
+  } else {
+    foreach($result as $item){
+      if (empty($item[0])){
+	$mailto[] = $item[1];
+      } else {
+        $mailto[] = $item[0].' '.$item[1];
+      }
+    }
+  }
+
+  return implode(', ', $mailto);
 }
 
+function strip_unknown_charset($ctype){
+    if (preg_match('/charset/', $ctype)){
+	$ctypes = explode(";", $ctype);
+	$ctype = "";
+        foreach($ctypes as $_s){
+            $_s = trim($_s);
+            if (!empty($_s) && ! preg_match('/^charset=.*unknown/i', $_s)){
+                $ctype .= "{$_s}; ";
+            }
+        }
+    }
+
+    return $ctype;
+}
+    
+
 /****** compose message ********/
 
 if (strlen($_POST['_draft_saveid']) > 3)
@@ -187,11 +226,18 @@
 
 // set default charset
 $input_charset = $OUTPUT->get_charset();
+$message_transfer_encoding = 'quoted-printable';
+if (array_key_exists($_SESSION['language'], $CONFIG['message_charset_languages'])){
+  $input_charset = $CONFIG['message_charset_languages'][$_SESSION['language']];
+  if (array_key_exists($_SESSION['language'], $CONFIG['transfer_encoding_languages'])){
+    $message_transfer_encoding = $CONFIG['transfer_encoding_languages'][$_SESSION['language']];
+  }
+}
 $message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
 
-$mailto = rcmail_mailto_format(get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset));
-$mailcc = rcmail_mailto_format(get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset));
-$mailbcc = rcmail_mailto_format(get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset));
+$mailto = rcmail_mailto_format(get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset), $message_charset, $message_transfer_encoding);
+$mailcc = rcmail_mailto_format(get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset), $message_charset, $message_transfer_encoding);
+$mailbcc = rcmail_mailto_format(get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset), $message_charset, $message_transfer_encoding);
 
 if (empty($mailto) && !empty($mailcc)) {
   $mailto = $mailcc;
@@ -212,7 +258,7 @@
 
 // compose headers array
 $headers = array('Date' => date('r'),
-                 'From' => rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset),
+                 'From' => rcmail_mailto_format(rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset), $message_charset, $message_transfer_encoding),
                  'To'   => $mailto);
 
 // additional recipients
@@ -232,7 +278,7 @@
   $headers['Organization'] = $identity_arr['organization'];
 
 if (!empty($_POST['_replyto']))
-  $headers['Reply-To'] = rcmail_mailto_format(get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset));
+  $headers['Reply-To'] = rcmail_mailto_format(get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset), $message_charset, $message_transfer_encoding);
 else if (!empty($identity_arr['reply-to']))
   $headers['Reply-To'] = $identity_arr['reply-to'];
 
@@ -348,16 +394,16 @@
     else
     {
       $ctype = str_replace('image/pjpeg', 'image/jpeg', $attachment['mimetype']); // #1484914
+      $ctype = strip_unknown_charset($ctype);
 
       // .eml attachments send inline
-      $MAIL_MIME->addAttachment($attachment['path'],
-        $ctype, 
-        $attachment['name'], true, 
+      $MAIL_MIME->addAttachment($attachment['path'], $ctype, 
+        rcube_charset_convert($attachment['name'], RCMAIL_CHARSET, $message_charset), true, 
         ($ctype == 'message/rfc822' ? $transfer_encoding : 'base64'),
         ($ctype == 'message/rfc822' ? 'inline' : 'attachment'),
         $message_charset, '', '', 
-	$CONFIG['mime_param_folding'] ? 'quoted-printable' : NULL,
-	$CONFIG['mime_param_folding'] == 2 ? 'quoted-printable' : NULL
+	$CONFIG['mime_param_folding'] ? $message_transfer_encoding : NULL,
+	$CONFIG['mime_param_folding'] == 2 ? $message_transfer_encoding : NULL
 	);
     }
   }
@@ -368,12 +414,15 @@
     {
     $ctype = $files['type'][$i];
     $ctype = str_replace('image/pjpeg', 'image/jpeg', $ctype); // #1484914
+    $ctype = strip_unknown_charset($ctype);
     
-    $MAIL_MIME->addAttachment($filepath, $ctype, $files['name'][$i], true,
+    $MAIL_MIME->addAttachment($filepath, $ctype,
+        rcube_charset_convert($files['name'][$i], RCMAIL_CHARSET, $message_charset), true,
 	$ctype == 'message/rfc822' ? $transfer_encoding : 'base64',
-	'attachment', $message_charset, '', '', 
-	$CONFIG['mime_param_folding'] ? 'quoted-printable' : NULL,
-	$CONFIG['mime_param_folding'] == 2 ? 'quoted-printable' : NULL
+	'attachment',
+        $message_charset, '', '', 
+	$CONFIG['mime_param_folding'] ? $message_transfer_encoding : NULL,
+	$CONFIG['mime_param_folding'] == 2 ? $message_transfer_encoding : NULL
 	);
     }
 
@@ -392,7 +441,7 @@
 if (function_exists("mb_encode_mimeheader"))
 {
   mb_internal_encoding($message_charset);
-  $headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset, 'Q');
+  $headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset, ($message_transfer_encoding == 'base64' ? 'B' : 'Q'));
   mb_internal_encoding(RCMAIL_CHARSET);
 }