You are here

devel_mail_logger.module in Devel Mail Logger 7

Same filename and directory in other branches
  1. 8 devel_mail_logger.module

File

devel_mail_logger.module
View source
<?php

/**
 * Implements hook_help().
 */
function devel_mail_logger_help($path, $arg) {
  switch ($path) {

    // Main module help for the devel_mail_logger module.
    case 'admin/help#devel_mail_logger':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('A Custom Mail Interface that logs mail to DB') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_menu().
 */
function devel_mail_logger_menu() {
  $items = array();
  $items['admin/reports/devel_mail_logger'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'devel_mail_logger_list',
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'title' => 'List Mails',
    'file' => 'devel_mail_logger.admin.inc',
  );
  $items['admin/reports/devel_mail_logger/mail/%devel_mail_logger/view'] = array(
    'page callback' => 'devel_mail_logger_show_mail',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'title' => 'Mail',
    'file' => 'devel_mail_logger.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/reports/devel_mail_logger/mail/%devel_mail_logger/send'] = array(
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'devel_mail_logger_send_mail',
      4,
    ),
    'access arguments' => array(
      'access site reports',
    ),
    'title' => 'Mail',
    'file' => 'devel_mail_logger.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/reports/devel_mail_logger/send'] = array(
    'page callback' => 'devel_mail_logger_send_test_mail',
    'access arguments' => array(
      'access site reports',
    ),
    'title' => 'Send test mail',
    'file' => 'devel_mail_logger.admin.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_mail().
 */
function devel_mail_logger_mail($key, &$message, $params) {
  $options = array(
    'langcode' => $message['langcode'],
  );
  switch ($key) {
    case 'send_test':
      $message['from'] = variable_get('site_mail');
      $message['subject'] = t('Your mail subject Here: @subject', array(
        '@subject' => $params['subject'],
      ), $options);
      if (is_array($params['message'])) {
        $message['body'] = $params['message'];
      }
      else {
        $message['body'][] = $params['message'];
      }
      break;
    case 'send_mail':
      $message['from'] = variable_get('site_mail');
      $message['subject'] = $params['subject'];
      $message['from_devel_mail_logger'] = TRUE;
      $message['body'] = $params['body'];
      break;
  }
}

/**
 * Implements hook_mail_alter().
 */
function devel_mail_logger_mail_alter(&$message) {
  if (!isset($message['from_devel_mail_logger']) || !$message['from_devel_mail_logger']) {
    $message['send'] = FALSE;
    $record = array(
      'timestamp' => REQUEST_TIME,
      'recipient' => $message['to'],
      'subject' => $message['subject'],
      'message' => json_encode($message),
    );
    if (drupal_write_record('devel_mail_logger', $record) == SAVED_NEW) {
      drupal_set_message(t('Email logged to database'));
    }
    else {
      drupal_set_message(t('Attempt to save email to database failed'), 'error');
    }
  }
}

/**
 * Loader function
 * @param $id
 * @return bool
 */
function devel_mail_logger_load($id) {
  $record = db_select('devel_mail_logger', 'dml')
    ->fields('dml')
    ->condition('id', $id)
    ->execute()
    ->fetchAllAssoc('id');
  return isset($record[$id]) ? $record[$id] : FALSE;
}