function hook_mail_alter

You are here

7 system.api.php hook_mail_alter(&$message)
5 core.php hook_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers)
6 core.php hook_mail_alter(&$message)
8 system.api.php hook_mail_alter(&$message)

Alter any aspect of email sent by Drupal. You can use this hook to add a common site footer to all outgoing email, add extra header fields, and/or modify the email in any way. HTML-izing the outgoing email is one possibility. See also drupal_mail().

Parameters

$message: A structured array containing the message to be altered. 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 mail 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.
  • 'body' An array of lines containing the message to be sent. Drupal will format the correct line endings for you.
  • 'from' The address the message will be marked as being from, which is either a custom address or the site-wide default email address.
  • 'headers' Associative array containing mail headers, such as From, Sender, MIME-Version, Content-Type, etc.
  • 'params' An array of optional parameters supplied by the caller of drupal_mail() that is used to build the message before hook_mail_alter() is invoked.
  • language' The language object used to build the message before hook_mail_alter() is invoked.

Related topics

1 invocation of hook_mail_alter()
drupal_mail in includes/mail.inc
Compose and optionally send an e-mail message.

File

developer/hooks/core.php, line 1106
These are the hooks that are invoked by the Drupal core.

Code

function hook_mail_alter(&$message) {
  if ($message['id'] == 'modulename_messagekey') {
    $message['body'][] = "--\nMail sent out from " . variable_get('sitename', t('Drupal'));
  }
}

Comments

<?php
function my_module_mail_alter(&$message) {
switch (
$message['id']) {
case
"user_register_admin_created":
case
"user_register_no_approval_required":
case
"user_register_pending_approval":
case
"user_status_blocked":
case
"user_status_deleted":
break;
case
"user_password_reset":
$message['headers']['From'] = $message['from'] = 'My Site Password Reset <'. $message['from'] .'>';
break;
}
}
?>

You can also replace $message['from'] with a hard-coded alternate email address for each case if needed.