diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 0453bac8357c20510dda8c15d96bb8df02ee6311..14cdb9e1db0054120d6de9d1334376939ad04b5e 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -642,7 +642,7 @@ class MailFetcher {
                 foreach ($this->tnef->attachments as $at) {
                     $attachments[] = array(
                         'cid' => @$at->AttachContentId ?: false,
-                        'data' => $at->Data,
+                        'data' => $at,
                         'type' => @$at->AttachMimeTag ?: false,
                         'name' => $at->getName(),
                     );
@@ -656,7 +656,10 @@ class MailFetcher {
                 if(!$ost->isFileTypeAllowed($file)) {
                     $file['error'] = 'Invalid file type (ext) for '.Format::htmlchars($file['name']);
                 }
-                elseif (!$file['data']) {
+                elseif (@$a['data'] instanceof TnefAttachment) {
+                    $file['data'] = $a['data']->getData();
+                }
+                else {
                     // only fetch the body if necessary
                     $self = $this;
                     $file['data'] = function() use ($self, $mid, $a) {
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 6883cdaf6a7560437c4ee0fcc5a00886bf111004..81a4353ac3480ef0f0f42585b98425f4e1017031 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -377,7 +377,8 @@ class Mail_Parse {
                     'type'  => strtolower($part->ctype_primary.'/'.$part->ctype_secondary),
                     );
 
-            if ($part->ctype_parameters['charset'])
+            if ($part->ctype_parameters['charset']
+                    && 0 === strcasecmp($part->ctype_primary, 'text'))
                 $file['data'] = $this->mime_encode($part->body,
                     $part->ctype_parameters['charset']);
             else
@@ -397,7 +398,7 @@ class Mail_Parse {
             foreach ($this->tnef->attachments as $at) {
                 $files[] = array(
                     'cid' => @$at->AttachContentId ?: false,
-                    'data' => $at->Data,
+                    'data' => $at->getData(),
                     'type' => @$at->AttachMimeTag ?: false,
                     'name' => $at->getName(),
                 );
diff --git a/include/tnef_decoder.php b/include/tnef_decoder.php
index 18e95d697c5eba8bb218ff81d5e9c903b2cedbd7..91a5e59fe562c036f396a57be78f835065834aec 100644
--- a/include/tnef_decoder.php
+++ b/include/tnef_decoder.php
@@ -209,11 +209,13 @@ class TnefAttribute {
     const AttributeReadOnly = 0x10f6;
     const CreationTime = 0x3007;
     const LastModificationTime = 0x3008;
+    const AttachDataBinary = 0x3701;
     const AttachEncoding = 0x3702;
     const AttachExtension = 0x3703;
     const AttachFilename = 0x3704;
     const AttachLongFilename = 0x3707;
     const AttachPathname = 0x3708;
+    const AttachTransportName = 0x370c;
     const AttachMimeTag = 0x370e;    # Mime content-type
     const AttachContentId = 0x3712;
     const AttachmentCharset = 0x371b;
@@ -234,7 +236,7 @@ class TnefAttribute {
     const ReceivedRepresentingSimpleDisplayName = 0x4034;
     const CreatorSimpleDisplayName = 0x4038;
     const LastModifierSimpleDisplayName = 0x4039;
-    const ContentFilterSpmnConfidenceLevel = 0x4076;
+    const ContentFilterSpamConfidenceLevel = 0x4076;
     const MessageEditorFormat = 0x5909;
 
     static function getName($code) {
@@ -485,7 +487,7 @@ class TnefStreamParser {
                     new TnefAttributeStreamReader($info['data']));
                 break;
             case self::attAttachTransportFilename:
-                $attach->_setFilename($info['data']);
+                $attach->_setFilename(rtrim($info['data'], "\x00"));
                 break;
             case self::attAttachData:
                 $attach->_setData($info['data']);
@@ -555,6 +557,13 @@ class TnefAttachment extends AbstractTnefObject {
         $this->Data = $data;
     }
 
+    function getData() {
+        if (isset($this->Data))
+            return $this->Data;
+        elseif (isset($this->AttachDataBinary))
+            return $this->AttachDataBinary;
+    }
+
     function _setRenderingData($data) {
         // Pass
     }
@@ -568,6 +577,8 @@ class TnefAttachment extends AbstractTnefObject {
             return basename($this->AttachLongFilename);
         elseif (isset($this->AttachFilename))
             return $this->AttachFilename;
+        elseif (isset($this->AttachTransportName))
+            return $this->AttachTransportName;
         else
             return $this->TransportFilename;
     }
diff --git a/setup/test/tests/stubs.php b/setup/test/tests/stubs.php
index a26014a11ad13e4ddad120d22d263c32d092ea91..1a40562c49f4fb02f8ad8a02214f617fe0c350fc 100644
--- a/setup/test/tests/stubs.php
+++ b/setup/test/tests/stubs.php
@@ -28,6 +28,7 @@ class mysqli_stmt {
 
 class ReflectionClass {
     function getMethods() {}
+    function getConstants() {}
 }
 
 class DomNode {
@@ -92,4 +93,9 @@ class ZipArchive {
     function statIndex() {}
     function getFromIndex() {}
 }
+
+class finfo {
+    function file() {}
+    function buffer() {}
+}
 ?>