You are here

htmlmail.module in HTML Mail 7

Send system emails in HTML.

File

htmlmail.module
View source
<?php

/**
 * @file
 * Send system emails in HTML.
 */

/**
 * Implements hook_help().
 */
function htmlmail_help($path, $arg) {
  switch ($path) {
    case 'admin/help#htmlmail':
    case 'admin/settings/htmlmail':
      $output = '<p>' . t("HTML Mail provides formatting and semantic markup capabilities in email that are not available with plain text.") . '</p>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function htmlmail_menu() {
  $items['admin/config/system/htmlmail'] = array(
    'title' => 'HTML Mail',
    'description' => 'Configure HTML Mail system-wide settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htmlmail_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'htmlmail.admin.inc',
  );
  $items['admin/config/system/htmlmail/settings'] = array(
    'title' => 'Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => '-2',
  );
  $items['admin/config/system/htmlmail/template'] = array(
    'title' => 'Template',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htmlmail_template_settings',
    ),
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => '-1',
    'file' => 'htmlmail.admin.inc',
  );
  $items['admin/config/system/htmlmail/test'] = array(
    'title' => 'Send Test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'htmlmail_test_form',
    ),
    'access arguments' => array(
      'access administration pages',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'htmlmail.admin.inc',
  );

  // Hack for formatting emails with a Drupal theme: Show them on a regular
  // page and then fetch that page via drupal_http_request().
  $items['admin/config/system/htmlmail/email'] = array(
    'title callback' => 'htmlmail_get_post_subject',
    'page callback' => 'htmlmail_show_email',
    'theme callback' => 'htmlmail_get_selected_theme',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'htmlmail.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function htmlmail_theme() {
  return array(
    'htmlmail' => array(
      'render element' => 'elements',
      'template' => 'htmlmail',
    ),
  );
}

/**
 * Process variables to format email messages.
 *
 * @see htmlmail.tpl.php
 */
function template_preprocess_htmlmail(&$variables) {
  $variables['path'] = url($variables['directory'], array(
    'absolute' => TRUE,
  ));
  $header = variable_get('htmlmail_header', array(
    'value' => '',
    'format' => NULL,
  ));
  $variables['header'] = check_markup($header['value'], $header['format']);
  $footer = variable_get('htmlmail_footer', array(
    'value' => '',
    'format' => NULL,
  ));
  $variables['footer'] = check_markup($footer['value'], $footer['format']);
  $variables['css'] = variable_get('htmlmail_css', '');
  $variables['template_files'][] = 'htmlmail-' . $variables['module'];
  $variables['debug'] = variable_get('htmlmail_debug', '0');
}

/**
 * Implements hook_mail().
 */
function htmlmail_mail($key, &$message, $params) {
  switch ($key) {
    case 'test':
      $message['subject'] = $params['subject'];
      $message['body'] = explode("\n\n", $params['body']);
      break;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function htmlmail_form_user_profile_form_alter(&$form, &$form_state) {
  if ($form['#user_category'] == 'account') {
    $account = $form['#user'];
    $form['htmlmail'] = array(
      '#type' => 'fieldset',
      '#title' => t('HTML Mail'),
      '#collapsible' => TRUE,
    );
    $form['htmlmail']['htmlmail_plaintext'] = array(
      '#type' => 'checkbox',
      '#title' => t('Plaintext-only emails'),
      '#default_value' => empty($account->data['htmlmail_plaintext']) ? FALSE : 1,
      '#description' => t('The %m module can send emails with fonts, styles, and other HTML formatting.  If you prefer to receive all your emails in plain text, select this option.', array(
        '%m' => 'HTML Mail',
      )),
    );
  }
}

/**
 * Implements hook_user_presave().
 */
function htmlmail_user_presave(&$edit, $account, $category) {
  $edit['data']['htmlmail_plaintext'] = empty($edit['htmlmail_plaintext']) ? 0 : 1;
}

/**
 * Returns an associative array of allowed themes.  The keys are the
 * machine-readable names and the values are the .info file names.
 * Code shamelessly stolen from the og_theme module.
 */
function htmlmail_get_allowed_themes() {
  $allowed =& drupal_static(__FUNCTION__);
  if (!isset($allowed)) {
    $allowed = array(
      '' => t('No theme'),
    );
    module_load_include('inc', 'system', 'system.admin');
    $themes = list_themes();
    uasort($themes, 'system_sort_modules_by_info_name');
    foreach ($themes as $key => $value) {
      if ($value->status) {
        $allowed[$key] = check_plain($value->info['name']);
      }
    }
  }
  return $allowed;
}

/**
 * Returns the selected theme to use for outgoing emails.
 * for use in a theme callback function.
 */
function htmlmail_get_selected_theme(&$selected = NULL) {
  if (!isset($selected)) {
    $selected = variable_get('htmlmail_theme', '');
  }

  // Make sure the selected theme is allowed.
  $themes = htmlmail_get_allowed_themes();
  if (isset($themes[$selected])) {
    return $selected;
  }

  // If a theme was specified but not allowed, fall back to site defaults.
  return $GLOBALS['theme'];
}

Functions

Namesort descending Description
htmlmail_form_user_profile_form_alter Implements hook_form_FORM_ID_alter().
htmlmail_get_allowed_themes Returns an associative array of allowed themes. The keys are the machine-readable names and the values are the .info file names. Code shamelessly stolen from the og_theme module.
htmlmail_get_selected_theme Returns the selected theme to use for outgoing emails. for use in a theme callback function.
htmlmail_help Implements hook_help().
htmlmail_mail Implements hook_mail().
htmlmail_menu Implements hook_menu().
htmlmail_theme Implements hook_theme().
htmlmail_user_presave Implements hook_user_presave().
template_preprocess_htmlmail Process variables to format email messages.