You are here

function tfa_mail in Two-factor Authentication (TFA) 8

Implements hook_mail().

File

./tfa.module, line 46
Contains tfa.module.

Code

function tfa_mail($key, &$message, $params) {
  $token_service = \Drupal::token();
  $language_manager = \Drupal::languageManager();
  $variables = [
    'user' => $params['account'],
  ];
  $language = $language_manager
    ->getLanguage($params['account']
    ->getPreferredLangcode());
  $original_language = $language_manager
    ->getConfigOverrideLanguage();
  $language_manager
    ->setConfigOverrideLanguage($language);
  $tfa_config = \Drupal::config('tfa.settings');
  $token_options = [
    'langcode' => $message['langcode'],
    'clear' => TRUE,
  ];

  // Configuration mapping key matches the hook_mail() $key.
  $subject = $tfa_config
    ->get("mail.{$key}.subject");
  $subject = $token_service
    ->replace($subject, $variables, $token_options);
  $message['subject'] = PlainTextOutput::renderFromHtml($subject);
  $body = $tfa_config
    ->get("mail.{$key}.body");
  $message['body'][] = $token_service
    ->replace($body, $variables, $token_options);
  $language_manager
    ->setConfigOverrideLanguage($original_language);
}