function email_example_mail_alter

Implements hook_mail_alter().

This function is not required to send an email using Drupal's mail system.

hook_mail_alter() provides an interface to 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 anyway. HTML-izing the outgoing email is one possibility.

Related topics

File

modules/email_example/email_example.module, line 79

Code

function email_example_mail_alter(&$message) {
  // For the purpose of this example, modify all the outgoing messages and
  // attach a site signature. The signature will be translated to the language
  // in which message was built.
  $options = [
    'langcode' => $message['langcode'],
  ];
  $signature = t("\n--\nMail altered by email_example module.", [], $options);
  if (is_array($message['body'])) {
    $message['body'][] = $signature;
  }
  else {
    // Some modules use the body as a string, erroneously.
    $message['body'] .= $signature;
  }
}