hook_mail

Versions
6 – 7
hook_mail($key, &$message, $params)

Prepare a message based on parameters; called from drupal_mail().

Parameters

$key An identifier of the mail.

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

  • 'id': An id to identify the mail sent. Look at module source code or drupal_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 e-mail to be sent. This must not contain any newline characters, or the mail may not be sent properly. drupal_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. drupal_mail() sets this to an empty array when the hook is invoked.
  • 'from': The address the message will be marked as being from, which is set by drupal_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. drupal_mail() pre-fills several headers in this array.

$params An arbitrary array of parameters set by the caller to drupal_mail.

Code

documentation/hooks/core.php, line 2299

<?php
function hook_mail($key, &$message, $params) {
  $account = $params['account'];
  $context = $params['context'];
  $variables = array(
    '%site_name' => variable_get('site_name', 'Drupal'),
    '%username' => $account->name,
  );
  if ($context['hook'] == 'taxonomy') {
    $object = $params['object'];
    $vocabulary = taxonomy_vocabulary_load($object->vid);
    $variables += array(
      '%term_name' => $object->name,
      '%term_description' => $object->description,
      '%term_id' => $object->tid,
      '%vocabulary_name' => $vocabulary->name,
      '%vocabulary_description' => $vocabulary->description,
      '%vocabulary_id' => $vocabulary->vid,
    );
  }

  // Node-based variable translation is only available if we have a node.
  if (isset($params['node'])) {
    $node = $params['node'];
    $variables += array(
      '%uid' => $node->uid,
      '%node_url' => url('node/'. $node->nid, array('absolute' => TRUE)),
      '%node_type' => node_get_types('name', $node),
      '%title' => $node->title,
      '%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'][] = drupal_html_to_text($body);
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters (without spaces) shown in the image.