You are here

mail_logger.module in Mail Logger 5

Same filename and directory in other branches
  1. 6 mail_logger.module
  2. 7 mail_logger.module

File

mail_logger.module
View source
<?php

/**
 * implementation of hook_perm().
 *
 * @return unknown
 */
function mail_logger_perm() {
  return array(
    'access mail logger',
  );
}

/**
 * Implementation of hook_menu().
 */
function mail_logger_menu($may_cache) {
  $items = array();
  if (!$may_cache) {
    $items[] = array(
      'path' => 'admin/logs/mail-logger',
      'title' => t('Outgoing Mail log entries'),
      'description' => t('View Mails that have been sent from this site.'),
      'callback' => 'mail_logger_overview',
      'weight' => -1,
      'access' => user_access('access mail logger'),
    );
    $items[] = array(
      'path' => 'admin/logs/mail-logger/mail',
      'title' => t('Outgoing Mail log entries'),
      'description' => t('View information about a single logged mail entry'),
      'callback' => 'mail_logger_read_mail',
      'weight' => -1,
      'access' => user_access('access mail logger'),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}
function mail_logger_read_mail($id) {
  if (!isset($id) || !is_numeric($id)) {
    return 'Invalid Mail Logger ID parameter';
  }
  else {
    if ($result = db_query('SELECT * FROM {mail_logger} WHERE mlid = %d', $id)) {
      drupal_add_css(drupal_get_path('module', 'mail_logger') . '/mail_logger.css');
      return theme('mail_logger_read_mail', db_fetch_object($result));
    }
    else {
      return 'No Mail Logger record found with id ' . $id;
    }
  }
}
function theme_mail_logger_read_mail($mail) {
  $output = '';
  $output .= '<div class="mail_logger_wrapper">';
  $output .= '<div class="mail_type_label">Mail Type: </div><div class="mail_type_data">' . $mail->mailkey . '</div>';
  $output .= '<div class="date_sent_label">Date Sent: </div><div class="date_sent_data">' . format_date($mail->date_sent, 'small') . '</div>';
  $output .= '<div class="mail_from_label">From: </div><div class="mail_from_data">' . check_plain($mail->from) . '</div>';
  $output .= '<div class="mail_to_label">To: </div><div class="mail_to_data">' . check_plain($mail->to) . '</div>';
  $output .= '<div class="mail_subject_label">Subject: </div><div class="mail_subject_data">' . check_plain($mail->subject) . '</div>';
  $output .= '<div class="mail_body_label">Body: </div><div class="mail_body_data">' . nl2br(check_plain($mail->body)) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * implementation of hook_mail_alter
 *
 * Purpose of this function is to log all outgoing mail
 */
function mail_logger_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers = '') {
  $args = array(
    $mailkey,
    $to,
    $subject,
    $body,
    $from,
    serialize($headers),
    time(),
  );
  db_query("INSERT INTO {mail_logger} (\n  `mlid` , `mailkey` , `to` , `subject` , `body` , `from` , `headers`, `date_sent`\n  ) VALUES ( \n  NULL, '%s', '%s', '%s', '%s', '%s', '%s', %d )", $args);
}

/**
 * Returns types of mailkeys in mail_logger table
 *
 */
function _mail_logger_get_mailkey_types() {
  $types = array();
  $result = db_query('SELECT DISTINCT(mailkey) FROM {mail_logger} ORDER BY mailkey');
  while ($object = db_fetch_object($result)) {
    $types[] = $object->mailkey;
  }
  return $types;
}
function mail_logger_form_overview() {
  $names['all'] = t('all mail types');
  foreach (_mail_logger_get_mailkey_types() as $type) {
    $names[$type] = t('!type', array(
      '!type' => t($type),
    ));
  }
  if (empty($_SESSION['mail_logger_overview_filter'])) {
    $_SESSION['mail_logger_overview_filter'] = 'all';
  }
  $form['filter'] = array(
    '#type' => 'select',
    '#title' => t('Filter by Mailkey'),
    '#options' => $names,
    '#default_value' => $_SESSION['mail_logger_overview_filter'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );
  $form['#redirect'] = FALSE;
  return $form;
}

/**
 * hook_submit for mail_logger_form_overview
 *
 */
function mail_logger_form_overview_submit($form_id, $form_values) {
  $_SESSION['mail_logger_overview_filter'] = $form_values['filter'];
}

/**
 * theme function for mail_logger_form_overview
 *
 */
function theme_mail_logger_form_overview($form) {
  return '<div class="container-inline">' . drupal_render($form) . '</div>';
}

/**
 * Menu callback; displays a listing of log mails.
 */
function mail_logger_overview() {
  $output = drupal_get_form('mail_logger_form_overview');
  $header = array(
    array(
      'data' => t('Mailkey'),
      'field' => 'ml.mailkey',
    ),
    array(
      'data' => t('To'),
      'field' => 'ml.to',
    ),
    array(
      'data' => t('From'),
      'field' => 'ml.from',
    ),
    array(
      'data' => t('Subject'),
      'field' => 'ml.subject',
    ),
    array(
      'data' => t('Date Sent'),
      'field' => 'ml.date_sent',
    ),
  );
  $tablesort = tablesort_sql($header);
  $sql = "SELECT ml.mlid, ml.mailkey, ml.to, ml.from, ml.subject, ml.date_sent FROM {mail_logger} ml";
  $type = $_SESSION['mail_logger_overview_filter'];
  if ($type != 'all') {
    $result = pager_query($sql . " WHERE ml.mailkey = '%s'" . $tablesort, 50, 0, NULL, $type);
  }
  else {
    $result = pager_query($sql . $tablesort, 50);
  }
  while ($mail_logger = db_fetch_object($result)) {
    $rows[] = array(
      'data' => array(
        // Cells
        $mail_logger->mailkey,
        $mail_logger->to,
        $mail_logger->from,
        l(truncate_utf8($mail_logger->subject, 56, TRUE, TRUE), 'admin/logs/mail-logger/mail/' . $mail_logger->mlid),
        format_date($mail_logger->date_sent, 'small'),
      ),
    );
  }
  if (!$rows) {
    $rows[] = array(
      array(
        'data' => t('No log messages available.'),
        'colspan' => 6,
      ),
    );
  }
  $output .= theme('table', $header, $rows);
  $output .= theme('pager', NULL, 50, 0);
  return $output;
}

Functions

Namesort descending Description
mail_logger_form_overview
mail_logger_form_overview_submit hook_submit for mail_logger_form_overview
mail_logger_mail_alter implementation of hook_mail_alter
mail_logger_menu Implementation of hook_menu().
mail_logger_overview Menu callback; displays a listing of log mails.
mail_logger_perm implementation of hook_perm().
mail_logger_read_mail
theme_mail_logger_form_overview theme function for mail_logger_form_overview
theme_mail_logger_read_mail
_mail_logger_get_mailkey_types Returns types of mailkeys in mail_logger table