From 68df49b3abfd42def919d4a04c321fa6726a055a Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 18 Feb 2014 16:29:03 -0600
Subject: [PATCH] tnef: Fix attachment handling when fetching

---
 include/class.mailfetch.php | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 72dcc6eee..eaa1ebcdd 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -558,14 +558,18 @@ class MailFetcher {
                 && ($attachments = $this->getAttachments($struct))) {
             foreach ($attachments as $i=>$info) {
                 if (0 === strcasecmp('application/ms-tnef', $info['type'])) {
-                    $data = $this->decode(imap_fetchbody($self->mbox,
-                        $mid, $info['index']), $info['encoding']);
-                    $tnef = new TnefStreamParser($data);
-                    $this->tnef = $tnef->getMessage();
-                    // No longer considered an attachment
-                    unset($attachments[$i]);
-                    // There should only be one of these
-                    break;
+                    try {
+                        $data = $this->decode(imap_fetchbody($this->mbox,
+                            $mid, $info['index']), $info['encoding']);
+                        $tnef = new TnefStreamParser($data);
+                        $this->tnef = $tnef->getMessage();
+                        // No longer considered an attachment
+                        unset($attachments[$i]);
+                        // There should only be one of these
+                        break;
+                    } catch (TnefException $ex) {
+                        // Noop -- winmail.dat remains an attachment
+                    }
                 }
             }
         }
@@ -627,7 +631,7 @@ class MailFetcher {
                 if(!$ost->isFileTypeAllowed($file)) {
                     $file['error'] = 'Invalid file type (ext) for '.Format::htmlchars($file['name']);
                 }
-                else {
+                elseif (!$file['data']) {
                     // only fetch the body if necessary
                     $self = $this;
                     $file['data'] = function() use ($self, $mid, $a) {
-- 
GitLab