diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 024f80ce5d430baab63c024987a7a9a565d1cacc..aac1d8cb5416663dc2f553f7e3763bb891604f94 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -193,6 +193,30 @@ class MailFetcher {
             $text=imap_binary($text);
             break;
             case 3:
+            if (strlen($text) > (1 << 20)) {
+                try {
+                    if (!($temp = tempnam(sys_get_temp_dir(), 'attachments'))
+                        || !($f = fopen($temp, 'w'))
+                        ) {
+                            throw new Exception();
+                    }
+                    $s_filter = stream_filter_append($f, 'convert.base64-decode',STREAM_FILTER_WRITE);
+                    if (!fwrite($f, $text))
+                        throw new Exception();
+                    stream_filter_remove($s_filter); 
+                    fclose($f);
+                    if (!($f = fopen($temp, 'r')) || !($text = fread($f, filesize($temp)))
+                        throw new Exception();
+                    fclose($f);
+                    unlink($temp);
+                    break;
+                }
+                catch (Exception $e) {
+                    // Noop. Fall through to imap_base64 method below
+                    @fclose($f);
+                    @unlink($temp);
+                }
+            }
             // imap_base64 implies strict mode. If it refuses to decode the
             // data, then fallback to base64_decode in non-strict mode
             $text = (($conv=imap_base64($text))) ? $conv : base64_decode($text);