I'm rendering HTML template for email


use Drupal\Component\Utility\SafeMarkup;

function demo_test_theme() {   return [
    'contact_mail' => [
      'variables' => ['params' => NULL],

function demo_test_mail($key, &$message, $params) {

  $render_email = [
    '#theme' => 'contact_email',
    '#params' => $params,   ];

  $mail = drupal_render($render_email);

  $message['body'][] = $mail;   


    <meta name="viewport" content="width=device-width">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      body {
        font-size: 20px;
        color: #ff0000;
        background-color: #FAFAFA;


$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';

But still not working.

  • You could use mail system module drupal.org/project/mailsystem – Kstack Nov 23 '17 at 3:13
  • Hi @Kstack need to use that module for this? Can't achieve via code changes? – Mike Nov 23 '17 at 3:51
  • @Mike Drupals default mail backend will convert any HTML to plain text. Look at PhpMail::format. You could achieve the desired result by writing your own mail system but that would require quite a lot of effort, especially if you want to send as both HTML and Plain text. – Marcus Aschan Nov 23 '17 at 11:33

You can try with Swift Mailer module.

One of the feature includes sending HTML (MIME) e-mails.

The module also lets you theme e-mails so that they reflect the general look and feel of your site and at the same time appear correctly in various e-mail clients. Images required by the theme can be attached to the e-mail and displayed inline.


I have done the same what you done above. Using hook_mail_alter function will not work for this case either.

Better is to use Mime Mail module to achieve this HTML Format.

It permits users to receive HTML email and can be used by other modules. The mail functionality accepts an HTML message body, mime-endcodes it and sends it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.