4

At Account settings page (/admin/config/people/accounts) I can edit system mails Drupal is sending (Welcome, Account activated, Account blocked and so on).

I also installed and configured Swift Mailer module. At Message settings page (admin/config/swiftmailer/messages) I selected HTML for message format.

Then I edit some message template and enter html content there like:

<!DOCTYPE html>
<html>
  <head>
    <title>Your account at [site:name] has been activated.</title>
  </head>
  <body>
[user:display-name],

Your account at [site:name] has been activated.

You may now log in by clicking this link or copying and pasting it into your browser:

[user:one-time-login-url]

This link can only be used once to log in and will lead you to a page where you can set your password.

After setting your password, you will be able to log in at [site:login-url] in the future using:

username: [user:account-name]
password: Your password

--  [site:name] team
  </body>
</html>

However in mail I receive I still got Content-Type: text/plain; and I see it as plain text.

At that Swift Mail Message settings page it says:

"You can set the default message format which should be applied to e-mail messages."

But I don't see that option anywhere? How to change this default message format (to "text/html")?

An update:

I found out that if I uncheck "Respect provided e-mail format." on SwiftMails Message settings page I do get text/html content type in mail header. However all the tags I enter in mail template are stripped out and new rows are automatically converted to paragraphs? I expected that there will be "HTML email" text format at Text formats page (/admin/config/content/formats) where I can set filters, but there aren't any.

0

2 Answers 2

0
+50

Swiftmailer works in combination with the (contrib) mailsystem module.

Check the settings here: /admin/config/system/mailsystem

Make sure Swift Mailer is set as global formatter (on top).

You also should select the theme you wish to render your HTML emails.

Or, under module specific configuration, you can add the user module to explicitly use Swift Mailer (as formatter). Just leave the key field empty to use all emails from the specific module. Then other emails are not affected ..

UPDATE: Swiftmailer can will let you choose form any text format you have defined, you can create a new one yourself. But, this will not let you output any meaningful HTML (see https://www.drupal.org/project/swiftmailer/issues/3122389) as the textformat needs "Show HTML as plain text" filter activated. Work with twig templates (see Why does swiftmailer doesn't use my HTML template?) you can probably make one per email like swiftmailer--password_reset.html.twig (see https://api.drupal.org/api/drupal/core%21modules%21user%21user.module/function/_user_mail_notify/8.2.x for all mail keys). Still if you want to have custom HTML "in-between", you need to hook_mail and add the information to be used in your template (using #theme render array entries).

3
  • Yes, Swift Mailer is set as global formatter and I don't have module specific formatters. And mail is sent as HTML now (check my update to the question), used template file is: modules/contrib/swiftmailer/templates/swiftmailer.html.twig Problem is that from inner mail content (edited at /admin/config/people/accounts) all html tags are stripped and new rows are replaced with paragraphs. How to use raw content I entered there, to avoid filtering?
    – MilanG
    Aug 12, 2020 at 6:47
  • 1
    It's not possible due to security devise. Drupal does not trust user input! Please see my update.
    – rémy
    Aug 12, 2020 at 21:42
  • @kiamlaluno why did you revert my change? Apr 16, 2021 at 17:04
3

My edit to the accepted answer got reverted, so I'll contribute it as a separate answer, for two reasons: 1) several grammatical and formatting errors in the accepted answer make it hard to read and understand and 2) the comments to the accepted answer (and the "Update" section) explain why the proposed solutions do not work. This answer adds an additional working solution, credited to the author of said approach, at the very bottom of the post.

The Swift Mailer Drupal module works in combination with the (contrib) Mail System module.

1. Setup Mail System

  1. Check the settings here: /admin/config/system/mailsystem

  2. Make sure Swift Mailer is set as the Default Mail System > Formatter.

  3. Select the theme you wish to render your HTML emails — typically your front-end theme — where you have the ability implement a custom swiftmailer.html.twig template with any wrapper HTML (eg email header and footer text that appears on all templates) and sub-templates.

  4. Alternatively, under Module specific configuration, add the user module to explicitly use Swift Mailer as the Formatter for the User module. Hint: Leave the key field empty to affect all emails from the specific module. This way, other system or contrib module emails are not affected.

2. Configure Swift Mailer to use HTML for Core User Templates

  1. Swift Mailer lets you choose the HTML format to use from a list of Text Formats you have defined in Drupal on the Message settings page: /admin/config/swiftmailer/messages.

    So you could theoretically create a new Text Format yourself, such as "HTML Email", but this setting does not let you output any meaningful HTML because the text format needs the "Display any HTML as plain text" filter activated (see Issue #3122389: Bugs with format conversion).

    In short, this option in the UI is a red herring and is only intended for upgrading Plain Text to a basic HTML format, by converting line breaks to <br>s and URLs like https://example.com to clickable links like <a href="https://example.com">https://example.com</a>.

  2. Alternatively, you can create a custom Twig template per module, like swiftmailer--user.html.twig, or per email key, like swiftmailer--user--password-reset.html.twig and replace {{ body }} with your HTML message, however you will not have access to the same Drupal Tokens that are available to the user account email templates in Drupal's admin UI. For a list of all user mail keys, see the $op parameter of _user_mail_notify().

  3. Therefore, the currently known best approach is to enter your custom HTML into the Drupal administrative UI, then use hook_mail_alter to add text/html to the Headers and convert the text to a valid Markup object.

use Drupal\Component\Render\FormattableMarkup;
/**
 * Implements hook_mail_alter().
 */
function MYMODULE_mail_alter(&$message) {
  switch ($message['key']) {

    // User module templates from admin/config/people/accounts
    case 'cancel_confirm':
    case 'password_reset':
    case 'register_admin_created':
    case 'register_no_approval_required':
    case 'register_pending_approval':
    case 'register_pending_approval_admin':
    case 'status_activated':
    case 'status_blocked':
    case 'status_canceled':

    // Contact module templates from admin/structure/contact/manage/sitewide
    case 'page_mail':
    case 'page_copy':
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
      foreach ($message['body'] as $key => $body) {
        $message['body'][$key] = new FormattableMarkup($body, []);
      }
      break;

  }
}

Hat tip to blog post from flocondetoile.fr for the code snippet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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