You are here

function mimemail_rfc_headers in Mime Mail 7

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

Attempts to RFC822-compliant headers for the mail message or its MIME parts.

@todo Could use some enhancement and stress testing.

Parameters

array $headers: An array of headers.

Return value

string A string containing the headers.

2 calls to mimemail_rfc_headers()
mimemail_mailengine in ./mimemail.module
Implements hook_mailengine().
mimemail_multipart_body in ./mimemail.inc
Build a multipart body.

File

./mimemail.inc, line 21
Common mail functions for sending e-mail. Originally written by Gerhard.

Code

function mimemail_rfc_headers($headers) {
  $header = '';
  $crlf = variable_get('mimemail_crlf', MAIL_LINE_ENDINGS);
  foreach ($headers as $key => $value) {
    $key = trim($key);

    // Collapse spaces and get rid of newline characters.
    $value = preg_replace('/(\\s+|\\n|\\r|^\\s|\\s$)/', ' ', $value);

    // Fold headers if they're too long.
    // A CRLF may be inserted before any WSP.
    // @see http://tools.ietf.org/html/rfc2822#section-2.2.3
    if (drupal_strlen($value) > 60) {

      // If there's a semicolon, use that to separate.
      if (count($array = preg_split('/;\\s*/', $value)) > 1) {
        $value = trim(implode(";{$crlf} ", $array));
      }
      else {
        $value = wordwrap($value, 50, "{$crlf} ", FALSE);
      }
    }
    $header .= $key . ": " . $value . $crlf;
  }
  return trim($header);
}