You are here

function mimemail_parse_headers in Mime Mail 7

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

Split a message (or message part) into its headers and body section.

1 call to mimemail_parse_headers()
mimemail_parse in includes/mimemail.incoming.inc
Parses a message into its parts.

File

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

Code

function mimemail_parse_headers($message) {

  // Split out body and headers.
  if (preg_match("/^(.*?)\r?\n\r?\n(.*)/s", $message, $match)) {
    list($hdr, $body) = array(
      $match[1],
      $match[2],
    );
  }

  // Un-fold the headers.
  $hdr = preg_replace(array(
    "/\r/",
    "/\n(\t| )+/",
  ), array(
    '',
    ' ',
  ), $hdr);
  $headers = array();
  foreach (explode("\n", trim($hdr)) as $row) {
    $split = strpos($row, ':');
    $name = trim(drupal_substr($row, 0, $split));
    $val = trim(drupal_substr($row, $split + 1));
    $headers[$name] = $val;
  }
  $type = preg_replace('/\\s*([^;]+).*/', '\\1', $headers['Content-Type']);
  return array(
    'headers' => $headers,
    'body' => $body,
    'content-type' => $type,
  );
}