You are here

function mailhandler_get_part in Mailhandler 6

Same name and namespace in other branches
  1. 5 mailhandler.module \mailhandler_get_part()
  2. 7 mailhandler.retrieve.inc \mailhandler_get_part()

Returns the first part with the specified mime_type

USAGE EXAMPLES - from php manual: imap_fetch_structure() comments $data = get_part($stream, $msg_number, "TEXT/PLAIN"); // get plain text $data = get_part($stream, $msg_number, "TEXT/HTML"); // get HTML text

1 call to mailhandler_get_part()
mailhandler_retrieve_message in ./mailhandler.retrieve.inc
Retrieve individual messages from an IMAP result

File

./mailhandler.retrieve.inc, line 17
Mailbox connection code.

Code

function mailhandler_get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
  if (!$structure) {
    $structure = imap_fetchstructure($stream, $msg_number, FT_UID);
  }
  if ($structure) {
    $encoding = variable_get('mailhandler_default_encoding', 'UTF-8');
    foreach ($structure->parameters as $parameter) {
      if (strtoupper($parameter->attribute) == 'CHARSET') {
        $encoding = $parameter->value;
      }
    }
    if ($mime_type == mailhandler_get_mime_type($structure)) {
      if (!$part_number) {
        $part_number = '1';
      }
      $text = imap_fetchbody($stream, $msg_number, $part_number, FT_UID);
      if ($structure->encoding == ENCBASE64) {
        return drupal_convert_to_utf8(imap_base64($text), $encoding);
      }
      else {
        if ($structure->encoding == ENCQUOTEDPRINTABLE) {
          return drupal_convert_to_utf8(quoted_printable_decode($text), $encoding);
        }
        else {
          return drupal_convert_to_utf8($text, $encoding);
        }
      }
    }
    if ($structure->type == TYPEMULTIPART) {

      /* multipart */
      $prefix = '';
      while (list($index, $sub_structure) = each($structure->parts)) {
        if ($part_number) {
          $prefix = $part_number . '.';
        }
        $data = mailhandler_get_part($stream, $msg_number, $mime_type, $sub_structure, $prefix . ($index + 1));
        if ($data) {
          return $data;
        }
      }
    }
  }
  return false;
}