Drupal 7 : Send HTML email by hook_mail_alter with sample Code

In Drupal we can't send HTML mails, It won’t support HTML content rendering. In Drupal mail Body considered as normal text. So we need to alter the mail content mime type to HTML for HTML tags support.

Syntax: 

/**
 * Implements hook_mail_alter().
 */
function MODULE_NAME_mail_alter(&$message) {
  if (($message['module'] == ‘MODULE_NAME’) && ($message['key'] = ‘MAIL_KEY’)) {
    $message['headers'] = array(
      'MIME-Version' => '1.0',
      'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
      'Content-Transfer-Encoding' => '8Bit',
      'X-Mailer' => 'Drupal',
    );
  }
}

Example: 

/**
 * Implements hook_mail_alter().
 */
function send_mails_mail_alter(&$message) {
  if (($message['module'] == 'send_mails') && ($message['key'] = 'send_mails_send')) {
    $message['headers'] = array(
      'MIME-Version' => '1.0',
      'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
      'Content-Transfer-Encoding' => '8Bit',
      'X-Mailer' => 'Drupal',
      'Return-Path' => $message['from'],
    );
  }
}
Category: