You are here

mail_safety.module in Mail Safety 7.2

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

File

mail_safety.module
View source
<?php

/**
 * @file
 * The core Mail Safety module file
 */
require_once 'mail_safety.entity.inc';

/**
 * Implements hook_menu().
 */
function mail_safety_menu() {
  $menu_items = array();
  $menu_items['admin/config/development/mail_safety/dashbard'] = array(
    'title' => 'Dashboard',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'use mail safety dashboard',
    ),
    'weight' => -10,
  );
  $menu_items['admin/config/development/mail_safety/%mail_safety_mail/view'] = array(
    'title' => 'View Mail',
    'page callback' => 'mail_safety_admin_view_mail',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'use mail safety dashboard',
    ),
    'file' => 'mail_safety.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $menu_items['admin/config/development/mail_safety/%mail_safety_mail/details'] = array(
    'title' => 'Details Mail',
    'page callback' => 'mail_safety_admin_details_mail',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'use mail safety dashboard',
    ),
    'file' => 'mail_safety.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $menu_items['admin/config/development/mail_safety/%mail_safety_mail/send_original'] = array(
    'title' => 'Send Original',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mail_safety_admin_send_original_form',
      4,
    ),
    'access arguments' => array(
      'use mail safety dashboard',
    ),
    'file' => 'mail_safety.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $menu_items['admin/config/development/mail_safety/%mail_safety_mail/send_default'] = array(
    'title' => 'Send Default',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mail_safety_admin_send_default_form',
      4,
    ),
    'access arguments' => array(
      'use mail safety dashboard',
    ),
    'file' => 'mail_safety.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $menu_items['admin/config/development/mail_safety/%mail_safety_mail/delete'] = array(
    'title' => 'Delete Mail',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mail_safety_admin_delete_form',
      4,
    ),
    'access arguments' => array(
      'use mail safety dashboard',
    ),
    'file' => 'mail_safety.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $menu_items['admin/config/development/mail_safety/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mail_safety_admin_settings_form',
    ),
    'access arguments' => array(
      'administer mail safety',
    ),
    'file' => 'mail_safety.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => -9,
  );
  return $menu_items;
}

/**
 * Implements hook_theme().
 */
function mail_safety_theme() {
  return array(
    'mail_safety_mail' => array(
      'variables' => array(
        'mail_safety_mail' => NULL,
      ),
    ),
    'mail_safety_details' => array(
      'variables' => array(
        'mail_safety_mail' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_mail_alter().
 *
 * Filters any e-mail going through the drupal mail system.
 *
 * If Mail Safety is enabled it stops all mails from being sent depending on
 * the settings it will either send the mails to a default mail address and/or
 * send it to the dashboard.
 */
function mail_safety_mail_alter(&$message) {
  if (variable_get('mail_safety_enabled', FALSE)) {
    $message['send'] = FALSE;
  }

  // If mail to dashboard is enabled it'll send the mail to the dashboard.
  if (variable_get('mail_safety_send_mail_to_dashboard', TRUE)) {
    mail_safety_insert_mail($message);
  }

  // If mail to default mail is enabled it will send the mail to the default
  // mail address.
  if (variable_get('mail_safety_send_mail_to_default_mail', TRUE)) {
    $message['to'] = variable_get('mail_safety_default_mail_address', '');
    unset($message['headers']['CC']);
    unset($message['headers']['BCC']);
    $message['send'] = TRUE;
  }
}

/**
 * Implements hook_permission().
 */
function mail_safety_permission() {
  return array(
    'use mail safety dashboard' => array(
      'description' => t('Use the Mail Safety dashboard.'),
      'title' => t('Use Mail Safety dashboard'),
      'restrict access' => TRUE,
    ),
    'administer mail safety' => array(
      'title' => t('Administer Mail Safety'),
      'description' => t('Enable and configure Mail Safety'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Saves the mail to the dashboard.
 *
 * @param array $message
 *   The drupal message array.
 */
function mail_safety_insert_mail($message) {

  // Let other modules alter the message array before a mail is inserted.
  // E.g. save attachments that are sent with the mail.
  drupal_alter('mail_safety_pre_insert', $message);
  $mail_safety_mail = new stdClass();
  $mail_safety_mail->mail_body = implode(' ', $message['body']);
  $mail_safety_mail->mail_subject = $message['subject'];
  $mail_safety_mail->mail_to = $message['to'];
  $mail_safety_mail->mail_cc = '';
  $mail_safety_mail->mail_key = $message['key'];
  $mail_safety_mail->mail_module = $message['module'];
  $mail_safety_mail->mail_serialized = serialize($message);
  $mail_safety_mail->created = time();
  $mail_safety_mail->sent = time();
  mail_safety_mail_save($mail_safety_mail);
}

/**
 * Delete a specific mail from the dashboard.
 *
 * @param int $mail_id
 *   The mail id as it is saved in the mail safety table.
 */
function mail_safety_delete_mail($mail_id = NULL) {
  mail_safety_mail_delete($mail_id);
  return TRUE;
}

/**
 * Implements hook_module_implements_alter().
 *
 * Make sure our hook is called last.
 */
function mail_safety_module_implements_alter(&$implementations, $hook) {
  if ($hook == 'mail_alter' && isset($implementations['mail_safety'])) {

    // Move mail_safety_mail_alter() to the end of the list.
    $group = $implementations['mail_safety'];
    unset($implementations['mail_safety']);
    $implementations['mail_safety'] = $group;
  }
}

/**
 * Returns HTML for the display of a mail.
 *
 * @param array $variables
 *   An associative array containing:
 *   - mail: The message array that is being formatted.
 *
 * @ingroup themeable
 */
function theme_mail_safety_mail($variables) {
  $mail_safety_mail = $variables['mail_safety_mail'];
  $message = $mail_safety_mail->message;
  $output = '<h2>' . $message['subject'] . '</h2>';
  $output .= '<div class="mail-body">' . $message['body'] . '</div>';
  return check_markup($output);
}

/**
 * Returns HTML for the details of a mail.
 *
 * @param array $variables
 *   An associative array containing:
 *   - mail: The message array that is being formatted.
 *
 * @ingroup themeable
 */
function theme_mail_safety_details($variables) {
  $mail_safety_mail = $variables['mail_safety_mail'];
  $message = $mail_safety_mail->message;
  $output = '<h2>' . $message['subject'] . '</h2>';
  $output .= '<pre>' . print_r($message, TRUE) . '</pre>';
  return check_markup($output);
}

Functions

Namesort descending Description
mail_safety_delete_mail Delete a specific mail from the dashboard.
mail_safety_insert_mail Saves the mail to the dashboard.
mail_safety_mail_alter Implements hook_mail_alter().
mail_safety_menu Implements hook_menu().
mail_safety_module_implements_alter Implements hook_module_implements_alter().
mail_safety_permission Implements hook_permission().
mail_safety_theme Implements hook_theme().
theme_mail_safety_details Returns HTML for the details of a mail.
theme_mail_safety_mail Returns HTML for the display of a mail.