From 0e7cfad31e708f9b82d1dae3ee80defa5b19e406 Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Tue, 11 Jun 2013 12:48:13 -0400
Subject: [PATCH] Fixed charset issue in mail library.

---
 interface/lib/classes/ispcmail.inc.php |   52 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/interface/lib/classes/ispcmail.inc.php b/interface/lib/classes/ispcmail.inc.php
index e30a711..e9534ec 100644
--- a/interface/lib/classes/ispcmail.inc.php
+++ b/interface/lib/classes/ispcmail.inc.php
@@ -414,16 +414,22 @@
             $this->body = "This is a multi-part message in MIME format.\n\n";
             
             if($text) {
-                $this->body .= "--{$this->mime_boundary}\n" .
+                /*$this->body .= "--{$this->mime_boundary}\n" .
                               "Content-Type:text/plain; charset=\"" . strtolower($this->mail_charset) . "\"\n" .
-                              "Content-Transfer-Encoding: 7bit\n\n" . $this->text_part . "\n\n";
-            }
+                              "Content-Transfer-Encoding: 7bit\n\n" . $this->text_part . "\n\n";*/
+				$this->body .= "--{$this->mime_boundary}\n" .
+                              "Content-Type:text/plain; charset=\"UTF-8\"\n" .
+                              "Content-Transfer-Encoding: 8bit\n\n" . $this->text_part . "\n\n";
+			}
             
             if($html) {
-                $this->body .= "--{$this->mime_boundary}\n" .
+                /*$this->body .= "--{$this->mime_boundary}\n" .
                                "Content-Type:text/html; charset=\"" . strtolower($this->mail_charset) . "\"\n" . 
-                               "Content-Transfer-Encoding: 7bit\n\n" . $this->html_part . "\n\n";
-            }
+                               "Content-Transfer-Encoding: 7bit\n\n" . $this->html_part . "\n\n";*/
+				$this->body .= "--{$this->mime_boundary}\n" .
+                              "Content-Type:text/html; charset=\"UTF-8\"\n" .
+                              "Content-Transfer-Encoding: 8bit\n\n" . $this->html_part . "\n\n";
+			}
             
             if($attach) {
                 foreach($this->attachments as $att) {
@@ -496,11 +502,32 @@
     * @access private
     */
     private function _encodeHeader($input, $charset = 'ISO-8859-1') {
-        preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
-        foreach ($matches[1] as $value) {
-            $replacement = preg_replace('/([\x20\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
-            $input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
-        }
+		preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
+		foreach ($matches[1] as $value) {
+			$replacement = preg_replace('/([\x20\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
+			$input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
+		}
+        
+        return $input;
+    }
+	
+	/**
+    * Function to encode the subject if necessary
+    * according to RFC2047
+    * @access private
+    */
+    private function _encodeSubject($input, $charset = 'ISO-8859-1') {
+        /*
+		if($charset == 'UTF-8' && function_exists('imap_8bit')) {
+			$input = "=?utf-8?Q?" . imap_8bit($input) . "?=";
+		} else {
+			preg_match_all('/(\s?\w*[\x80-\xFF]+\w*\s?)/', $input, $matches);
+			foreach ($matches[1] as $value) {
+				$replacement = preg_replace('/([\x20\x80-\xFF])/e', '"=" . strtoupper(dechex(ord("\1")))', $value);
+				$input = str_replace($value, '=?' . $charset . '?Q?' . $replacement . '?=', $input);
+			}
+		}*/
+		$input='=?UTF-8?B?'.base64_encode($input).'?=';
         
         return $input;
     }
@@ -578,7 +605,8 @@
             //$subject = $this->_encodeHeader($this->headers['Subject'], $this->mail_charset);
             $subject = $this->headers['Subject'];
             
-            $enc_subject = $this->_encodeHeader($subject, $this->mail_charset);
+            //$enc_subject = $this->_encodeHeader($subject, $this->mail_charset);
+			$enc_subject = $this->_encodeSubject($subject, $this->mail_charset);
             unset($this->headers['Subject']);
         }
         

--
Gitblit v1.9.1