This is my code to sending email

   function hook_mail($key, &$message, $params) {
     $options = array(
    'langcode' => $message['langcode'],

  switch ($key) {
    case 'create_article':
      $message['from'] = \Drupal::config('system.site')->get('mail');
      // $message['headers']['Cc'] = $params['cc'];

      $message['subject'] = 'SUBJECT';
      $message['body'][] = Markup::create($params['message']);

Mails are getting without any issue.But some of the HTML tags are not supporting Eg: HTML bold,italics.... I have also configured SMTP authentication.


You can't send HTML email with Drupal 8 "out of the box."

To send HTML email, you can use the Swift Mailer module, which has the following features:

  • Sending e-mails directly through a SMTP server of your choice, a locally installed MTA agent such as sendmail or the mail functionality provided by PHP.
  • Sending HTML (MIME) e-mails.
  • Adding file attachments to e-mails.
  • Adding inline images to e-mails.

The Commerce 2 documentation includes a tutorial on how to install and configure the Swift Mailer module.

  • But HTML <br> tag is working. – user49951 Dec 7 '17 at 4:17

In D7 you need to install Mime Mail and in your code add:

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

Hopefully it's still the same on D8, give it a try.

  • 1
    I also used Mime Mail for Drupal 7, but I think Swift Mailer has more development momentum in Drupal 8. For example, Swift Mailer is the recommendation for use with Commerce 2: docs.drupalcommerce.org/commerce2/developer-guide/core/… – Patrick Kenny Dec 7 '17 at 9:10
  • @PatrickKenny thanks for the info, I think he'll probably still need to add the code I posted above when sending programmatically with Swift Mailer. – No Sssweat Dec 7 '17 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy