From df03e7754c19d2e4ef6f04d860803c0030ccc932 Mon Sep 17 00:00:00 2001
From: Carlos Alberto Lopez Perez <clopez@igalia.com>
Date: Fri, 26 Mar 2010 12:42:55 +0100
Subject: [PATCH] Use quoted-printable transfer enconding for text emails
 And wraps the body text with a new function that will not
 cut lines when the text is part of a reply

---
 program/steps/mail/sendmail.inc |   53 ++++++++++++++++++++++++++++++++++++--
 1 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index afd1735..a9efaa1 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -52,9 +52,54 @@ if (!$savedraft) {
   }
 }
 
-
 /****** message sending functions ********/
 
+// wrap a long paragraph into smaller chunks without cutting words
+function wrap_paragraph($str_var, $width, $break)
+{
+    $cut =true;
+    $i=0;
+    $space=" ";
+    $strlen_var=strlen($str_var);
+    for ($c = 0; $c < $strlen_var; ++$c)
+    {
+        // if cut is enabled line width exceeded and is a space cut
+        if (($i > $width) && ($str_var[$c] == $space) && ($cut == true))
+        {
+            // we are cutting the line so reset line width
+            $i = 0;
+            $newstr .= $space;
+            // insert the line break
+            $newstr .= $break;
+        }
+        // add current char to string if we are not cutting
+        else $newstr .= $str_var[$c];
+
+        // now will check for quoted text for disable or enable cutting
+        if ($str_var[$c] == "\n")
+        {
+            // if the next line is a quoted text disable cut
+            if ($str_var[$c+1] == ">")
+            {
+                $cut = false;
+            }
+            // else enable it
+            else
+            {
+                $cut = true;
+            }
+            // there is a new line so reset line width
+            $i = 0;
+        }
+        // make line width grow
+        $i++;
+
+    }
+    // return the string wrapped
+    return $newstr;
+}
+
+
 // encrypt parts of the header
 function rcmail_encrypt_header($what)
 {
@@ -381,10 +426,12 @@ if ($isHtml) {
 }
 else
   {
-  $message_body = rc_wordwrap($message_body, 75, "\r\n");
+  $message_body = wrap_paragraph($message_body, 75, "\r\n");
+  // This ads EOL when line width is 75 or longer without cutting words
   if ($footer)
     $message_body .= "\r\n" . $footer;
   $message_body = wordwrap($message_body, 998, "\r\n", true);
+  // This ads EOL when line width is 998 cutting words if its needed
   if (!strlen($message_body)) { 
     // empty message body breaks attachment handling in drafts 
     $message_body = "\r\n"; 
@@ -450,7 +497,7 @@ if (is_array($_FILES['_attachments']['tmp_name'])) {
 
 // encoding settings for mail composing
 $MAIL_MIME->setParam(array(
-  'text_encoding' => $transfer_encoding,
+  'text_encoding' => 'quoted-printable',
   'html_encoding' => 'quoted-printable',
   'head_encoding' => 'quoted-printable',
   'head_charset'  => $message_charset,
-- 
1.6.3.3

