You are here

function mailhandler_get_part in Mailhandler 5

Same name and namespace in other branches
  1. 6 mailhandler.retrieve.inc \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 in ./mailhandler.module
Retrieve all msgs from a given mailbox and process them.

File

./mailhandler.module, line 446

Code

function mailhandler_get_part($stream, $msg_number, $mime_type, $structure = false, $part_number = false) {
  if (!$structure) {
    $structure = imap_fetchstructure($stream, $msg_number);
  }
  if ($structure) {
    foreach ($structure->parameters as $parameter) {
      if (strtoupper($parameter->attribute) == 'CHARSET') {
        $encoding = $parameter->value;

        //watchdog('mailhandler', 'Encoding is ' . $encoding);
      }
    }
    if ($mime_type == mailhandler_get_mime_type($structure)) {
      if (!$part_number) {
        $part_number = '1';
      }
      $text = imap_fetchbody($stream, $msg_number, $part_number);
      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 */
      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;
}