You are here

mail_safety.admin.inc in Mail Safety 7.2

Same filename and directory in other branches
  1. 7 mail_safety.admin.inc

Admin functionality for Mail Safety

File

mail_safety.admin.inc
View source
<?php

/**
 * @file
 * Admin functionality for Mail Safety
 */

/**
 * Form constructor for the Mail Safety settings form.
 *
 * @ingroup forms
 */
function mail_safety_admin_settings_form() {
  $form = array();
  $form['mail_safety_enabled'] = array(
    '#title' => t('Stop outgoing mails'),
    '#type' => 'checkbox',
    '#description' => t('When Mail Safety is enabled it will stop all outgoing emails from being sent and will send them to either the dashboard and/or the defaut mail address instead.'),
    '#default_value' => variable_get('mail_safety_enabled', FALSE),
  );
  $form['mail_safety_send_mail_to_dashboard'] = array(
    '#title' => t('Send mail to dashboard'),
    '#type' => 'checkbox',
    '#description' => t('If enabled, all mails will be sent to the dashboard'),
    '#default_value' => variable_get('mail_safety_send_mail_to_dashboard', TRUE),
  );
  $form['mail_safety_send_mail_to_default_mail'] = array(
    '#title' => t('Send mail to default mail'),
    '#type' => 'checkbox',
    '#description' => t('If enabled, all mails will be sent to the the default mail address'),
    '#default_value' => variable_get('mail_safety_send_mail_to_default_mail', TRUE),
  );
  $form['mail_safety_default_mail_address'] = array(
    '#title' => t('Default mail address'),
    '#type' => 'textfield',
    '#description' => t('The default email address that outgoing e-mails will be rerouted to if enabled.'),
    '#default_value' => variable_get('mail_safety_default_mail_address', ''),
  );
  return system_settings_form($form);
}

/**
 * Page callback: Displays a mail filtered by the dashboard.
 *
 * @param stdClass $mail_safety_mail
 *   The mail safety mail entity filtered by Mail Safety
 * 
 * @see node_menu()
 */
function mail_safety_admin_view_mail($mail_safety_mail) {
  $mail_system = drupal_mail_system($mail_safety_mail->mail_module, $mail_safety_mail->mail_key);
  $mail_safety_mail->message = $mail_system
    ->format($mail_safety_mail->message);
  return theme('mail_safety_mail', array(
    'mail_safety_mail' => $mail_safety_mail,
  ));
}

/**
 * Page callback: Displays a mail its parameters filtered by the dashboard.
 *
 * @param stdClass $mail_safety_mail
 *   The mail safety mail entity filtered by Mail Safety
 *   
 * @see node_menu()
 */
function mail_safety_admin_details_mail($mail_safety_mail) {
  return theme('mail_safety_details', array(
    'mail_safety_mail' => $mail_safety_mail,
  ));
}

/**
 * Form constructor for the send to original mail address form.
 * 
 * @see mail_safety_admin_send_original_form()
 *
 * @ingroup forms
 */
function mail_safety_admin_send_original_form($form, $form_state, $mail_safety_mail) {
  $form = array();
  $form['mid'] = array(
    '#type' => 'value',
    '#value' => $mail_safety_mail->mid,
  );
  $form['#submit'] = array(
    'mail_safety_admin_send_original_form_submit',
  );
  return confirm_form($form, t('Are you sure you want to send "@subject" to @to?', array(
    '@subject' => $mail_safety_mail->mail_subject,
    '@to' => $mail_safety_mail->mail_to,
  )), 'admin/config/development/mail_safety', t('This will send the mail to the original recipient.'), t('Send'));
}

/**
 * Form submission handler for mail_safety_admin_send_original_form.
 * 
 * Sends the mail to the original recipient.
 */
function mail_safety_admin_send_original_form_submit(&$form, &$form_state) {
  if (!empty($form_state['values']['mid']) && ($mail_safety_mail = mail_safety_mail_load($form_state['values']['mid']))) {

    // Resend the mail and bypass mail_alter by using the drupal_mail_system
    $mail_array = $mail_safety_mail->message;
    $mail_array['send'] = TRUE;

    // Let other modules respond before a mail is sent.
    // E.g. add attachments that were saved in the mail.
    $modules = module_implements('mail_safety_pre_send');
    foreach ($modules as $module) {
      $mail_array = module_invoke($module, 'mail_safety_pre_send', $mail_array);
    }
    $system = drupal_mail_system($mail_array['module'], $mail_array['key']);
    $mail_array = $system
      ->format($mail_array);
    $mail_array['result'] = $system
      ->mail($mail_array);
    if ($mail_array['result']) {
      drupal_set_message(t('Succesfully sent the message to @to', array(
        '@to' => $mail_array['to'],
      )));
    }
    else {
      drupal_set_message(t('Failed to send the message to @to', array(
        '@to' => $mail_array['to'],
      )), 'error');
    }
  }
  else {
    drupal_set_message(t('Could not send the mail to the original sender'), 'error');
  }
}

/**
 * Form constructor for the send to default mail address form.
 * 
 * @see mail_safety_admin_send_default_form()
 *
 * @ingroup forms
 */
function mail_safety_admin_send_default_form($form, $form_state, $mail_safety_mail) {
  $form = array();
  $form['to'] = array(
    '#type' => 'textfield',
    '#title' => t('Send to another address'),
    '#description' => t('Only use this field if you want the e-mail to go to an address other than the default address.'),
    '#default_value' => variable_get('mail_safety_default_mail_address', ''),
  );
  $form['mid'] = array(
    '#type' => 'value',
    '#value' => $mail_safety_mail->mid,
  );
  $form['#submit'] = array(
    'mail_safety_admin_send_default_form_submit',
  );
  return confirm_form($form, t('Are you sure you want to send "@subject" to @to?', array(
    '@subject' => $mail_safety_mail->mail_subject,
    '@to' => variable_get('mail_safety_default_mail_address', ''),
  )), 'admin/config/development/mail_safety', t('This will send the mail to the default e-mail address.'), t('Send'));
}

/**
 * Form submission handler for mail_safety_admin_send_default_form_submit.
 * 
 * Sends the mail to a default recipient.
 */
function mail_safety_admin_send_default_form_submit(&$form, &$form_state) {
  if (!empty($form_state['values']['mid']) && !empty($form_state['values']['to']) && ($mail_safety_mail = mail_safety_mail_load($form_state['values']['mid']))) {

    // Resend the mail and bypass mail_alter by using
    // the drupal_mail_system.
    $mail_array = $mail_safety_mail->message;
    $mail_array['send'] = TRUE;

    // Let other modules respond before a mail is sent.
    // E.g. add attachments that were saved in the mail.
    $modules = module_implements('mail_safety_pre_send');
    foreach ($modules as $module) {
      $mail_array = module_invoke($module, 'mail_safety_pre_send', $mail_array);
    }

    // Change the recipient to the default recipient.
    $mail_array['to'] = $form_state['values']['to'];

    // Remove the CC recipients.
    unset($mail_array['headers']['CC']);
    $system = drupal_mail_system($mail_array['module'], $mail_array['key']);
    $mail_array = $system
      ->format($mail_array);
    $mail_array['result'] = $system
      ->mail($mail_array);
    if ($mail_array['result']) {
      drupal_set_message(t('Succesfully sent the message to @to', array(
        '@to' => $mail_array['to'],
      )));
    }
    else {
      drupal_set_message(t('Failed to send the message to @to', array(
        '@to' => $mail_array['to'],
      )), 'error');
    }
  }
  else {
    drupal_set_message(t('Could not send the mail to the original sender'), 'error');
  }
}

/**
 * Form constructor to delete a mail from the dashboard.
 * 
 * @see mail_safety_admin_delete_form_submit()
 *
 * @ingroup forms
 */
function mail_safety_admin_delete_form($form, $form_state, $mail_safety_mail) {
  $form = array();
  $form['mid'] = array(
    '#type' => 'value',
    '#value' => $mail_safety_mail->mid,
  );
  $form['#submit'] = array(
    'mail_safety_admin_delete_form_submit',
  );
  return confirm_form($form, t('Are you sure you want to delete "@subject"?', array(
    '@subject' => $mail_safety_mail->mail_subject,
  )), 'admin/config/development/mail_safety', t('This will remove the mail from the dashboard.'), t('Delete'));
}

/**
 * Form submission handler for mail_safety_admin_delete_form_submit.
 * 
 * Delete a mail from the dashboard.
 */
function mail_safety_admin_delete_form_submit(&$form, &$form_state) {
  if (!empty($form_state['values']['mid'])) {
    mail_safety_mail_delete($form_state['values']['mid']);
    drupal_set_message(t('Succesfully deleted the mail'));
  }
}

Functions

Namesort descending Description
mail_safety_admin_delete_form Form constructor to delete a mail from the dashboard.
mail_safety_admin_delete_form_submit Form submission handler for mail_safety_admin_delete_form_submit.
mail_safety_admin_details_mail Page callback: Displays a mail its parameters filtered by the dashboard.
mail_safety_admin_send_default_form Form constructor for the send to default mail address form.
mail_safety_admin_send_default_form_submit Form submission handler for mail_safety_admin_send_default_form_submit.
mail_safety_admin_send_original_form Form constructor for the send to original mail address form.
mail_safety_admin_send_original_form_submit Form submission handler for mail_safety_admin_send_original_form.
mail_safety_admin_settings_form Form constructor for the Mail Safety settings form.
mail_safety_admin_view_mail Page callback: Displays a mail filtered by the dashboard.