From 654443697407a47f5202dd59ebd608e854f76b63 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 28 Jan 2014 14:11:27 -0600
Subject: [PATCH] fetch: Implement parsing attached email

If the content of the message is of type `message/rfc822`, then support
downloading the body of the message and sending it off to the mail parsing
system, simulating a piped email.
---
 include/api.tickets.php     |  5 +++--
 include/class.mailfetch.php | 12 ++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/include/api.tickets.php b/include/api.tickets.php
index 00d7628b2..feca779c5 100644
--- a/include/api.tickets.php
+++ b/include/api.tickets.php
@@ -141,9 +141,10 @@ class TicketApiController extends ApiController {
         return $ticket;
     }
 
-    function processEmail() {
+    function processEmail($data=false) {
 
-        $data = $this->getEmailRequest();
+        if (!$data)
+            $data = $this->getEmailRequest();
 
         if (($thread = ThreadEntry::lookupByEmailHeaders($data))
                 && $thread->postEmail($data)) {
diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 9438e5e5d..21797c231 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -522,6 +522,18 @@ class MailFetcher {
         // then this is a message with the forwarded message as the
         // attachment. Download the body and pass it along to the mail
         // parsing engine.
+        $info = Mail_Parse::splitHeaders($mailinfo['header']);
+        if (strtolower($info['Content-Type']) == 'message/rfc822') {
+            if ($wrapped = $this->getPart($mid, 'message/rfc822')) {
+                require_once INCLUDE_DIR.'api.tickets.php';
+                // Simulate piping the contents into the system
+                $api = new TicketApiController();
+                $parser = new EmailDataParser();
+                if ($data = $parser->parse($wrapped))
+                    return $api->processEmail($data);
+            }
+            // If any of this fails, create the ticket as usual
+        }
 
 	    //Is the email address banned?
         if($mailinfo['email'] && TicketFilter::isBanned($mailinfo['email'])) {
-- 
GitLab