Skip to content

Commit

Permalink
Rewrite SMARTFORWARD handling;
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrubinsk committed Aug 27, 2020
1 parent 1cddf5c commit b54abd8
Showing 1 changed file with 83 additions and 26 deletions.
109 changes: 83 additions & 26 deletions lib/Horde/Core/ActiveSync/Mail.php
Expand Up @@ -369,25 +369,29 @@ 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
// text sent from the client, not the fully generated MIME message.
$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');
Expand All @@ -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'));
Expand All @@ -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(
Expand All @@ -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;
}

/**
Expand All @@ -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(
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -678,4 +735,4 @@ public static function html2text($msg)
return Horde_Text_Filter::filter($msg, 'Html2text', array('nestingLimit' => 1000));
}

}
}

0 comments on commit b54abd8

Please sign in to comment.