From e9155a000b93c3293f7f4322a16c6bbf8d7d8eb6 Mon Sep 17 00:00:00 2001 From: Klaus Leithoff Date: Thu, 10 Nov 2016 12:50:51 +0000 Subject: [PATCH] * Mail/Z-Push: handle charset-problem on sending mails with added EGW-Signature --- api/src/Mailer.php | 76 ++++++++++++++++++++++++++++++- mail/inc/class.mail_zpush.inc.php | 7 +++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/api/src/Mailer.php b/api/src/Mailer.php index 7b69cb5813..e6d3f8c166 100644 --- a/api/src/Mailer.php +++ b/api/src/Mailer.php @@ -65,6 +65,7 @@ class Mailer extends Horde_Mime_Mail * * @param int|Mail\Account|boolean $account =null mail account to use, default use Mail\Account::get_default($smtp=true) * false: no NOT initialise account and set other EGroupware specific headers, used to parse mails (not sending them!) + * initbasic: return $this */ function __construct($account=null) { @@ -72,7 +73,14 @@ class Mailer extends Horde_Mime_Mail Preferences::setlocale(LC_MESSAGES); parent::__construct(); - + if ($account ==='initbasic') + { + $this->_headers = new Horde_Mime_Headers(); + $this->clearAllRecipients(); + $this->clearReplyTos(); + error_log(__METHOD__.__LINE__.array2string($this)); + return $this; + } if ($account !== false) { $this->_headers->setUserAgent('EGroupware API '.$GLOBALS['egw_info']['server']['versions']['api']); @@ -678,6 +686,72 @@ class Mailer extends Horde_Mime_Mail $this->getBasePart()->toString(array('canonical' => true)); } + /** + * Convert charset of text-parts of message to utf-8. non static AND include Bcc + * + * @param string|resource $message + * @param boolean $stream =false return stream or string (default) + * @param string $charset ='utf-8' charset to convert to + * @param boolean &$converted =false on return if conversation was necessary + * @return string|stream + */ + function convertMessageTextParts($message, $stream=false, $charset='utf-8', &$converted=false) + { + $headers = Horde_Mime_Headers::parseHeaders($message); + $this->addHeaders($headers); + $base = Horde_Mime_Part::parseMessage($message); + foreach($headers->toArray(array('nowrap' => true)) as $header => $value) + { + foreach((array)$value as $n => $val) + { + switch($header) + { + case 'Bcc': + case 'bcc': + //error_log(__METHOD__.__LINE__.':'.$header.'->'.$val); + $this->addBcc($val); + break; + } + } + } + foreach($base->partIterator() as $part) + { + if ($part->getPrimaryType()== 'text') + { + $charset = $part->getContentTypeParameter('charset'); + if ($charset && $charset != 'utf-8') + { + $content = Translation::convert($part->toString(array( + 'encode' => Horde_Mime_Part::ENCODE_BINARY, // otherwise we cant recode charset + )), $charset, 'utf-8'); + $part->setContents($content, array( + 'encode' => Horde_Mime_Part::ENCODE_BINARY, // $content is NOT encoded + )); + $part->setContentTypeParameter('charset', 'utf-8'); + if ($part === $base) + { + $this->addHeader('Content-Type', $part->getType(true)); + // need to set Transfer-Encoding used by base-part, it always seems to be "quoted-printable" + $this->addHeader('Content-Transfer-Encoding', 'quoted-printable'); + } + $converted = true; + } + } + elseif ($part->getType() == 'message/rfc822') + { + $mailerWithIn = new Mailer('initbasic'); + $part->setContents($mailerWithIn->convertMessageTextParts($part->toString(), $stream, $charset, $converted)); + } + } + if ($converted) + { + $this->setBasePart($base); + $this->forceBccHeader(); + return $this->getRaw($stream); + } + return $message; + } + /** * Convert charset of text-parts of message to utf-8 * diff --git a/mail/inc/class.mail_zpush.inc.php b/mail/inc/class.mail_zpush.inc.php index 79e831abdc..26ead728c8 100644 --- a/mail/inc/class.mail_zpush.inc.php +++ b/mail/inc/class.mail_zpush.inc.php @@ -434,8 +434,15 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail, $force8bit=false; if (Api\Translation::detect_encoding($sigTextPlain)!='ascii') $force8bit=true; + // beware. the section below might cause trouble regarding bcc and attachments, so maybe this is to be handeled differently + if ($force8bit) + { + $converterObj = new Api\Mailer('initbasic'); + $smartdata->mime = $converterObj->convertMessageTextParts($smartdata->mime,false,'utf-8'); + } // initialize the new Api\Mailer object for sending $mailObject = new Api\Mailer(self::$profileID); + $this->mail->parseRawMessageIntoMailObject($mailObject,$smartdata->mime,$force8bit); // Horde SMTP Class uses utf-8 by default. as we set charset always to utf-8 $mailObject->Sender = $activeMailProfile['ident_email'];