### Eclipse Workspace Patch 1.0
#P mailhandler
Index: mailhandler.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/mailhandler/mailhandler.module,v
retrieving revision 1.93
diff -u -r1.93 mailhandler.module
--- mailhandler.module	30 Jun 2007 01:04:35 -0000	1.93
+++ mailhandler.module	21 Oct 2007 22:32:55 -0000
@@ -10,9 +10,8 @@
 define('MAILHANDLER_MIME_IMAGE', 5);
 define('MAILHANDLER_MIME_VIDEO', 6);
 define('MAILHANDLER_MIME_OTHER', 7);
-
-
-
+define('MAILHANDLER_MIME_BASE64', 3);
+define('MAILHANDLER_MIME_QUOTED_PRINTABLE', 4);
 
 /**
  * Retrieve all msgs from a given mailbox and process them.
@@ -46,13 +45,23 @@
       }
 
       $mime = explode(',', variable_get('mime', 'TEXT/HTML,TEXT/PLAIN'));
+
+      // Get the first text part - this will be the node body
       $origbody = mailhandler_get_part($result, $i, $mime[0]);
+
+      // If we didn't get a body from our first attempt, try the alternate format (HTML or PLAIN)
       if (!$origbody) {
         $origbody = mailhandler_get_part($result, $i, $mime[1]);
-        if (!$origbody) {
-          // @TODO: Log that we got an empty email?
-          continue;
-        }
+      }
+
+      // Parse MIME parts, so all mailhandler modules have access to
+      // the full array of mime parts without having to process the email.
+      $mimeparts = mailhandler_get_parts($result, $i);
+
+      // Is this an empty message with no body and no mimeparts?
+      if (!$origbody && !$mimeparts) {
+        // @TODO: Log that we got an empty email?
+        continue;
       }
 
       $num_processed++;
@@ -63,9 +72,8 @@
       // check if mail originates from an authenticated user
       $node = mailhandler_authenticate($node, $header, $origbody, $mailbox);
 
-      // Parse MIME parts, so all mailhandler modules have access to 
-      // the full array of mime part without having to process the email.
-      $node->mimeparts = mailhandler_get_parts($result, $i);
+      // Put $mimeparts on the node
+      $node->mimeparts = $mimeparts;
 
       // we need to change the current user
       // this has to be done here to allow modules
@@ -404,17 +412,17 @@
         $part_number = '1';
       }
       $text = imap_fetchbody($stream, $msg_number, $part_number);
-      if ($structure->encoding == 3) {
+      if ($structure->encoding == MAILHANDLER_MIME_BASE64) {
         return drupal_convert_to_utf8(imap_base64($text), $encoding);
       }
-      else if ($structure->encoding == 4) {
+      else if ($structure->encoding == MAILHANDLER_MIME_QUOTED_PRINTABLE) {
         return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding);
       }
       else {
         return drupal_convert_to_utf8($text, $encoding);
       }
     }
-    if ($structure->type == 1) { /* multipart */
+    if ($structure->type == MAILHANDLER_MIME_MULTIPART) { /* multipart */
       while (list($index, $sub_structure) = each ($structure->parts)) {
         if ($part_number) {
           $prefix = $part_number .'.';
@@ -433,13 +441,13 @@
 
 /**
  * Returns an array of parts as file objects
- * 
+ *
  * @param
- * @param $structure 
- *   A message structure, usually used to recurse into specific parts 
+ * @param $structure
+ *   A message structure, usually used to recurse into specific parts
  * @param $max_depth
  *   Maximum Depth to recurse into parts.
- * @param $depth 
+ * @param $depth
  *   The current recursion depth.
  * @param $part_number
  *   A message part number to track position in a message during recursion.
@@ -459,7 +467,7 @@
   if ($structure->type == MAILHANDLER_MIME_MULTIPART) {
     // Restrict recursion depth.
     if ($depth >= $max_depth) {
-      watchdog('mailhandler', t('Maximum recursion depths met in mailhander_get_structure_part for 
+      watchdog('mailhandler', t('Maximum recursion depths met in mailhander_get_structure_part for
                   message number %msg_number.',  array('%msg_number' => $msg_number)), WATCHDOG_NOTICE);
       return $parts;
     }
@@ -518,7 +526,7 @@
   }
 
   // Retrieve part  convert MIME encoding to UTF-8
-  if(!$part->data = imap_fetchbody($stream, $msg_number, $part_number, FT_UID)) {
+  if(!$part->data = imap_fetchbody($stream, $msg_number, $part_number)) {
     drupal_set_message("imap_fetchbody($stream, $msg_number, $part_number)");
     watchdog('mailhandler', 'No Data!!', WATCHDOG_ERROR);
     return $parts;
@@ -528,6 +536,15 @@
   if ($structure->type == MAILHANDLER_MIME_TEXT) {
     $part->data = imap_utf8($part->data);
   }
+  else {
+    // If not text then decode as necessary
+    if ($structure->encoding == MAILHANDLER_MIME_BASE64) {
+      $part->data = imap_base64($part->data);
+    }
+    else if ($structure->encoding == MAILHANDLER_MIME_QUOTED_PRINTABLE) {
+      $part->data = quoted_printable_decode($part->data);
+    }
+  }
 
   //always return an array to satisfy array_merge in recursion catch, and array return value.
   $parts[] = $part;
