From 07fec4c4d19d27eade440bad0041a952ea4f99f8 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 27 Jan 2014 16:31:57 -0600
Subject: [PATCH] pipe: Unwrap forwarded emails

If the mail received into the system has a content type of 'message/rfc822',
then unwrap the inner message and use it as the parsed email. Effectively,
assume the email was forwarded as an message/rfc822 attachment.
---
 include/class.mailfetch.php |  5 +++++
 include/class.mailparse.php | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index e7b039211..9438e5e5d 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -518,6 +518,11 @@ class MailFetcher {
         if(!($mailinfo = $this->getHeaderInfo($mid)))
             return false;
 
+        // TODO: If the content-type of the message is 'message/rfc822',
+        // then this is a message with the forwarded message as the
+        // attachment. Download the body and pass it along to the mail
+        // parsing engine.
+
 	    //Is the email address banned?
         if($mailinfo['email'] && TicketFilter::isBanned($mailinfo['email'])) {
 	        //We need to let admin know...
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 3356cb9bf..1e9f81a5c 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -56,7 +56,18 @@ class Mail_Parse {
         $this->splitBodyHeader();
         $this->struct=Mail_mimeDecode::decode($params);
 
-        return (PEAR::isError($this->struct) || !(count($this->struct->headers)>1))?FALSE:TRUE;
+        if (PEAR::isError($this->struct))
+            return false;
+
+        // 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)
+                $this->struct = $outer->parts[0];
+        }
+
+        return (count($this->struct->headers) > 1);
     }
 
     function splitBodyHeader() {
-- 
GitLab