You are here

function pet_send_mail in Previewable email templates 8.4

Same name and namespace in other branches
  1. 8 pet.module \pet_send_mail()
  2. 6 pet.module \pet_send_mail()
  3. 7 pet.module \pet_send_mail()

Send tokenized email to a list of recipients.

Given a list of recipients, and an optional node id, perform token substitution and send an email to each. The node substitutions, if any, are the same in each email sent. The user tokens, if any are custom based on the account (if any) associated with each email.

Parameters

int $pet_id: The unique id of the PET template.

array $recipients: An array of at least one recipient in one of two formats: 1. a simple email address, in which case the uid is looked up 2. an array('mail' => <email address>, 'uid' => <uid>) in which case the uid is already available (more efficient).

array $options: An array of options as follows: nid - An optional node id for token substitutions. subject - An optional subject which if provided will override the subject in the PET. body - An optional body which if provided which will override the body in the PET. body_plain - An optional plain text body which if provided which will override the plain text body in the PET. from - An optional from email which if provided which will override the from in the PET (which in turn overrides the site default). reply_to - Optional cc - Optional cc emails which if provided which will override the cc's in the PET. bcc - Optional bcc emails which if provided which will override the bcc's in the PET.

1 call to pet_send_mail()
PetPreviewForm::submitForm in src/Form/PetPreviewForm.php
Form submission handler.

File

./pet.module, line 102
Previewable Email Template module.

Code

function pet_send_mail($pet_id, $recipients, $options) {
  $pet = pet_load($pet_id);
  if (!$pet) {
    \Drupal::logger('pet')
      ->error('Unable to load PET %pet_id.', [
      '%pet_id' => $pet_id,
    ]);
    return;
  }
  if (is_array($recipients) == FALSE) {
    $recipients = [
      $recipients,
    ];
  }
  else {
    if (is_array($recipients) == TRUE && count($recipients) < 1) {
      \Drupal::logger('pet')
        ->notice('At least one recipient must be provided for PET %pet_label.', [
        '%pet_label' => $pet
          ->label(),
      ]);
      return;
    }
  }

  // Override subject and body if necessary.
  if (isset($options['subject'])) {
    $pet
      ->setSubject($options['subject']);
  }
  if (isset($options['body'])) {
    $pet
      ->setMailbody($options['body']);
  }
  if (isset($options['body_plain'])) {
    $pet
      ->setMailbodyPlain($options['body_plain']);
  }

  // Resolve from address.
  if (pet_isset_or($options['from'])) {
    $from = $options['from'];
  }
  elseif ($pet
    ->getFromOverride()) {
    $from = $pet
      ->getFromOverride();
  }
  else {
    $from = \Drupal::config('system.site')
      ->get('mail');
  }

  // Store data in params in case a module wants to act on them somehow.
  $params = [
    'pet_from' => $from,
    'pet_recipients' => $recipients,
    'pet_nid' => pet_isset_or($options['nid']),
    'pet_cc' => pet_parse_mails(pet_isset_or($options['cc'])),
    'pet_bcc' => pet_parse_mails(pet_isset_or($options['bcc'])),
    'pet_reply_to' => pet_isset_or($options['reply_to']),
    'pet_options' => $options,
  ];

  // Array to hold status of messages send.
  $message_status = [];
  foreach ($recipients as $recipient) {
    if (is_array($recipient)) {
      $params['pet_to'] = $recipient['mail'];
      $params['pet_uid'] = $recipient['uid'];
    }
    else {

      // Strip leading uid for backward compatibility.
      $mail = preg_replace('/^[0-9]*\\|/', '', $recipient);
      $params['pet_to'] = $mail;
      $params['pet_uid'] = pet_lookup_uid($mail);
    }
    $message_status[$params['pet_to']] = pet_send_one_mail($pet, $params);
  }

  // Return message status.
  return $message_status;
}