You are here

function mailhandler_get_parts in Mailhandler 5

Same name and namespace in other branches
  1. 6 mailhandler.retrieve.inc \mailhandler_get_parts()
  2. 7 mailhandler.retrieve.inc \mailhandler_get_parts()

Returns an array of parts as file objects

Parameters

@param $structure: A message structure, usually used to recurse into specific parts

$max_depth: Maximum Depth to recurse into parts.

$depth: The current recursion depth.

$part_number: A message part number to track position in a message during recursion.

Return value

An array of file objects.

1 call to mailhandler_get_parts()
mailhandler_retrieve in ./mailhandler.module
Retrieve all msgs from a given mailbox and process them.

File

./mailhandler.module, line 505

Code

function mailhandler_get_parts($stream, $msg_number, $max_depth = 10, $depth = 0, $structure = FALSE, $part_number = FALSE) {
  $parts = array();

  // Load Structure.
  if (!$structure && !($structure = imap_fetchstructure($stream, $msg_number))) {
    watchdog('mailhandler', t('Could not fetch structure for message number %msg_number', array(
      '%msg_number' => $msg_number,
    )), WATCHDOG_NOTICE);
    return $parts;
  }

  // Recurse into multipart messages.
  if ($structure->type == TYPEMULTIPART) {

    // Restrict recursion depth.
    if ($depth >= $max_depth) {
      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;
    }
    foreach ($structure->parts as $index => $sub_structure) {

      // If a part number was passed in and we are a multitype message, prefix the
      // the part number for the recursive call to match the imap4 dot seperated part indexing.
      if ($part_number) {
        $prefix = $part_number . '.';
      }
      $sub_parts = mailhandler_get_parts($stream, $msg_number, $max_depth, $depth + 1, $sub_structure, $prefix . ($index + 1));
      $parts = array_merge($parts, $sub_parts);
    }
    return $parts;
  }

  // Per Part Parsing.
  // Initalize file object like part structure.
  $part = new StdClass();
  $part->attributes = array();
  $part->filename = 'unnamed_attachment';
  if (!($part->filemime = mailhandler_get_mime_type($structure))) {
    watchdog('mailhandler', t('Could not fetch mime type for message part. Defaulting to application/octet-stream.'), WATCHDOG_NOTICE);
    $part->filemime = 'application/octet-stream';
  }
  if ($structure->ifparameters) {
    foreach ($structure->parameters as $parameter) {
      switch (strtoupper($parameter->attribute)) {
        case 'NAME':
        case 'FILENAME':
          $part->filename = $parameter->value;
          break;
        default:

          // put every thing else in the attributes array;
          $part->attributes[$parameter->attribute] = $parameter->value;
      }
    }
  }

  // Handle Content-Disposition parameters for non-text types.
  if ($structure->type != TYPETEXT && $structure->ifdparameters) {
    foreach ($structure->dparameters as $parameter) {
      switch (strtoupper($parameter->attribute)) {
        case 'NAME':
        case 'FILENAME':
          $part->filename = $parameter->value;
          break;

        // put every thing else in the attributes array;
        default:
          $part->attributes[$parameter->attribute] = $parameter->value;
      }
    }
  }

  // Retrieve part  convert MIME encoding to UTF-8
  if (!($part->data = imap_fetchbody($stream, $msg_number, $part_number))) {
    watchdog('mailhandler', 'No Data!!', WATCHDOG_ERROR);
    return $parts;
  }

  // convert text attachment to UTF-8.
  if ($structure->type == TYPETEXT) {
    $part->data = imap_utf8($part->data);
  }
  else {

    // If not text then decode as necessary
    if ($structure->encoding == ENCBASE64) {
      $part->data = imap_base64($part->data);
    }
    else {
      if ($structure->encoding == ENCQUOTEDPRINTABLE) {
        $part->data = quoted_printable_decode($part->data);
      }
    }
  }

  //always return an array to satisfy array_merge in recursion catch, and array return value.
  $parts[] = $part;
  return $parts;
}