You are here

function mimemail_parse in Mime Mail 7

Same name and namespace in other branches
  1. 5 mimemail.inc \mimemail_parse()
  2. 6 mimemail.inc \mimemail_parse()

Parses a message into its parts.

Parameters

string $message: The message to parse.

Return value

array The parts of the message.

1 call to mimemail_parse()
mimemail_incoming in includes/mimemail.incoming.inc
Parses an externally received message.

File

includes/mimemail.incoming.inc, line 62
Functions that handle inbound messages to mimemail.

Code

function mimemail_parse($message) {

  // Provides a "headers", "content-type" and "body" element.
  $mail = mimemail_parse_headers($message);

  // Get an address-only version of "From" (useful for user_load() and such).
  $mail['from'] = preg_replace('/.*\\b([a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4})\\b.*/i', '\\1', drupal_strtolower($mail['headers']['From']));

  // Get a subject line, which may be cleaned up/modified later.
  $mail['subject'] = $mail['headers']['Subject'];

  // Make an array to hold any non-content attachments.
  $mail['attachments'] = array();

  // We're dealing with a multi-part message.
  $mail['parts'] = mimemail_parse_boundary($mail);
  foreach ($mail['parts'] as $part_body) {
    $part = mimemail_parse_headers($part_body);
    $sub_parts = mimemail_parse_boundary($part);

    // Content is encoded in a multipart/alternative section.
    if (count($sub_parts) > 1) {
      foreach ($sub_parts as $sub_part_body) {
        $sub_part = mimemail_parse_headers($sub_part_body);
        if ($sub_part['content-type'] == 'text/plain') {
          $mail['text'] = mimemail_parse_content($sub_part);
        }
        if ($sub_part['content-type'] == 'text/html') {
          $mail['html'] = mimemail_parse_content($sub_part);
        }
        else {
          $mail['attachments'][] = mimemail_parse_attachment($sub_part);
        }
      }
    }
    if ($part['content-type'] == 'text/plain' && !isset($mail['text'])) {
      $mail['text'] = mimemail_parse_content($part);
    }
    elseif ($part['content-type'] == 'text/html' && !isset($mail['html'])) {
      $mail['html'] = mimemail_parse_content($part);
    }
    else {
      $mail['attachments'][] = mimemail_parse_attachment($part);
    }
  }

  // Make sure our text and html parts are accounted for.
  if (isset($mail['html']) && !isset($mail['text'])) {
    $mail['text'] = preg_replace('|<style.*</style>|mis', '', $mail['html']);
    $mail['text'] = drupal_html_to_text($mail['text']);
  }
  elseif (isset($mail['text']) && !isset($mail['html'])) {
    $mail['html'] = check_markup($mail['text'], variable_get('mimemail_format', filter_fallback_format()));
  }

  // Last ditch attempt - use the body as-is.
  if (!isset($mail['text'])) {
    $mail['text'] = mimemail_parse_content($mail);
    $mail['html'] = check_markup($mail['text'], variable_get('mimemail_format', filter_fallback_format()));
  }
  return $mail;
}