You are here

function _support_get_message_body_part in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \_support_get_message_body_part()
1 call to _support_get_message_body_part()
support_client_fetch in ./support.module
Fetch mail for a specific client.

File

./support.module, line 1580
support.module

Code

function _support_get_message_body_part($stream, $message, $mime_type, $structure = FALSE, $part = FALSE) {
  if (!$structure) {
    $structure = imap_fetchstructure($stream, $message);
  }
  if (!empty($structure)) {
    foreach ($structure->parameters as $parameter) {
      if (strtoupper($parameter->attribute) == 'CHARSET') {
        $encoding = mb_decode_mimeheader($parameter->value);
        break;
      }
    }
    if ($structure->type == TYPEMULTIPART) {
      $prefix = '';
      while (list($index, $sub_structure) = each($structure->parts)) {
        if ($part) {
          $prefix = $part . '.';
        }
        $mime_type = _support_get_filemime($sub_structure);
        $data[] = _support_get_message_body_part($stream, $message, $mime_type, $sub_structure, $prefix . ($index + 1));
      }
      if (!empty($data)) {

        // Grab the last element, which is HTML if existing.
        return array_shift($data);
      }
      if (!empty($data)) {
        return $data;
      }
    }
    else {
      if ($mime_type == _support_get_filemime($structure)) {
        if (!$part) {
          $part = 1;
        }
        $body = imap_fetchbody($stream, $message, $part);
        switch ($structure->encoding) {
          case ENCBASE64:
            return drupal_convert_to_utf8(imap_base64($body), $encoding);
            break;
          case ENCQUOTEDPRINTABLE:
            return drupal_convert_to_utf8(quoted_printable_decode($body), $encoding);
            break;
          default:
            return drupal_convert_to_utf8($body, $encoding);
            break;
        }
      }
    }
  }
}