You are here

smtp.module in SMTP Authentication Support 7

Same filename and directory in other branches
  1. 8 smtp.module
  2. 5 smtp.module
  3. 6 smtp.module
  4. 7.2 smtp.module

Enables Drupal to send e-mail directly to an SMTP server.

This module uses a customized extract of the PHPMailer library (originally by Brent R. Matzelle, now maintained by Codeworx Tech.) relicensed from LGPL to GPL, included as a part of the module.

Overriding mail handling in Drupal to make SMTP the default transport layer, requires to change the mail_system variable's default value array('default-system' => 'DefaultMailSystem'). This module uses array('default-system' => 'SmtpMailSystem').

File

smtp.module
View source
<?php

/**
 * @file
 * Enables Drupal to send e-mail directly to an SMTP server.
 *
 * This module uses a customized extract of the PHPMailer
 * library (originally by Brent R. Matzelle, now maintained
 *  by Codeworx Tech.) relicensed from LGPL to GPL, included
 * as a part of the module.
 *
 * Overriding mail handling in Drupal to make SMTP the default
 * transport layer, requires to change the mail_system variable's
 * default value array('default-system' => 'DefaultMailSystem').
 * This module uses array('default-system' => 'SmtpMailSystem').
 */

/**
 * SMTP logging -- logging is disabled
 */
define('SMTP_LOGGING_NONE', 0);

/**
 * SMTP logging -- all messages are logged
 */
define('SMTP_LOGGING_ALL', 1);

/**
 * SMTP logging -- only errors are logged
 */
define('SMTP_LOGGING_ERRORS', 2);

/**
 * Implements hook_help().
 */
function smtp_help($path, $arg) {
  switch ($path) {
    case 'admin/help#smtp':
      return t('Allow for site emails to be sent through an SMTP server of your choice.');
  }
}

/**
 * Implements hook_menu().
 */
function smtp_menu() {
  $items['admin/config/system/smtp'] = array(
    'title' => 'SMTP Authentication Support',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'smtp_admin_settings',
    ),
    'access arguments' => array(
      'administer smtp module',
    ),
    'description' => 'Allow for site emails to be sent through an SMTP server of your choice.',
    'file' => 'smtp.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function smtp_permission() {
  return array(
    'administer smtp module' => array(
      'title' => t('Administer SMTP Authentication Support module'),
      'description' => t('Perform administration tasks for SMTP Authentication Support module.'),
    ),
  );
}

/**
 * Implements hook_mail().
 */
function smtp_mail($key, &$message, $params) {
  if ($key == 'smtp-test') {
    $message['subject'] = $params['subject'];
    $message['body'] = $params['body'];
  }
}

/**
 * Implements hook_cron_queue_info().
 */
function smtp_cron_queue_info() {
  $queues['smtp_send_queue'] = array(
    'worker callback' => 'smtp_send_queue_runner',
    'time' => 60,
  );
  $queues['smtp_failure_queue'] = array(
    'worker callback' => 'smtp_failure_queue_runner',
    'time' => 30,
  );
  return $queues;
}

/**
 * Function to queue a message into the Drupal system queue.
 *
 * @param object
 */
function smtp_send_queue($mailer_object) {
  $queue = DrupalQueue::get('smtp_send_queue');
  $queue
    ->createItem($mailer_object);
}

/**
 * Queue runner function defined in hook_cron_queue_info() above.
 *
 * @param $message
 *
 * @return bool
 */
function smtp_send_queue_runner($message) {
  $logging = variable_get('smtp_debugging', SMTP_LOGGING_ERRORS);

  // Legacy for mails queued before 7.x-v1.3
  // What was passed to the runner used to be a PHPMailer object, not a message array.
  if (!empty($message['mailer']) && is_object($message['mailer'])) {
    if (!$message['mailer']
      ->Send()) {
      if ($logging == SMTP_LOGGING_ALL || $logging == SMTP_LOGGING_ERRORS) {
        watchdog('smtp', 'Error sending e-mail from @from to @to, will retry on cron run : !error_message.', array(
          '@from' => $message['from'],
          '@to' => $message['to'],
          '!error_message' => $message['mailer']->ErrorInfo,
        ), WATCHDOG_ERROR);
      }
    }
    return;
  }

  // Let the people know what is going on.
  if ($logging == SMTP_LOGGING_ALL) {
    watchdog('smtp', 'Sending mail to: @to', array(
      '@to' => $message['to'],
    ));
  }

  // Send the message.
  $mail_system = new SmtpMailSystem();
  $mail_system
    ->mailWithoutQueue($message);
}

/**
 * Store failed messages for later.
 *
 * @param array $new_message
 *
 * @return array
 *   All messages that have been saved.
 */
function smtp_failed_messages($message) {
  $queue = DrupalQueue::get('smtp_failure_queue');
  $queue
    ->createItem($message);
}

/**
 * Queue runner for smtp_failure_queue.
 *
 * @param $message
 *   A drupal_mail-formatted message.
 */
function smtp_failure_queue_runner($message) {
  $queue = DrupalQueue::get('smtp_send_queue');
  $queue
    ->createItem($message);
}

Functions

Namesort descending Description
smtp_cron_queue_info Implements hook_cron_queue_info().
smtp_failed_messages Store failed messages for later.
smtp_failure_queue_runner Queue runner for smtp_failure_queue.
smtp_help Implements hook_help().
smtp_mail Implements hook_mail().
smtp_menu Implements hook_menu().
smtp_permission Implements hook_permission().
smtp_send_queue Function to queue a message into the Drupal system queue.
smtp_send_queue_runner Queue runner function defined in hook_cron_queue_info() above.

Constants

Namesort descending Description
SMTP_LOGGING_ALL SMTP logging -- all messages are logged
SMTP_LOGGING_ERRORS SMTP logging -- only errors are logged
SMTP_LOGGING_NONE SMTP logging -- logging is disabled