Implements hook_mail().
function user_mail($key, &$message, $params) {
$token_service = Drupal::token();
$langcode = $message['langcode'];
$variables = array(
'user' => $params['account'],
);
// Get configuration objects customized for the user specified in $params as
// this user is not necessarily the same as the one triggering the mail. This
// allows the configuration objects to be localized for the user's language if
// the locale module is enabled.
$user_config_context = config_context_enter('Drupal\\user\\UserConfigContext');
$user_config_context
->setAccount($params['account']);
$mail_config = config('user.mail');
// We do not sanitize the token replacement, since the output of this
// replacement is intended for an e-mail message, not a web browser.
$token_options = array(
'langcode' => $langcode,
'callback' => 'user_mail_tokens',
'sanitize' => FALSE,
'clear' => TRUE,
);
$message['subject'] .= $token_service
->replace($mail_config
->get($key . '.subject'), $variables, $token_options);
$message['body'][] = $token_service
->replace($mail_config
->get($key . '.body'), $variables, $token_options);
// Return the previous config context.
config_context_leave();
}