From cca2e4dde6f87665196483f9e272d2b21607846e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 12 Aug 2013 15:04:57 +0000
Subject: [PATCH] Ensure that the message has a message-id

And ensure that the email address is trimmed to help matching against email
registered for previous tickets.
---
 include/class.mailfetch.php | 22 +++++++++++++++-------
 include/class.mailparse.php |  9 +++++++++
 2 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 3dc79e8d5..183874799 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -229,9 +229,10 @@ class MailFetcher {
         $sender=$headerinfo->from[0];
         //Just what we need...
         $header=array('name'  =>@$sender->personal,
-                      'email' =>(strtolower($sender->mailbox).'@'.$sender->host),
+                      'email'  => trim(strtolower($sender->mailbox).'@'.$sender->host),
                       'subject'=>@$headerinfo->subject,
-                      'mid'    =>$headerinfo->message_id
+                      'mid'    => trim(@$headerinfo->message_id),
+                      'header' => $this->getHeader($mid),
                       );
 
         //Try to determine target email - useful when fetched inbox has
@@ -251,6 +252,13 @@ class MailFetcher {
 
         $header['emailId'] = $emailId;
 
+        // Ensure we have a message-id. If unable to read it out of the
+        // email, use the hash of the entire email headers
+        if (!$header['mid'] && $header['header'])
+            if (!($header['mid'] = Mail_Parse::findHeaderEntry($header['header'],
+                    'message-id')))
+                $header['mid'] = '<' . md5($header['header']) . '@local>';
+
         return $header;
     }
 
@@ -380,10 +388,6 @@ class MailFetcher {
         if(!($mailinfo = $this->getHeaderInfo($mid)))
             return false;
 
-        //Make sure the email is NOT already fetched... (undeleted emails)
-        if($mailinfo['mid'] && ($id=Ticket::getIdByMessageId(trim($mailinfo['mid']), $mailinfo['email'])))
-            return true; //Reporting success so the email can be moved or deleted.
-
 	    //Is the email address banned?
         if($mailinfo['email'] && TicketFilter::isBanned($mailinfo['email'])) {
 	        //We need to let admin know...
@@ -391,12 +395,16 @@ class MailFetcher {
 	        return true; //Report success (moved or delete)
         }
 
+        //Make sure the email is NOT already fetched... (undeleted emails)
+        if($mailinfo['mid'] && ($id=Ticket::getIdByMessageId($mailinfo['mid'], $mailinfo['email'])))
+            return true; //Reporting success so the email can be moved or deleted.
+
         $vars = array();
         $vars['email']=$mailinfo['email'];
         $vars['name']=$this->mime_decode($mailinfo['name']);
         $vars['subject']=$mailinfo['subject']?$this->mime_decode($mailinfo['subject']):'[No Subject]';
         $vars['message']=Format::stripEmptyLines($this->getBody($mid));
-        $vars['header']=$this->getHeader($mid);
+        $vars['header']=$mailinfo['header'];
         $vars['emailId']=$mailinfo['emailId']?$mailinfo['emailId']:$this->getEmailId();
         $vars['mid']=$mailinfo['mid'];
 
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 822d3f5ed..9082d6d5e 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -106,6 +106,15 @@ class Mail_Parse {
         return $array;
     }
 
+    /* static */
+    function findHeaderEntry($headers, $name) {
+        if (!is_array($headers))
+            $headers = self::splitHeaders($headers);
+        foreach ($headers as $key=>$val)
+            if (strcasecmp($key, $name) === 0)
+                return $val;
+        return false;
+    }
 
     function getStruct(){
         return $this->struct;
-- 
GitLab