From b4f18d5cf02c44a8b2742f75581c716f63e12277 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 3 Feb 2014 12:41:13 -0600
Subject: [PATCH] pipe: For wrapped messages, use wrapped headers

07fec4c4d19d27eade440bad0041a952ea4f99f8 introduced the ability to receive
and process messages forwarded as a message/rfc822. However, the email
headers that were saved on file in the %email_info_table were the headers of
the outer message.

This patch pulls the headers from the wrapped message to be saved in the
database. This mainly addresses the email References and In-Reply-To headers
for the email responses from the system.
---
 include/class.mailparse.php | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index eca76a0b7..e9f83de4d 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -53,18 +53,27 @@ class Mail_Parse {
                         'include_bodies'=> $this->include_bodies,
                         'decode_headers'=> $this->decode_headers,
                         'decode_bodies' => $this->decode_bodies);
-        $this->splitBodyHeader();
+
         $this->struct=Mail_mimeDecode::decode($params);
 
         if (PEAR::isError($this->struct))
             return false;
 
+        $this->splitBodyHeader();
+
         // Handle wrapped emails when forwarded
         if ($this->struct && $this->struct->parts) {
             $outer = $this->struct;
             $ctype = $outer->ctype_primary.'/'.$outer->ctype_secondary;
-            if (strcasecmp($ctype, 'message/rfc822') === 0)
+            if (strcasecmp($ctype, 'message/rfc822') === 0) {
                 $this->struct = $outer->parts[0];
+                // Use headers of the wrapped message
+                $headers = array();
+                foreach ($this->struct->headers as $h=>$v)
+                    $headers[mb_convert_case($h, MB_CASE_TITLE)] = $v;
+                $this->header = Format::array_implode(
+                     ": ", "\n", $headers);
+            }
         }
 
         return (count($this->struct->headers) > 1);
-- 
GitLab