hook_mail_alter(&$message)
Alter an email message created with MailManagerInterface->mail().
hook_mail_alter() allows modification of email messages created and sent with MailManagerInterface->mail(). Usage examples include adding and/or changing message text, message fields, and message headers.
Email messages sent using functions other than MailManagerInterface->mail() will not invoke hook_mail_alter(). For example, a contributed module directly calling the MailInterface->mail() or PHP mail() function will not invoke this hook. All core modules use MailManagerInterface->mail() for messaging, it is best practice but not mandatory in contributed modules.
$message: An array containing the message data. Keys in this array include:
\Drupal\Core\Mail\MailManagerInterface::mail()
function hook_mail_alter(&$message) { if ($message['id'] == 'modulename_messagekey') { if (!example_notifications_optin($message['to'], $message['id'])) { // If the recipient has opted to not receive such messages, cancel // sending. $message['send'] = FALSE; return; } $message['body'][] = "--\nMail sent out from " . \Drupal::config('system.site')->get('name'); } }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!core.api.php/function/hook_mail_alter/8.1.x