As you can understand from name itself it’s basically used to Alter an email created with drupal mail in D7/ MailManagerInterface->mail() in D8.  hook_mail_alter() allows modification of email messages which includes adding and/or changing message text, message fields, and message headers.

Email sent rather than drupal_mail() will not call hook_mail_alter(). All core modules use drupal_mail() & always a recommendation to use drupal_mail but it’s not mandatory.
 

Syntax: hook_mail_alter(&$message)

Parameters

$message: Array containing the message data. Below are the Keys in this array include:

  • 'id': The id of the message.
  • 'to': The address or addresses the message will be sent to. 
  • 'from': This address will be marked as being from, which is either a custom address or site-wide default mail address.
  • 'subject': Subject of the email to be sent. Subject should not contain any newline characters.
  • 'body': An array of strings containing the message text. message body created by concatenating individual array strings into a single text string.
  • '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.
  • 'send': Set to FALSE to abort sending this email message.

Why i am discussing on hook_mail_alter() ?

Recently i was doing one of the Drupal 8 project where client was looking for formatted HTML mail that also works for contact form. So whenever an anonymous user fill up and submit the contact form, It triggers an automated mail to the admin user. If you ever get a chance then just look at that weird formatted Email. Mail related to contact form is being triggered from core contact module in D8.

Just to alter the email format in Drupal 8 we have decided to write a custom module using hook_mail_alter() which alters the outgoing email message using drupal_mail().


Let’s start with code:

To implement hook_mail_alter() whether you can write your own custom module or put it in any of the custom module.

Sample Source code:

/**
 * Implements hook_mail_alter().
 */
function mymodule_mail_alter(&$message) {
  if (isset($message['id']) && $message['id'] == 'contact_page_mail') {
    /** @var \Drupal\contact\Entity\Message $contact_message */
    $contact_message = $message['params']['contact_message'];
    // Get sender's name.
    $sender_name = $contact_message->getSenderName();
    // Get sender's mail.
    $sender_mail = $contact_message->getSenderMail();
    // Get subject.
    $subject = $contact_message->getSubject();
    // Get message.
    $message_body = $contact_message->getMessage();
    // Get the value of "field_request" field.
    $request_value = $contact_message->get('field_request')->getValue();
  }
}

In Above source code  as you can see that we have added conditional statement so the changes will be impact only on specific mail.  

$message['params']['contact_message']: The message entity stored in the variable and it contains all the values from the contact form. Where contact_message is the type of entity. We can also fetch all the custom field value using get() method.

Source code: https://github.com/xaiwant/D8MailAlter

Conclusion: hook_mail_alter is solution to customize your mail body send through drupal mail. In this blog i have shared my idea of how can we send custom HTML formatted mail which triggers from Core drupal 8 contact form.  
 

 

comments powered by Disqus