From 7e8e923f9382c30776c2983fc4ae90eeadf0eb64 Mon Sep 17 00:00:00 2001
From: Andreas Gohr <gohr@cosmocode.de>
Date: Thu, 10 Feb 2011 14:16:44 +0100
Subject: [PATCH] Use Base64 encoding for long subjects FS#2169

Quoted-Printable specifies a maximum line length and some mail tools
(Apple mail and Thunderbird) take this quite serious and will fail to
decode subjects encoded with quoted-printable when the subject exceeds
the length limit.

The correct fix would be to wrap the header into multiple lines. But
this seems not to be possible with mails() $subject variable.

This patch switches to Base64 encoding for long subjects.

A general decision if switching completely to Base64 is the best way
to go is still open. (see bugreport)
---
 inc/mail.php | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/inc/mail.php b/inc/mail.php
index c45a7c57e..f991909d0 100644
--- a/inc/mail.php
+++ b/inc/mail.php
@@ -112,9 +112,16 @@ function _mail_send_action($data) {
     }
 
     if(!utf8_isASCII($subject)) {
-        $subject = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
+        $enc_subj = '=?UTF-8?Q?'.mail_quotedprintable_encode($subject,0).'?=';
         // Spaces must be encoded according to rfc2047. Use the "_" shorthand
-        $subject = preg_replace('/ /', '_', $subject);
+        $enc_sub = preg_replace('/ /', '_', $enc_sub);
+
+        // quoted printable has length restriction, use base64 if needed
+        if(strlen($subject) > 74){
+            $enc_subj = '=?UTF-8?B?'.base64_encode($subject).'?=';
+        }
+
+        $subject = $enc_subj;
     }
 
     $header  = '';
-- 
GitLab