Drupal 6: Creating a module to email severe log entries to users

In this code snippet I’ll explain how you can create a module that emails log entries of a defined severity to a list of users.

The first step is to create an admin setting page that allows the admin to define the email recipients.

<?php

/**
 * Implements hook_perm()
 */
function logemail_perm() {
  return array(
    'administer log email',
  );
}

/**
 * Implements hook_menu()
 */
function logemail_menu() {

  $items = array();

  // admin settings page callback
  $items['admin/settings/log-email'] = array(
    'title' => t('Log Email'),
    'description' => t('Configure Log Email settings'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('logemail_callback_admin_settings'),
    'access arguments' => array('administer log email'),
  );

  return $items;
}

/**
 * Implements form page callback for module admin settings page
 */
function logemail_callback_admin_settings() {

  $form = array();

  $form['logemail_recipients'] = array(
    '#type' => 'textfield',
    '#title' => t('Email Recipients'),
    '#default_value' => variable_get('logemail_recipients', ''),
    '#description' => t('Enter email addresses comma separated.'),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}

/**
 * Implements validation callback for admin settings form
 */
function logemail_callback_admin_settings_validate($form, &$form_state) {

  // explode email addresses on comma
  $emails = explode(',', trim($form_state['values']['logemail_recipients']));

  if (!count($emails) || !is_array($emails)) {
    form_set_error('logemail_recipients', t('Invalid email addresses.'));
    return;
  }

  // loop through each email address and validate
  foreach ($emails as $key => $value) {

    // validate email address
    $validate_result = valid_email_address(trim($value));

    if ($validate_result !== 1) {
      form_set_error('logemail_recipients', t('Invalid email addresses.'));
      return;
    }
    else {
      // trim email addresses
      $emails[$key] = trim($value);
    }
  }

  // set trimmed values
  $form_state['values']['logemail_recipients'] = implode(',', $emails);
}
?>

The above code creates an admin setting page that allows the admin to define the recipients list, and implements validation to ensure the email addresses exist and are valid.

Log Email Admin Settings

The next two functions implement hook_mail() and hook_watchdog() to check the severity of the log entry, compose the email message, and send it.

<?php
/**
 * Implements hook_watchdog()
 */
function logemail_watchdog($log_entry) {

  // check severity of log entry
  if ($log_entry['severity']<5) {

    // define parameters for drupal_mail
    $module = 'logemail';
    $key = 'watchdog';
    $to = variable_get('logemail_recipients', '');
    $language = language_default();
    $params = array(
      'log_entry' => $log_entry,
    );

    // send email using drupal_mail
    drupal_mail($module, $key, $to, $language, $params);
  }
}

/**
 * Implements hook_mail()
 */
function logemail_mail($key, &$message, $params) {
  switch ($key) {
    case 'watchdog':
      // set email subject
      $message['subject'] = t("Log Email Watchdog") . ": " . $params['log_entry']['type'];

      // define email body - start

      // process variables using t()
      $email_body = t($params['log_entry']['message'], $params['log_entry']['variables']);

      // decode html special characters
      $email_body = htmlspecialchars_decode($email_body);

      // remove html tags
      $email_body = strip_tags($email_body);

      // define email body - end

      // set email body
      $message['body'] = $email_body;

      break;
  }
}
?>

Now when log entries are created that match the defined severity, they are emailed to the recipient list.

Updated: