From 6969120d8b11bbf36be3166351a696f137870064 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Wed, 11 Nov 2015 12:25:43 +0000 Subject: [PATCH] z-push requires text-parts to be in utf-8, not eg. iso-8859-1 --- mail/inc/class.mail_zpush.inc.php | 3 +- phpgwapi/inc/class.egw_mailer.inc.php | 62 ++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 8 deletions(-) diff --git a/mail/inc/class.mail_zpush.inc.php b/mail/inc/class.mail_zpush.inc.php index e67f933d39..b55df5000f 100644 --- a/mail/inc/class.mail_zpush.inc.php +++ b/mail/inc/class.mail_zpush.inc.php @@ -934,7 +934,8 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail, //SYNC_BODYPREFERENCE_MIME ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__." bodypreference 4 requested"); $output->asbody->type = SYNC_BODYPREFERENCE_MIME;//4; - $Body = $this->mail->getMessageRawBody($id, '', $_folderName); + // use egw_mailer::convert to convert charset of all text parts to utf-8, which is a z-push or AS requirement! + $Body = egw_mailer::convert($this->mail->getMessageRawBody($id, '', $_folderName)); if ($this->debugLevel>2) debugLog(__METHOD__.__LINE__." Setting Mailobjectcontent to output:".$Body); $output->asbody->data = $Body; } diff --git a/phpgwapi/inc/class.egw_mailer.inc.php b/phpgwapi/inc/class.egw_mailer.inc.php index 687ad8de08..1af391f8bd 100644 --- a/phpgwapi/inc/class.egw_mailer.inc.php +++ b/phpgwapi/inc/class.egw_mailer.inc.php @@ -52,7 +52,8 @@ class egw_mailer extends Horde_Mime_Mail /** * Constructor: always throw exceptions instead of echoing errors and EGw pathes * - * @param int|emailadmin_account $account =null mail account to use, default use emailadmin_account::get_default($smtp=true) + * @param int|emailadmin_account|boolean $account =null mail account to use, default use emailadmin_account::get_default($smtp=true) + * false: no NOT initialise account and set other EGroupware specific headers, used to parse mails (not sending them!) */ function __construct($account=null) { @@ -60,16 +61,20 @@ class egw_mailer extends Horde_Mime_Mail common::setlocale(LC_MESSAGES); parent::__construct(); - $this->_headers->setUserAgent('EGroupware API '.$GLOBALS['egw_info']['server']['versions']['phpgwapi']); - $this->setAccount($account); + if ($account !== false) + { + $this->_headers->setUserAgent('EGroupware API '.$GLOBALS['egw_info']['server']['versions']['phpgwapi']); - $this->is_html = false; + $this->setAccount($account); - $this->clearAllRecipients(); - $this->clearReplyTos(); + $this->is_html = false; - $this->clearParts(); + $this->clearAllRecipients(); + $this->clearReplyTos(); + + $this->clearParts(); + } } /** @@ -640,6 +645,49 @@ class egw_mailer extends Horde_Mime_Mail $this->getBasePart()->toString(array('canonical' => true)); } + /** + * Convert charset of text-parts of message to utf-8 + * + * @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 + */ + static function convert($message, $stream=false, $charset='utf-8', &$converted=false) + { + $mailer = new egw_mailer(false); // false = no default headers and mail account + $mailer->addHeaders(Horde_Mime_Headers::parseHeaders($message)); + $base = Horde_Mime_Part::parseMessage($message); + 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); + $part->setContentTypeParameter('charset', 'utf-8'); + if ($part === $base) $mailer->addHeader('Content-Type', $base->getType(true)); + $converted = true; + } + } + elseif ($part->getType() == 'message/rfc822') + { + $part->setContents(self::convert($part->toString(), $stream, $charset, $converted)); + } + } + if ($converted) + { + $mailer->setBasePart($base); + return $mailer->getRaw($stream); + } + return $message; + } + /** * Find body: 1. part with mimetype "text/$subtype" *