From b54abd8fc4203062379bbdf6467d21fcc5c2da59 Mon Sep 17 00:00:00 2001 From: Michael J Rubinsky Date: Thu, 27 Aug 2020 13:34:10 -0400 Subject: [PATCH] Rewrite SMARTFORWARD handling; We weren't really properly forwarding the email here, but rather doing a sort-of modified Reply. Change that to now correctly attach the original mesasge as a message/rfc822 message part. --- lib/Horde/Core/ActiveSync/Mail.php | 109 ++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 26 deletions(-) diff --git a/lib/Horde/Core/ActiveSync/Mail.php b/lib/Horde/Core/ActiveSync/Mail.php index bd4515d5e..da817ef47 100644 --- a/lib/Horde/Core/ActiveSync/Mail.php +++ b/lib/Horde/Core/ActiveSync/Mail.php @@ -369,7 +369,6 @@ protected function _tryWithoutBinary($recipients, array $headers) */ protected function _sendSmart() { - $mime_message = $this->_raw->getMimeObject(); // Need to remove content-type header from the incoming raw message // since in a smart request, we actually construct the full MIME msg // ourselves and the content-type in _headers only applies to the reply @@ -377,17 +376,22 @@ protected function _sendSmart() $this->_headers->removeHeader('Content-Type'); $this->_headers->removeHeader('Content-Transfer-Encoding'); - // Check for EAS 16.0 Forwardees - if (!empty($this->_forwardees)) { - $list = new Horde_Mail_Rfc822_List(); - foreach ($this->_forwardees as $forwardee) { - $to = new Horde_Mail_Rfc822_Address($forwardee->email); - $to->personal = $forwardee->name; - $list->add($to); - } - $this->_headers->add('To', $list->writeAddress()); + if ($this->_forward) { + $mail = $this->_doSmartForward(); + } else { + $mail = $this->_doSmartReply(); + } + + try { + $mail->send($GLOBALS['injector']->getInstance('Horde_Mail')); + $this->_mailer = $mail; + } catch (Horde_Mime_Exception $e) { + throw new Horde_ActiveSync_Exception($e); } + } + protected function _doSmartReply() + { $mail = new Horde_Mime_Mail($this->_headers->toArray(array('charset' => 'UTF-8'))); $base_part = $this->imapMessage->getStructure(); $plain_id = $base_part->findBody('plain'); @@ -401,23 +405,57 @@ protected function _sendSmart() } catch (Horde_Exception_NotFound $e) { throw new Horde_ActiveSync_Exception($e->getMessage()); } + + $mime_message = $this->_raw->getMimeObject(); if (!empty($html_id)) { $mail->setHtmlBody($this->_getHtmlPart($html_id, $mime_message, $body_data, $base_part)); } elseif (!empty($plain_id)) { $mail->setBody($this->_getPlainPart($plain_id, $mime_message, $body_data, $base_part)); } - if ($this->_forward) { - foreach ($base_part->contentTypeMap() as $mid => $type) { - if ($this->imapMessage->isAttachment($mid, $type)) { - $mail->addMimePart($this->imapMessage->getMimePart($mid)); - } + + foreach ($mime_message->contentTypeMap() as $mid => $type) { + if ($mid != 0 && $mid != $mime_message->findBody('plain') && $mid != $mime_message->findBody('html')) { + $mail->addMimePart($mime_message->getPart($mid)); } } + + return $mail; + } + + protected function _doSmartForward() + { + // Check for EAS 16.0 Forwardees + if (!empty($this->_forwardees)) { + $list = new Horde_Mail_Rfc822_List(); + foreach ($this->_forwardees as $forwardee) { + $to = new Horde_Mail_Rfc822_Address($forwardee->email); + $to->personal = $forwardee->name; + $list->add($to); + } + $this->_headers->add('To', $list->writeAddress()); + } + + // Forwarded message + $rfc822Stream = $this->imapMessage->getFullMsg(true); + $rfc822Part = new Horde_Mime_Part(); + $rfc822Part->setType("message/rfc822"); + $rfc822Part->setContents($rfc822Stream); + $rfc822Part->setName(Horde_Core_Translation::t("Forwarded Message")); + + // Incoming message to append to forward. + $mime_message = $this->_raw->getMimeObject(); + + // Outgoing message + $mail = new Horde_Mime_Mail($this->_headers->toArray(array('charset' => 'UTF-8'))); + $mail->setBody($this->_getSmartPlainText($mime_message)); + $mail->setHtmlBody($this->_getSmartHtmlText($mime_message)); + foreach ($mime_message->contentTypeMap() as $mid => $type) { if ($mid != 0 && $mid != $mime_message->findBody('plain') && $mid != $mime_message->findBody('html')) { $mail->addMimePart($mime_message->getPart($mid)); } } + $mail->addMimePart($rfc822Part); try { $mail->send($GLOBALS['injector']->getInstance('Horde_Mail')); @@ -442,6 +480,23 @@ protected function _sendSmart() */ protected function _getPlainPart( $plain_id, Horde_Mime_Part $mime_message, array $body_data, Horde_Mime_Part $base_part) + { + $smart_text = _getSmartPlainText($mime_message); + if ($this->_forward) { + return $smart_text . $this->_forwardText($body_data, $base_part->getPart($plain_id)); + } + + return $smart_text . $this->_replyText($body_data, $base_part->getPart($plain_id)); + } + +/** + * Return plain text part from incoming smart request. + * + * @param Horde_Mime_Part $mime_message Mime_Part representing the incoming msg + * + * @return string Plain text suitable for setting as the body of mime msg. + */ + protected function _getSmartPlainText(Horde_Mime_Part $mime_message) { if (!$id = $mime_message->findBody('plain')) { $smart_text = Horde_ActiveSync_Utils::ensureUtf8( @@ -456,11 +511,7 @@ protected function _getPlainPart( $mime_message->getCharset()); } - if ($this->_forward) { - return $smart_text . $this->_forwardText($body_data, $base_part->getPart($plain_id)); - } - - return $smart_text . $this->_replyText($body_data, $base_part->getPart($plain_id)); + return $smart_text; } /** @@ -476,7 +527,16 @@ protected function _getPlainPart( * * @return string The plaintext part of the email message that is being sent. */ - protected function _getHtmlPart($html_id, $mime_message, $body_data, $base_part) + protected function _getHtmlPart($html_id, Horde_Mime_Part $mime_message, $body_data, $base_part) + { + $smart_text = $this->_getSmartHtmlText($mime_message); + if ($this->_forward) { + return $smart_text . $this->_forwardText($body_data, $base_part->getPart($html_id), true); + } + return $smart_text . $this->_replyText($body_data, $base_part->getPart($html_id), true); + } + + protected function _getSmartHtmlText(Horde_Mime_Part $mime_message) { if (!$id = $mime_message->findBody('html')) { $smart_text = self::text2html( @@ -489,10 +549,7 @@ protected function _getHtmlPart($html_id, $mime_message, $body_data, $base_part) $mime_message->getCharset()); } - if ($this->_forward) { - return $smart_text . $this->_forwardText($body_data, $base_part->getPart($html_id), true); - } - return $smart_text . $this->_replyText($body_data, $base_part->getPart($html_id), true); + return $smart_text; } /** @@ -678,4 +735,4 @@ public static function html2text($msg) return Horde_Text_Filter::filter($msg, 'Html2text', array('nestingLimit' => 1000)); } -} \ No newline at end of file +}