You are here

function email_example_mail_alter in Examples for Developers 8

Same name and namespace in other branches
  1. 6 email_example/email_example.module \email_example_mail_alter()
  2. 7 email_example/email_example.module \email_example_mail_alter()
  3. 3.x modules/email_example/email_example.module \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

email_example/email_example.module, line 79
Example of how to use Drupal's mail API.

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;
  }
}