Same name and namespace in other branches
  1. 6.x developer/hooks/core.php \hook_mail()
  2. 7.x modules/system/system.api.php \hook_mail()
  3. 8.9.x core/core.api.php \hook_mail()
  4. 9 core/core.api.php \hook_mail()

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.

Parameters

$key: An identifier of the mail.

$message: An array to be filled in. Elements in this array include:

  • id: An ID to identify the mail sent. Look at module source code or MailManagerInterface->mail() for possible id values.
  • to: The address or addresses the message will be sent to. The formatting of this string must comply with RFC 2822.
  • subject: Subject of the email to be sent. This must not contain any newline characters, or the mail may not be sent properly. MailManagerInterface->mail() sets this to an empty string when the hook is invoked.
  • body: An array of lines containing the message to be sent. Drupal will format the correct line endings for you. MailManagerInterface->mail() sets this to an empty array when the hook is invoked. The array may contain either strings or objects implementing \Drupal\Component\Render\MarkupInterface.
  • from: The address the message will be marked as being from, which is set by MailManagerInterface->mail() to either a custom address or the site-wide default email address when the hook is invoked.
  • headers: Associative array containing mail headers, such as From, Sender, MIME-Version, Content-Type, etc. MailManagerInterface->mail() pre-fills several headers in this array.

$params: An array of parameters supplied by the caller of MailManagerInterface->mail().

See also

\Drupal\Core\Mail\MailManagerInterface::mail()

Related topics

5 functions implement hook_mail()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

contact_mail in core/modules/contact/contact.module
Implements hook_mail().
mail_html_test_mail in core/modules/system/tests/modules/mail_html_test/mail_html_test.module
Implements hook_mail().
update_mail in core/modules/update/update.module
Implements hook_mail().
user_load_by_mail in core/modules/user/user.module
Fetches a user object by email address.
user_mail in core/modules/user/user.module
Implements hook_mail().
1 invocation of hook_mail()
MailManager::doMail in core/lib/Drupal/Core/Mail/MailManager.php
Composes and optionally sends an email message.

File

core/core.api.php, line 2098
Documentation landing page and topics, plus core library hooks.

Code

function hook_mail($key, &$message, $params) {
  $account = $params['account'];
  $context = $params['context'];
  $variables = [
    '%site_name' => \Drupal::config('system.site')
      ->get('name'),
    '%username' => $account
      ->getDisplayName(),
  ];
  if ($context['hook'] == 'taxonomy') {
    $entity = $params['entity'];
    $vocabulary = Vocabulary::load($entity
      ->id());
    $variables += [
      '%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 += [
      '%uid' => $node
        ->getOwnerId(),
      '%url' => $node
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString(),
      '%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([
    "\r",
    "\n",
  ], '', $subject);
  $message['body'][] = MailFormatHelper::htmlToText($body);
}