You are here

function pet_send_one_mail in Previewable email templates 8

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

Send one email, with token substitution.

This may be called directly from other modules.

Parameters

object $pet: The loaded PET object to use for the email.

array $params: Array of parameters used when constructing the email. pet_from (required) - a valid sender email address pet_to (required) - a valid recipient email address pet_recipients (optional) - if called from pet_send_mail() will contain the full recipient list pet_uid (optional) - if provided, a valid user id for 'user' type token substitution pet_nid (optional) - if provided, a valid node id for 'node' type token substitution pet_reply_to (optional) The $params array may also contain data passed in by other modules. One use of this is for token substitution.

See also

hook_pet_substitutions_alter()

2 calls to pet_send_one_mail()
pet_action_send_pet in ./pet.rules.inc
Callback for eponymous rules action.
pet_send_mail in ./pet.module
Send tokenized email to a list of recipients.

File

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

Code

function pet_send_one_mail(Pet $pet, $params) {
  $pet_logging = \Drupal::config('pet.settings')
    ->get('pet_logging');
  $messenger = \Drupal::messenger();
  if (!pet_is_valid($pet)) {
    if ($pet_logging < 2) {
      \Drupal::logger('pet')
        ->error('Invalid PET object in pet_send_one_mail().');
    }
    else {
      $messenger
        ->addMessage(t('Invalid PET object in pet_send_one_mail().'), 'error');
    }
    return;
  }
  $pet_title = $pet
    ->label();
  if (empty($params['pet_from'])) {
    if ($pet_logging < 2) {
      \Drupal::logger('pet')
        ->error('Missing sender email address in pet_send_one_mail() for PET \'%pet_title\'.', [
        '%pet_title' => $pet_title,
      ]);
    }
    else {
      $messenger
        ->addMessage(t('Missing sender email address in pet_send_one_mail() for PET \'%pet_title\'.', [
        '%pet_title' => $pet_title,
      ]), 'error');
    }
    return;
  }
  if (empty($params['pet_to'])) {
    if ($pet_logging < 2) {
      \Drupal::logger('pet')
        ->error('Missing recipient email address in pet_send_one_mail() for PET \'%pet_title\'.', [
        '%pet_title' => $pet_title,
      ]);
    }
    else {
      $messenger
        ->addMessage(t('Missing recipient email address in pet_send_one_mail() for PET \'%pet_title\'.', [
        '%pet_title' => $pet_title,
      ]), 'error');
    }
    return;
  }
  if (isset($params['pet_reply_to'])) {
    $message['headers']['Reply-To'] = $params['pet_reply_to'];
  }
  $params['pet'] = $pet;
  $substitutions = pet_substitutions($pet, $params);
  $token = \Drupal::token();
  $params['subject'] = $token
    ->replace($pet
    ->getSubject(), $substitutions, [
    'clear' => TRUE,
  ]);
  $params['body'] = $token
    ->replace($pet
    ->getMailbody(), $substitutions, [
    'clear' => TRUE,
  ]);

  // Provided for Mime Mail module; alternate text-only form for multipart MIME.
  $mail_body_plain = trim($pet
    ->getMailbodyPlain());
  if (!empty($mail_body_plain)) {
    $params['plaintext'] = $token
      ->replace($pet
      ->getMailbodyPlain(), $substitutions, [
      'clear' => TRUE,
    ]);
  }

  // Provided for Mime Mail module; send ONLY plain text.
  $params['plain'] = $pet
    ->getSendPlain();
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();
  $langcode = $language_interface
    ->getId();
  $message = \Drupal::service('plugin.manager.mail')
    ->mail('pet', $pet
    ->id(), $params['pet_to'], $langcode, $params, $params['pet_from']);
  if ($message['send'] && $pet_logging == 0) {
    \Drupal::logger('pet')
      ->notice('Successfully sent email to %recipient', [
      '%recipient' => $params['pet_to'],
    ]);
  }

  // Return message, useful for show custom message, based email send status.
  return $message;
}