Send HTML mails with Drupal 8

At the time of this article in October 2015, the HTML swiftmailer module version for Drupal 8 was not released. Thus the below solution is not relevant anymore but may help for configuration or setup. We also have a new post about installation of swiftmailer

Here is a possible solution for sending HTML mails with Drupal 8 specifically when creating custom modules.

The solution below uses Swiftmailer library.

The main module compatible with Drupal 8 can be downloaded here.

This version needs some corrections if it is used with a recent Drupal 8 release. In this case, these are the lines changed:

  • SafeMarkup is generating an error. L 79 of \src\Plugin\Mail\SwiftMailer.php changed to $message['body'] = Xss::filterAdmin(implode($line_endings, $message['body'])); and add "use Drupal\Component\Utility\Xss;";
  • L 151 add "require_once(DRUPAL_ROOT . '/libraries/swiftmailer/lib/swift_required.php');"


The second change is necessary to include another library used by SwiftMailer but the path can be different. This library is here.

The other module used to manage the mail system is here.

Note: most of those modules are under development process for Drupal 8 versions.

Once the module are installed, you can set Drupal to use swiftmailer as default mail system to send HTML mail.


Mail system configuration ex.

Mail system


In Swiftmailer module, you can select HTML format as in the ex. below:



Now in your module, you can implement hook_mail() function to send email that will be formatted as HTML.

Call mail service ex.:


Hook_mail() ex.:

 * Implementation hook_mail().
function MODULE_mail($key, &$message, $params) {

    switch($key) {
      case 'KEY':
        $message['subject'] = $params['subject'];
        $message['body'][] = $params['body'];


If you have implemented this solution or other better solutions, please feel free to give your feedback.

Thank you.