You are here

function simplenews_mail in Simplenews 8

Same name and namespace in other branches
  1. 8.2 simplenews.module \simplenews_mail()
  2. 6.2 simplenews.module \simplenews_mail()
  3. 6 simplenews.module \simplenews_mail()
  4. 7.2 simplenews.module \simplenews_mail()
  5. 7 simplenews.module \simplenews_mail()
  6. 3.x simplenews.module \simplenews_mail()

Implements hook_mail().

Send simplenews mails using drupal mail API.

Parameters

$key: Must be one of: node, test, subscribe, unsubscribe.

$message: The message array, containing at least the following keys:

  • from
  • headers: An array containing at least a 'From' key.
  • language: The preferred message language.

array $params: The parameter array, containing the following keys:

  • simplenews_source: An implementation of SimplenewsSourceInterface which provides the necessary information to build the newsletter mail.

File

./simplenews.module, line 679
Simplenews node handling, sent email, newsletter block and general hooks

Code

function simplenews_mail($key, &$message, $params) {

  /** @var \Drupal\simplenews\Mail\MailBuilder $builder */
  $builder = \Drupal::service('simplenews.mail_builder');
  switch ($key) {
    case 'node':
    case 'test':
      $builder
        ->buildNewsletterMail($message, $params['simplenews_mail']);
      break;
    case 'subscribe':
      $builder
        ->buildSubscribeMail($message, $params);
      break;
    case 'subscribe_combined':
      $builder
        ->buildCombinedMail($message, $params);
      break;
    case 'unsubscribe':
      $builder
        ->buildUnsubscribeMail($message, $params);
      break;
  }

  // Debug message to check for outgoing emails messages.
  // Debug message of node and test emails is set in simplenews_mail_mail().
  $config = \Drupal::config('simplenews.settings');
  if ($config
    ->get('mail.debug') && $key != 'node' && $key != 'test') {
    \Drupal::logger('simplenews')
      ->debug('Outgoing email. Message type: %type<br />Subject: %subject<br />Recipient: %to', array(
      '%type' => $key,
      '%to' => $message['to'],
      '%subject' => $message['subject'],
    ));
  }
}