You are here

public function MailhandlerPhpImapRetrieve::get_part in Mailhandler 7.2

Same name and namespace in other branches
  1. 6.2 modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php \MailhandlerPhpImapRetrieve::get_part()

Gets a message part and adds it to $parts.

Parameters

object $stream: The IMAP stream from which to fetch data.

int $msg_number: The message number.

object $structure: The structure definition.

int $part_number: The part number to add.

array $parts: The array of added parts.

1 call to MailhandlerPhpImapRetrieve::get_part()
MailhandlerPhpImapRetrieve::get_parts in modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php
Returns an array of parts as file objects.

File

modules/mailhandler_php_imap/plugins/mailhandler/retrieve/MailhandlerPhpImapRetrieve.class.php, line 228
Definition of MailhandlerPhpImapRetrieve class.

Class

MailhandlerPhpImapRetrieve
Retrieve messages using PHP IMAP library.

Code

public function get_part($stream, $msg_number, $structure, $part_number, &$parts) {

  // Get data.
  $data = '';
  if ($part_number) {
    $data = imap_fetchbody($stream, $msg_number, $part_number, FT_PEEK);
  }
  else {
    $data = imap_body($stream, $msg_number, FT_PEEK);
  }

  // Get params.
  $params = array();
  if ($structure->ifparameters) {
    foreach ($structure->parameters as $parameter) {
      $params[drupal_strtolower($parameter->attribute)] = $parameter->value;
    }
  }
  if ($structure->ifdparameters) {
    foreach ($structure->dparameters as $parameter) {
      $params[drupal_strtolower($parameter->attribute)] = $parameter->value;
    }
  }
  $charset = 'auto';
  if (isset($params['charset'])) {
    $charset = $params['charset'];
  }

  // Decode data.
  switch ($structure->encoding) {
    case ENCQUOTEDPRINTABLE:
      $data = quoted_printable_decode($data);

      // Properly decode most Microsoft encodings (Hotmail and Windows).
      $data = drupal_convert_to_utf8($data, $charset);
      break;
    case ENCBASE64:
      $data = base64_decode($data);
      break;
    case ENC7BIT:
    case ENC8BIT:
      $data = imap_utf8($data);

      // Properly decode most Microsoft encodings (Hotmail and Windows).
      $data = drupal_convert_to_utf8($data, $charset);
      break;
  }

  // Get attachments.
  foreach ($params as $attribute => $value) {
    switch ($attribute) {
      case 'filename':
      case 'name':
        $attachment = new stdClass();
        $attachment->filename = $value;
        $attachment->data = $data;
        if (isset($structure->id)) {
          $attachment->id = $structure->id;
        }
        if (!($attachment->filemime = $this
          ->get_mime_type($structure))) {
          mailhandler_report('warning', 'Could not fetch mime type for message part. Defaulting to application/octet-stream.');
          $attachment->filemime = 'application/octet-stream';
        }
        $parts['attachments'][] = $attachment;
        break 2;
    }
  }

  // Get bodies.
  if (($structure->type == TYPETEXT || $structure->type == TYPEMESSAGE) && $data) {

    // Messages may be split in different parts because of inline attachments,
    // so append parts together with blank row.
    if (drupal_strtolower($structure->subtype) == 'plain') {
      $parts['text_body'] .= trim($data);
    }
    else {
      $parts['html_body'] .= $data;
    }
  }

  // Recurse for multi-part messages.
  if ($structure->type = TYPEMULTIPART && isset($structure->parts)) {
    foreach ($structure->parts as $index => $sub_structure) {
      $this
        ->get_part($stream, $msg_number, $sub_structure, $part_number . '.' . ($index + 1), $parts);
    }
  }
}