]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Better handle multipart emails (especially those with multipart/alternative contents)
authorCraig Andrews <candrews@integralblue.com>
Mon, 20 Jul 2009 18:02:42 +0000 (14:02 -0400)
committerCraig Andrews <candrews@integralblue.com>
Mon, 20 Jul 2009 18:02:42 +0000 (14:02 -0400)
scripts/maildaemon.php

index 11ddf06b7502734b3c3b239433e764fc31aca78a..3f5402becb37113067e7767b6b2b65794549eb6a 100755 (executable)
@@ -299,25 +299,37 @@ class MailerDaemon
 
         $attachments = array();
 
+        $this->extract_part($parsed,$msg,$attachments);
+
+        return array($from, $to, $msg, $attachments);
+    }
+
+    function extract_part($parsed,&$msg,&$attachments){
         if ($parsed->ctype_primary == 'multipart') {
-            foreach ($parsed->parts as $part) {
-                if ($part->ctype_primary == 'text' &&
-                    $part->ctype_secondary == 'plain') {
-                    $msg = $part->body;
-                }else{
-                    if ($part->body) {
-                       $attachment = tmpfile();
-                       fwrite($attachment, $part->body);
-                        $attachments[] = $attachment;
-                    }
+            if($parsed->ctype_secondary == 'alternative'){
+                $altmsg = $this->extract_msg_from_multipart_alternative_part($parsed);
+                if(!empty($altmsg)) $msg = $altmsg;
+            }else{
+                foreach($parsed->parts as $part){
+                    $this->extract_part($part,$msg,$attachments);
                 }
             }
-        } else if ($type == 'text/plain') {
+        } else if ($parsed->ctype_primary == 'text'
+            && $parsed->ctype_secondary=='plain') {
             $msg = $parsed->body;
-        } else {
-            $this->unsupported_type($type);
+        }else if(!empty($parsed->body)){
+            $attachment = tmpfile();
+            fwrite($attachment, $parsed->body);
+            $attachments[] = $attachment;
         }
-        return array($from, $to, $msg, $attachments);
+    }
+
+    function extract_msg_from_multipart_alternative_part($parsed){
+        foreach ($parsed->parts as $part) {
+            $this->extract_part($part,$msg,$attachments);
+        }
+        //we don't want any attachments that are a result of this parsing
+        return $msg;
     }
 
     function unsupported_type($type)