You are here

function mimemail_headers in Mime Mail 7

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

Gives useful defaults for standard email headers.

Parameters

array $headers: Message headers.

string $from: The address of the sender.

Return value

array Overwrited headers.

2 calls to mimemail_headers()
MimeMailUnitTestCase::testHeaders in tests/mimemail.test
mimemail_prepare_message in ./mimemail.module
Prepares the message for sending.

File

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

Code

function mimemail_headers($headers, $from = NULL) {
  $default_from = variable_get('site_mail', ini_get('sendmail_from'));

  // Overwrite standard headers.
  if ($from) {
    if (!isset($headers['From']) || $headers['From'] == $default_from) {
      $headers['From'] = $from;
    }
    if (!isset($headers['Sender']) || $headers['Sender'] == $default_from) {
      $headers['Sender'] = $from;
    }

    // This may not work. The MTA may rewrite the Return-Path.
    if (!isset($headers['Return-Path']) || $headers['Return-Path'] == $default_from) {

      // According to IANA the current longest TLD is 23 characters.
      if (preg_match('/[a-z\\d\\-\\.\\+_]+@(?:[a-z\\d\\-]+\\.)+[a-z\\d]{2,23}/i', $from, $matches)) {
        $headers['Return-Path'] = "<{$matches[0]}>";
      }
    }
  }

  // Convert From header if it is an array.
  if (is_array($headers['From'])) {
    $headers['From'] = mimemail_address($headers['From']);
  }

  // Run all headers through mime_header_encode() to convert non-ascii
  // characters to an rfc compliant string, similar to drupal_mail().
  foreach ($headers as $key => $value) {

    // According to RFC 2047 addresses MUST NOT be encoded.
    if ($key !== 'From' && $key !== 'Sender') {
      $headers[$key] = mime_header_encode($value);
    }
  }
  return $headers;
}