hook_mail is not sending rendered HTML email I'm rendering HTML template for email

demo_test.module

use Drupal\Component\Utility\SafeMarkup;

function demo_test_theme() {   return [
    'contact_mail' =&gt; [
      'variables' =&gt; ['params' =&gt; NULL],
    ]   
  ]; 
}

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

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

  $mail = drupal_render($render_email);

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


contact_mail.html.twig



&lt;html&gt;
  &lt;head&gt;
    &lt;meta name="viewport" content="width=device-width"&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;style&gt;
      body {
        font-size: 20px;
        color: #ff0000;
        background-color: #FAFAFA;
      }
    &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;test&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;


Tried:

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


But still not working.
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.