You are here

function htmlmail_mail_alter in HTML Mail 6

Same name and namespace in other branches
  1. 5 htmlmail.module \htmlmail_mail_alter()

Implementation of hook_mail_alter().

File

./htmlmail.module, line 60
Send system emails in HTML

Code

function htmlmail_mail_alter(&$message) {

  // Match plain exclusions settings
  if ($plain_settings = variable_get('htmlmail_plain_settings', '')) {
    $items = preg_split('/(\\r\\n?|\\n)/', $plain_settings);
    $plain_match = FALSE;
    foreach ($items as $item) {
      if (!strncmp($message['id'], $item, strlen($item))) {
        $plain_match = TRUE;
        break;
      }
    }

    // reverse $plain_match if type is include
    if (variable_get('htmlmail_plain_type', 0)) {
      $plain_match = !$plain_match;
    }
  }
  if (!$plain_match) {

    // All good, make mail html
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed;';

    // message body may be an array
    $message['body'] = is_array($message['body']) ? implode("\n\n", $message['body']) : $message['body'];

    // The paragraph an break stuff
    if (variable_get('htmlmail_autop', '1')) {
      $message['body'] = _filter_autop($message['body']);
    }
    if (variable_get('htmlmail_urlfilter', '1')) {

      // defaults to 72 as there is no filter 0 -- make filters a configuration option?
      $message['body'] = _filter_url($message['body'], 0);
    }

    // Theme from template htmlmail.tpl.php
    $message['body'] = theme('htmlmail', $message['body'], $message['id']);
    if (variable_get('htmlmail_emogrifier', '0')) {
      $message['body'] = _htmlmail_emogrify($message);
    }

    // Convert relative urls to absolute if rel_to_abs is enabled.
    if (module_exists('rel_to_abs') && variable_get('htmlmail_rel_to_abs', 1)) {
      $message['body'] = rel_to_abs_filter('prepare', 0, -1, $message['body']);
    }

    // Send the message key in email for theme template suggestions
    if (variable_get('htmlmail_debug', '0')) {
      $message['body'] .= '<p> Message Key ID: ' . $message['id'] . '</p>';
    }
  }
}