0

This question already has an answer here:

I'm rendering HTML template for email

demo_test.module

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;   
  break; 
}

contact_mail.html.twig

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

Tried:

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

But still not working.

marked as duplicate by kiamlaluno Jan 23 '18 at 17:31

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
0

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.

0

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.