You are here

function pet_mail in Previewable email templates 8.3

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

Implements hook_mail().

To customize, e.g. to change the content type to text/html etc, you can use hook_mail_alter() in one of your modules.

See also

https://api.drupal.org/api/drupal/core%21core.api.php/function/hook_mail...

https://api.drupal.org/api/drupal/core%21core.api.php/function/hook_mail...

File

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

Code

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

  /** @var \Drupal\pet\Entity\PetInterface $pet */
  $pet = $params['pet'];
  $context = $params['context'];
  $substitutions = PetHelper::getSubstitutions($context);
  $token = \Drupal::token();
  if ($cc = $pet
    ->getCc()) {
    $message['headers']['Cc'] = $cc;
  }
  if ($bcc = $pet
    ->getBcc()) {
    $message['headers']['Bcc'] = $bcc;
  }

  // Make sure the 'From' header uses the site name in pet's language.
  $language_manager = \Drupal::service('language_manager');
  $langcode = $pet
    ->get('langcode')
    ->getValue()[0]['value'];
  $language = $language_manager
    ->getLanguage($langcode);
  $original_language = $language_manager
    ->getConfigOverrideLanguage();
  $language_manager
    ->setConfigOverrideLanguage($language);
  $site_config = \Drupal::config('system.site');
  $site_mail = $site_config
    ->get('mail');
  $message['headers']['From'] = $site_config
    ->get('name') . ' <' . $site_mail . '>';
  $language_manager
    ->setConfigOverrideLanguage($original_language);

  // Set subject and body.
  $message['subject'] = $token
    ->replace($pet
    ->getSubject(), $substitutions, [
    'clear' => TRUE,
  ]);

  // MimeMail integration.
  if (PetHelper::hasMimeMail()) {
    $message['body'][] = $token
      ->replace($pet
      ->getBodyHtml(), $substitutions, [
      'clear' => TRUE,
    ]);
    $mail_body = trim($pet
      ->getBody());
    if ($mail_body) {
      $message['params']['plaintext'] = $token
        ->replace($mail_body, $substitutions, [
        'clear' => TRUE,
      ]);
    }
    $message['params']['plain'] = $pet
      ->getSendPlain();
  }
  else {
    $message['body'][] = $token
      ->replace($pet
      ->getBody(), $substitutions, [
      'clear' => TRUE,
    ]);
  }
}