You are here

maillog.module in Maillog / Mail Developer 6

Same filename and directory in other branches
  1. 8 maillog.module
  2. 7 maillog.module

Provides a 'maillog' node type

@todo: better implementation of detail link

Extensibility of maillog engines is based on the mimemail engine hooks. See mimemail_get_engines in mimemail.module with Please install mimemail to make the outgoing engine pluggable.

mail flow: drupal_mail -> maillog:drupal_mail_wrapper -> maillog_mail_send mimemail -> maillog_mailengine -> maillog_mail_send [-> ANY_engine]

File

maillog.module
View source
<?php

/**
 * @file
 * Provides a 'maillog' node type
 *
 * @todo: better implementation of detail link
 *
 *
 * Extensibility of maillog engines is based on the mimemail engine hooks.
 * See mimemail_get_engines in mimemail.module with
 * Please install mimemail to make the outgoing engine pluggable.
 *
 * mail flow:
 * drupal_mail -> maillog:drupal_mail_wrapper -> maillog_mail_send
 * mimemail -> maillog_mailengine -> maillog_mail_send [-> ANY_engine]
 */

/**
 * Implementation of hook_perm().
 */
function maillog_perm() {
  return array(
    'view maillog',
    'delete maillog',
    'administer maillog',
  );
}

/**
 * Implementation of hook_menu().
 */
function maillog_maillog_delete($maillog) {
  $idmaillog = $maillog['idmaillog'];
  $result = db_query("DELETE FROM {maillog} WHERE `idmaillog` = '%d'", $idmaillog);
  if ($result == FALSE) {
    drupal_set_message(t('A Problem occured when deleting Mail with idmaillog !idmaillog !', array(
      '!idmaillog' => $idmaillog,
    )));
  }
  else {
    drupal_set_message(t('Mail with idmaillog !idmaillog has been deleted!', array(
      '!idmaillog' => $idmaillog,
    )));
  }
  drupal_goto('maillog/log');
  return '';
}

/**
 * Implementation of hook_menu().
 */
function maillog_menu() {
  $items = array();
  $items['admin/settings/maillog'] = array(
    'title' => t('Maillog Settings'),
    'description' => t('Configure the settings of Maillog module.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'maillog_admin_settings',
    ),
    'access arguments' => array(
      'administer maillog',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['maillog/delete/%maillog_maillog'] = array(
    'title' => t("Delete Maillog from 'maillog' Table"),
    'description' => t("Delete the Maillog with the idmaillog given by the parameter from 'maillog' Table"),
    'page callback' => 'maillog_maillog_delete',
    'page arguments' => array(
      2,
    ),
    'access arguments' => array(
      'delete maillog',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['maillog/details/%maillog_maillog'] = array(
    'title callback' => 'maillog_maillog_title',
    'title arguments' => array(
      2,
    ),
    'description' => t("Delete the Maillog with the idmaillog given by the parameter from 'maillog' Table"),
    'page callback' => 'maillog_maillog_page',
    'page arguments' => array(
      2,
    ),
    'access arguments' => array(
      'view maillog',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 *
 */
function maillog_maillog_title($maillog) {
  return $maillog['subject'];
}

/**
 *
 */
function maillog_maillog_load($idmaillog) {
  $result = db_query("SELECT idmaillog, header_from, header_to, header_reply_to, header_all, subject, body FROM {maillog} WHERE idmaillog = '%d'", $idmaillog);
  if ($result == FALSE) {
    $maillog = NULL;
  }
  else {
    $maillog = db_fetch_array($result);

    // unserialize values
    $maillog['header_all'] = unserialize($maillog['header_all']);
  }
  return $maillog;
}

/**
 *
 */
function maillog_maillog_page($maillog) {
  return theme('maillog', $maillog);
}

/**
 * Implementation of the module settings form.
 */
function maillog_admin_settings() {
  $form = array();
  $form['maillog_send'] = array(
    '#type' => 'checkbox',
    '#title' => t("Allow the e-mails to be sent."),
    '#default_value' => variable_get('maillog_send', TRUE),
  );
  $form['maillog_log'] = array(
    '#type' => 'checkbox',
    '#title' => t("Create table entries in maillog table for each e-mail."),
    '#default_value' => variable_get('maillog_log', TRUE),
  );
  $form['maillog_devel'] = array(
    '#type' => 'checkbox',
    '#title' => t("Display the e-mails on page using devel module (if enabled)."),
    '#default_value' => variable_get('maillog_devel', TRUE),
  );
  if (module_exists('mimemail')) {
    $engines = mimemail_get_engines();

    // maillog will be unset, because ist would cause an recursion
    unset($engines['maillog']);
    $form['maillog_engine'] = array(
      '#type' => 'select',
      '#title' => t("Select the mailengine which should be used."),
      '#default_value' => variable_get('maillog_engine', 'mimemail'),
      '#options' => $engines,
    );
  }
  return system_settings_form($form);
}

/**
 * Implementation of hook_views_api().
 */
function maillog_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'maillog') . '/includes/',
  );
}

/**
 * Implementation of hook_mailengine from mimemail modul
 */
function maillog_mailengine($op, $message = NULL) {

  // default values
  if (is_array($message)) {
    $message = array_merge(array(
      'address' => '',
      'subject' => '',
      'body' => '',
      'sender' => '',
      'headers' => '',
    ), $message);
  }
  switch ($op) {
    case 'name':
      return t('Maillog Mail');
    case 'description':
      return t("Maillog engine using drupal_mail() or another mailengine from maillog_mailengine variable to send mail.");
    case 'settings':

      // not implemented
      return FALSE;
    case 'multiple':
    case 'single':
    case 'send':
      $message['from'] = $message['sender'];
      $message['to'] = $message['address'];
      unset($message['sender']);
      unset($message['address']);
      return maillog_mail_send($message, TRUE);
      break;
  }
}

/**
 * Implementation of hook_theme().
 */
function maillog_theme() {
  return array(
    'maillog_header_from' => array(
      'arguments' => array(
        'header_from',
      ),
    ),
    'maillog_header_to' => array(
      'arguments' => array(
        'header_to',
      ),
    ),
    'maillog_header_reply_to' => array(
      'arguments' => array(
        'header_reply_to',
      ),
    ),
    'maillog_header_all' => array(
      'arguments' => array(
        'header_all',
      ),
    ),
    'maillog_body' => array(
      'arguments' => array(
        'body',
      ),
    ),
    'maillog' => array(
      'arguments' => array(
        'maillog',
      ),
    ),
  );
}

/**
 * Render the 'From' field in the node type 'Logged Mail'
 */
function theme_maillog_header_from($header_from) {
  $output = '';
  $output .= '<div class="field mail-log-header-from">';
  $output .= '<div class="field-label">' . t('From') . ':</div>';
  $output .= '<div class="field-item">' . check_plain($header_from) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 *
 */
function theme_maillog($maillog) {
  $output = theme('maillog_header_from', $maillog['header_from']);
  $output .= theme('maillog_header_to', $maillog['header_to']);
  $output .= theme('maillog_header_reply_to', $maillog['header_reply_to']);
  $output .= theme('maillog_header_all', $maillog['header_all']);
  $output .= theme('maillog_body', $maillog['body']);
  return $output;
}

/**
 * Render the 'To' field in the node type 'Logged Mail'
 */
function theme_maillog_header_to($header_to) {
  $output = '';
  $output .= '<div class="field mail-log-header-to">';
  $output .= '<div class="field-label">' . t('To') . ':</div>';
  $output .= '<div class="field-item">' . check_plain($header_to) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Render the 'Reply-To' field in the node type 'Logged Mail'
 */
function theme_maillog_header_reply_to($header_reply_to) {
  $output = '';
  $output .= '<div class="field mail-log-header-reply-to">';
  $output .= '<div class="field-label">' . t('Reply To') . ':</div>';
  $output .= '<div class="field-item">' . check_plain($header_reply_to) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Render the 'Header' field in the node type 'Logged Mail'
 */
function theme_maillog_header_all($header_all) {
  $output = '';
  $output .= '<div class="field mail-log-header-all">';
  $output .= '<div class="field-label">' . t('Header') . ':</div>';
  $output .= '<div class="field-item">';
  foreach ($header_all as $header_all_name => $header_all_value) {
    $output .= '<div class="mail-log-header-all-subitem">';
    $output .= check_plain($header_all_name) . ': ' . check_plain($header_all_value);
    $output .= '</div>';
  }
  $output .= '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Render the 'Body' field in the node type 'Logged Mail'
 */
function theme_maillog_body($body) {
  $output = '';
  $output .= '<div class="field mail-log-body">';
  $output .= '<div class="field-label">' . t('Body') . ':</div>';
  $output .= '<div class="field-item">';
  $output .= '<pre>';
  $output .= check_plain($body);
  $output .= '</pre>';
  $output .= '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Receive a command to send a mail.
 *
 * $message
 * to: recipient
 * from: source
 * $mailengine_call TRUE if function called from maillog_mailengine, otherwise falls. This variable helps to prevent a kind of recursion.
 *
 * @return
 * TRUE on success
 */
function maillog_mail_send($message, $mailengine_call = FALSE) {

  // Static variable used to check for recursive mail sending and prevent it
  static $recursive_send = FALSE;
  if ($recursive_send) {
    drupal_set_message(t('Maillog has detected recursive mail sending.'), 'warning');
    return FALSE;
  }
  $recursive_send = TRUE;

  // Log the e-mail
  if (variable_get('maillog_log', TRUE)) {
    $record = new stdClass();
    $record->header_message_id = isset($message['headers']['Message-ID']) ? $message['headers']['Message-ID'] : NULL;
    $record->subject = $message['subject'];
    $record->body = $message['body'];
    $record->header_from = isset($message['from']) ? $message['from'] : NULL;
    $record->header_to = NULL;
    if (isset($message['to'])) {
      $header_to = $message['to'];
      if (is_array($header_to)) {
        $header_to = implode(', ', $header_to);
      }
      $record->header_to = $header_to;
    }
    $record->header_reply_to = isset($message['headers']['Reply-To']) ? $message['headers']['Reply-To'] : '';
    $record->header_all = serialize($message['headers']);
    $record->sent_date = time();
    drupal_write_record('maillog', $record);
  }

  // Display the e-mail using Devel module
  if (variable_get('maillog_devel', TRUE) && function_exists('dpm')) {
    $devel_msg = array();
    $devel_msg[t('Subject')] = $message['subject'];
    $devel_msg[t('From')] = $message['from'];
    $devel_msg[t('To')] = $message['to'];
    $devel_msg[t('Reply-To')] = $message['reply_to'];
    $devel_msg[t('Header')] = $message['headers'];
    $devel_msg[t('Body')] = $message['body'];
    dpm($devel_msg, 'maillog');
  }

  // Send the e-mail
  $ret = FALSE;
  if (variable_get('maillog_send', TRUE)) {

    // note: CORE: we're unable to support further mail wrappers here (function name limit)
    // We implement mimemail hook_engine for extensibility.
    $mail_sent = FALSE;
    if ($mailengine_call == TRUE) {
      $engine = variable_get('maillog_engine', '') . '_mailengine';
      if (function_exists($engine) && $engine != 'maillog_mailengine') {
        $message['sender'] = $message['from'];
        $message['address'] = $message['to'];
        unset($message['from']);
        unset($message['to']);
        $ret = $engine('send', $message);
        $mail_sent = TRUE;
      }
    }
    if ($mail_sent == FALSE) {

      // build headers
      $mimeheaders = array();
      foreach ($message['headers'] as $name => $value) {
        $mimeheaders[] = $name . ': ' . mime_header_encode($value);
      }
      $mail_headers = join("\n", $mimeheaders);

      // encode subject
      $mail_subject = mime_header_encode($message['subject']);

      // cleanup body
      $mail_body = str_replace("\r", '', $message['body']);
      $ret = mail($message['to'], $mail_subject, $mail_body, $mail_headers);
    }
  }
  else {
    static $mailwarning = TRUE;
    if ($mailwarning == TRUE) {
      if (user_access('administer maillog')) {
        drupal_set_message(t('Sending of e-mail messages is disabled by Maillog module. Go ' . l('here', 'admin/settings/maillog') . ' to enable.'), 'warning', TRUE);
      }
      else {
        drupal_set_message(t('Drupal tried to send a mail. Note that mails are disabled currently.'), 'warning', TRUE);
      }
    }
    $mailwarning = FALSE;

    // important: report fake success for sending mail for debugging purposes
    $ret = TRUE;
  }
  $recursive_send = FALSE;
  return $ret;
}

Functions

Namesort descending Description
maillog_admin_settings Implementation of the module settings form.
maillog_mailengine Implementation of hook_mailengine from mimemail modul
maillog_maillog_delete Implementation of hook_menu().
maillog_maillog_load
maillog_maillog_page
maillog_maillog_title
maillog_mail_send Receive a command to send a mail.
maillog_menu Implementation of hook_menu().
maillog_perm Implementation of hook_perm().
maillog_theme Implementation of hook_theme().
maillog_views_api Implementation of hook_views_api().
theme_maillog
theme_maillog_body Render the 'Body' field in the node type 'Logged Mail'
theme_maillog_header_all Render the 'Header' field in the node type 'Logged Mail'
theme_maillog_header_from Render the 'From' field in the node type 'Logged Mail'
theme_maillog_header_reply_to Render the 'Reply-To' field in the node type 'Logged Mail'
theme_maillog_header_to Render the 'To' field in the node type 'Logged Mail'