From e1b8f44e3f635a757d0d13cc2ac908a175593875 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 05 Aug 2014 07:07:53 -0400
Subject: [PATCH] Fix invalid Content-Type header when send_format_flowed=false (#1489992)

---
 program/lib/Mail/mime.php       |   23 ++++++++++++++---------
 CHANGELOG                       |    1 +
 program/steps/mail/sendmail.inc |    6 +-----
 3 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 70b6c7e..3200879 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -45,6 +45,7 @@
 - Fix errors when using localStorage in Safari's private browsing mode (#1489996)
 - Fix bug where $Forwarded flag was being set even if server didn't support it (#1490000)
 - Fix various iCloud vCard issues, added fallback for external photos (#1489993)
+- Fix invalid Content-Type header when send_format_flowed=false (#1489992)
 
 RELEASE 1.0.2
 -------------
diff --git a/program/lib/Mail/mime.php b/program/lib/Mail/mime.php
index 50297dd..db0fd1f 100644
--- a/program/lib/Mail/mime.php
+++ b/program/lib/Mail/mime.php
@@ -1387,19 +1387,24 @@
 
         if ($headers['Content-Type'] == 'text/plain') {
             // single-part message: add charset and encoding
-            $charset = 'charset=' . $this->_build_params['text_charset'];
-            // place charset parameter in the same line, if possible
-            // 26 = strlen("Content-Type: text/plain; ")
-            $headers['Content-Type']
-                .= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset";
+            if ($this->_build_params['text_charset']) {
+                $charset = 'charset=' . $this->_build_params['text_charset'];
+                // place charset parameter in the same line, if possible
+                // 26 = strlen("Content-Type: text/plain; ")
+                $headers['Content-Type']
+                    .= (strlen($charset) + 26 <= 76) ? "; $charset" : ";$eol $charset";
+            }
+
             $headers['Content-Transfer-Encoding']
                 = $this->_build_params['text_encoding'];
         } else if ($headers['Content-Type'] == 'text/html') {
             // single-part message: add charset and encoding
-            $charset = 'charset=' . $this->_build_params['html_charset'];
-            // place charset parameter in the same line, if possible
-            $headers['Content-Type']
-                .= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset";
+            if ($this->_build_params['html_charset']) {
+                $charset = 'charset=' . $this->_build_params['html_charset'];
+                // place charset parameter in the same line, if possible
+                $headers['Content-Type']
+                    .= (strlen($charset) + 25 <= 76) ? "; $charset" : ";$eol $charset";
+            }
             $headers['Content-Transfer-Encoding']
                 = $this->_build_params['html_encoding'];
         } else {
diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index cdffd18..bac7512 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -484,15 +484,11 @@
     $transfer_encoding = $RCMAIL->config->get('force_7bit') ? 'quoted-printable' : '8bit';
 }
 else {
-    $text_charset      = '';
+    $text_charset      = 'US-ASCII';
     $transfer_encoding = '7bit';
 }
 
 if ($flowed) {
-    if (!$text_charset) {
-        $text_charset = 'US-ASCII';
-    }
-
     $text_charset .= ";\r\n format=flowed";
 }
 

--
Gitblit v1.9.1