You are here

smtp.module in SMTP Authentication Support 8

Same filename and directory in other branches
  1. 5 smtp.module
  2. 6 smtp.module
  3. 7.2 smtp.module
  4. 7 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 'system.mail.interface' default value ['default' => 'Drupal\Core\Mail\PhpMail']. This module uses ['default' => '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 'system.mail.interface'
 * default value ['default' => 'Drupal\Core\Mail\PhpMail'].
 * This module uses ['default' => 'SMTPMailSystem'].
 */
use Drupal\Core\Routing\RouteMatchInterface;

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

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

/**
 * Implements hook_queue_info().
 */
function smtp_queue_info() {
  $queues['smtp_send_queue'] = [
    'worker callback' => 'smtp_send_queue_runner',
    'cron' => [
      'time' => 60,
    ],
  ];
  return $queues;
}

/**
 * Smtp_send_queue queuer.
 */
function smtp_send_queue($mailerObj) {
  $queue = Drupal::queue('smtp_send_queue');
  $queue
    ->createItem($mailerObj);
}

/**
 * SMTP queue runner function.
 *
 * @param array $variables
 *   Variables to send trought runner.
 */
function smtp_send_queue_runner(array $variables) {
  _smtp_mailer_send($variables);
}

/**
 * Helper function to send mails.
 *
 * @param array $variables
 *   Variables to send email.
 *
 * @return bool
 *   True if email was sent. False otherwise.
 */
function _smtp_mailer_send(array $variables) {
  $smtp_config = \Drupal::config('smtp.settings');
  $mailer = $variables['mailer'];
  $to = $variables['to'];
  $from = $variables['from'];
  $logger = \Drupal::logger('smtp');

  // Let the people know what is going on.
  $logger
    ->info('Sending mail to: @to', [
    '@to' => $to,
  ]);

  // Try to send e-mail. If it fails, set watchdog entry.
  try {
    $mailer
      ->Send();
  } catch (Exception $e) {
    $logger
      ->error('Error sending e-mail from @from to @to: @error_message', [
      '@from' => $from,
      '@to' => $to,
      '@error_message' => $mailer->ErrorInfo,
    ]);
    return FALSE;
  }
  if (!$smtp_config
    ->get('smtp_keepalive')) {
    $mailer
      ->SmtpClose();
  }
  return TRUE;
}

Functions

Namesort descending Description
smtp_help Implements hook_help().
smtp_mail Implements hook_mail().
smtp_queue_info Implements hook_queue_info().
smtp_send_queue Smtp_send_queue queuer.
smtp_send_queue_runner SMTP queue runner function.
_smtp_mailer_send Helper function to send mails.