hook_mail($key, &$message, $params)
Prepares a message based on parameters;
This hook is called from MailManagerInterface->mail(). Note that hook_mail(), unlike hook_mail_alter(), is only called on the $module argument to MailManagerInterface->mail(), not all modules.
$key: An identifier of the mail.
$message: An array to be filled in. Elements in this array include:
$params: An array of parameters supplied by the caller of MailManagerInterface->mail().
\Drupal\Core\Mail\MailManagerInterface->mail()
function hook_mail($key, &$message, $params) { $account = $params['account']; $context = $params['context']; $variables = array( '%site_name' => \Drupal::config('system.site')->get('name'), '%username' => $account->getDisplayName(), ); if ($context['hook'] == 'taxonomy') { $entity = $params['entity']; $vocabulary = Vocabulary::load($entity->id()); $variables += array( '%term_name' => $entity->name, '%term_description' => $entity->description, '%term_id' => $entity->id(), '%vocabulary_name' => $vocabulary->label(), '%vocabulary_description' => $vocabulary->getDescription(), '%vocabulary_id' => $vocabulary->id(), ); } // Node-based variable translation is only available if we have a node. if (isset($params['node'])) { /** @var \Drupal\node\NodeInterface $node */ $node = $params['node']; $variables += array( '%uid' => $node->getOwnerId(), '%url' => $node->url('canonical', array('absolute' => TRUE)), '%node_type' => node_get_type_label($node), '%title' => $node->getTitle(), '%teaser' => $node->teaser, '%body' => $node->body, ); } $subject = strtr($context['subject'], $variables); $body = strtr($context['message'], $variables); $message['subject'] .= str_replace(array("\r", "\n"), '', $subject); $message['body'][] = MailFormatHelper::htmlToText($body); }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!core.api.php/function/hook_mail/8.1.x