You are here

simple_mail.module in Simple Mail 8

Same filename and directory in other branches
  1. 7 simple_mail.module
  2. 2.0.x simple_mail.module

Simple Mail module.

This module provides simple email delivery functionality. The two primary functions are simple_mail_send(), which sends emails directly, and simple_mail_queue(), which queues emails in the simple_mail_queue using Drupal's Queue API, and then sends emails in batches during cron runs.

File

simple_mail.module
View source
<?php

/**
 * @file
 * Simple Mail module.
 *
 * This module provides simple email delivery functionality. The two primary
 * functions are simple_mail_send(), which sends emails directly, and
 * simple_mail_queue(), which queues emails in the simple_mail_queue using
 * Drupal's Queue API, and then sends emails in batches during cron runs.
 */

/**
 * Implements hook_mail().
 *
 * Send HTML emails.
 *
 * @todo - Also add plain text alternative.
 */
function simple_mail_mail($key, &$message, $params) {
  switch ($key) {
    case 'simple_mail':
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
      $message['subject'] = $params['subject'];
      $message['body'][] = $params['body'];
      break;
  }
}

/**
 * Implements hook_mail_alter().
 *
 * Override email 'to' address, if configured.
 */
function simple_mail_mail_alter(&$message) {

  // Re-route emails when override email variable is set.
  $override_email = \Drupal::config('simple_mail.settings')
    ->get('override');
  if (!empty($override_email)) {
    $message['to'] = $override_email;
  }
}

/**
 * Easy-to-use email sending function.
 *
 * @param (string) $from
 *   Email sender. Defaults to the system email.
 * @param (string) $to
 *   Email receipient.
 * @param (string) $subject
 *   Email subject.
 * @param (string) $body
 *   Email message body (can be HTML or plaintext).
 *
 * @return (array)
 *   Message array structure, as returned by drupal_mail(). Check for 'result' =
 *   TRUE to verify that the message was sent (at least, according to PHP).
 */
function simple_mail_send($from, $to, $subject, $body) {
  if (empty($from)) {
    $from = \Drupal::config('system.site')
      ->get('mail');
  }
  $params['subject'] = $subject;
  $params['body'] = $body;
  $langcode = \Drupal::languageManager()
    ->getDefaultLanguage();

  // Send email with drupal_mail.
  return \Drupal::service('plugin.manager.mail')
    ->mail('simple_mail', 'simple_mail', $to, $langcode, $params, $from);
}

/**
 * Queue an email to be sent via the Queue API during cron runs.
 *
 * Queues an email for sending through Drupal's Queue API, rather than sending
 * immediately. This function is basically a queued version of the nearly-
 * identical function simple_mail_send_email(). Use this function when
 * sending batches of email so the page request is not delayed while waiting
 * for individual messages to be sent.
 *
 * @param (string) $from
 *   Email sender. Defaults to the system email.
 * @param (string) $to
 *   Email receipient.
 * @param (string) $subject
 *   Email subject.
 * @param (string) $body
 *   Email message body (can be HTML or plaintext).
 *
 * @return bool
 *   TRUE if the email was queued, FALSE otherwise.
 *
 * @see simple_mail_send()
 */
function simple_mail_queue($from, $to, $subject, $body) {
  $queue_enabled = \Drupal::config('simple_mail.settings')
    ->get('queue_enabled');

  // If the queue is disabled, return FALSE.
  if (!$queue_enabled) {
    return FALSE;
  }

  // Queue the email.
  $queue = Drupal::queue('simple_mail_queue', TRUE);
  $item = array(
    'from' => $from,
    'to' => $to,
    'subject' => $subject,
    'body' => $body,
  );
  $queue
    ->createItem($item);
  return TRUE;
}

Functions

Namesort descending Description
simple_mail_mail Implements hook_mail().
simple_mail_mail_alter Implements hook_mail_alter().
simple_mail_queue Queue an email to be sent via the Queue API during cron runs.
simple_mail_send Easy-to-use email sending function.