*** support.module.orig	2011-01-16 00:16:22.000000000 +0100
--- support.module	2011-01-16 15:45:57.000000000 +0100
***************
*** 1176,1181 ****
--- 1176,1184 ----
  }
  
  function _support_get_attachments($stream, $message, $structure, $parts) {
+   $a=create_part_array($structure);
+   return _support_get_attachments_part($stream, $message, $a);
+ /*
    $attachments = array();
    for ($part = 2; $part <= $parts; $part++) {
      $attachment = imap_fetchbody($stream, $message, $part);
***************
*** 1196,1203 ****
--- 1199,1304 ----
      $attachments[] = $details;
    }
    return $attachments;
+ */
  }
  
+ function _support_get_attachments_part($stream, $message, $structure) {
+   $attachments = array();
+   $new_struct_attachs=array();
+   for ($i = 0; $i < count($structure); $i++) {
+ 	// this structure should be used as return data structure 
+ 	$attachments[$i] = array(
+ 			'is_attachment' => false,
+ 			'filename' => '',
+ 			'name' => '',
+ 			'mime' => '',
+ 			'attachment' => ''
+ 			
+ 		);
+ 		
+ 	$o=$structure[$i]['part_object'];
+ 	if($o->ifdparameters){
+ 			foreach($o->dparameters as $object) {
+ 				if(strtolower($object->attribute) == 'filename') {
+ 					$attachments[$i]['is_attachment'] = true;
+ 					$attachments[$i]['filename'] = $object->value;
+ 				}
+ 			}
+ 	}
+ 	if($o->ifparameters) {
+ 		foreach($o->parameters as $object) {
+ 				if(strtolower($object->attribute) == 'name') {
+ 					$attachments[$i]['is_attachment'] = true;
+ 					$attachments[$i]['name'] = $object->value;
+ 				}
+ 			}
+ 	}
+ 	if($attachments[$i]['is_attachment']) {
+ 			
+ 		$attachments[$i]['mime'] = _support_get_filemime($o);
+ 		$o->filemime=$attachments[$i]['mime'];
+ 
+ 		$attachments[$i]['attachment'] = imap_fetchbody($stream, $message,$structure[$i]['part_number'] );
+ 
+ 		if($o->encoding == ENCBASE64 ) { // 3 = BASE64
+ 			$attachments[$i]['attachment'] = imap_base64($attachments[$i]['attachment']);
+ 		}
+ 		elseif($o->encoding == ENCQUOTEDPRINTABLE) { // 4 = QUOTED-PRINTABLE
+ 			$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
+ 		}
+ 		elseif($o->encoding == TYPETEXT) { 
+ 			$attachments[$i]['attachment'] = imap_utf8($attachments[$i]['attachment']);
+ 		}
+ 
+ 		$o->attachment=$attachments[$i]['attachment'];
+ 		$new_struct_attachs[]=$o;
+ 
+ 	}
+ 	
+   }
+ 
+   return $new_struct_attachs;
+ }
+ 
+ 
+ function create_part_array($structure, $prefix="") {
+     //print_r($structure);
+     if (sizeof($structure->parts) > 0) {    // There some sub parts
+         foreach ($structure->parts as $count => $part) {
+             add_part_to_array($part, $prefix.($count+1), $part_array);
+         }
+     }else{    // Email does not have a seperate mime attachment for text
+         $part_array[] = array('part_number' => $prefix.'1', 'part_object' => $obj);
+     }
+    return $part_array;
+ }
+ // Sub function for create_part_array(). Only called by create_part_array() and itself.
+ function add_part_to_array($obj, $partno, & $part_array) {
+     $part_array[] = array('part_number' => $partno, 'part_object' => $obj);
+     if ($obj->type == 2) { // Check to see if the part is an attached email message, as in the RFC-822 type
+         //print_r($obj);
+         if (sizeof($obj->parts) > 0) {    // Check to see if the email has parts
+             foreach ($obj->parts as $count => $part) {
+                 // Iterate here again to compensate for the broken way that imap_fetchbody() handles attachments
+                 if (sizeof($part->parts) > 0) {
+                     foreach ($part->parts as $count2 => $part2) {
+                         add_part_to_array($part2, $partno.".".($count2+1), $part_array);
+                     }
+                 }else{    // Attached email does not have a seperate mime attachment for text
+                     $part_array[] = array('part_number' => $partno.'.'.($count+1), 'part_object' => $obj);
+                 }
+             }
+         }else{    // Not sure if this is possible
+             $part_array[] = array('part_number' => $prefix.'.1', 'part_object' => $obj);
+         }
+     }else{    // If there are more sub-parts, expand them out.
+         if (sizeof($obj->parts) > 0) {
+             foreach ($obj->parts as $count => $p) {
+                 add_part_to_array($p, $partno.".".($count+1), $part_array);
+             }
+         }
+     }
+ }
  function _support_get_message_body_part($stream, $message, $mime_type, $structure = FALSE, $part = FALSE) {
    if (!$structure) {
      $structure = imap_fetchstructure($stream, $message);
