You are here

function _htmlmail_emogrify in HTML Mail 6

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

If the Emogrifier <http://www.pelagodesign.com/sidecar/emogrifier/> exists, the CSS styles inside the the $message['body'] are inserted into the other HTML tags within the same $message['body'] as inline style attributes, based on CSS selectors.

This function is based on code in the simplenews_template module.

This emogrifier differs from that of simplenews_template in that it permits modules or users to adjoin CSS into the $message['body'] using the HTML <style> tag. The function searches the entire body for style tags, concatenates them in order of appearance in the file, then sends them to the Emogrifier script.

Note that the method modifies the $message['body'] directly, and the return value is the modified $message['body'] string as well.

Parameters

$message: The message array to be sent. This function works directly on the $message['body'].

Return value

$message['body'] The modified message body string with inlined CSS applied.

1 call to _htmlmail_emogrify()
htmlmail_mail_alter in ./htmlmail.module
Implementation of hook_mail_alter().

File

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

Code

function _htmlmail_emogrify(&$message) {
  $path = drupal_get_path('module', 'htmlmail') . '/emogrifier/emogrifier.php';
  if (is_file($path)) {
    $style = array();

    // Pull out the contents of any style tags
    if (preg_match_all("@<style[^>]*>(.*)</style>@Usi", $message['body'], $matches, PREG_PATTERN_ORDER)) {
      $style = $matches[1];
    }

    // Emogrify can't handle several CSS rules on one line. As a precaution,
    // we therefore insert LF after each closing bracket.
    $style = preg_replace('/}\\s*/', "}\n", implode("\n", $style));

    // Inline the CSS rules.
    include_once $path;

    // get and reset error levels so we dont get DOMDocument::loadHTML() errors
    $errorlevel = error_reporting();
    error_reporting(0);
    $emogrifier = new Emogrifier($message['body'], $style);
    $message['body'] = $emogrifier
      ->emogrify();
    error_reporting($errorlevel);
  }
  return $message['body'];
}