2
votes

I've built a very simple module to learn how to integrate the swiftmailer module for sending emails that use a custom twig template.

A copy of the default swiftmailer twig template located here: http://cgit.drupalcode.org/swiftmailer/tree/templates/swiftmailer.html.twig was slightly modified and renamed following the instructions included within the file itself:

 * This template may be overriden by module and/or mail key, using any of the
 * following template names:
 * - swiftmailer.html.twig: global, used by default.
 * - swiftmailer--mymodule.html.twig: only emails sent by the module "mymodule".
 * - swiftmailer--mymodule--test.html.twig: only emails by the module
 *   "mymodule" with key "test".
 *

The name of the twig file is swiftmailer--emailtester.html.twig which is located within the active themes/mytheme/templates folder.

When I execute the call to send the email within the emailtester.module and walk through the calls being made within the ThemeManager.php, twig.engine.php, and Render.php files in Core, the template path points back to the/modules/contrib/swiftmailer/templates/swiftmailer.html.twig file and not to the /themes/mytheme/templates/swiftmailer--emailtester.html.twig file.

I've cleared my cache using drush cr as well as clear all caches within the config->performance section. I've also tried clearing the theme registry as well but nothing seems to make a difference as swiftmailer always falls back to using its global template file.

Any suggestions would be greatly appreciated.

cheers,

devin

2
Experiencing the exact same issue at this time! Was this ever resolved? I will post a solution if I find one!Emmanuel Buckshi
Enable twig debug comments and check the resulting email html. Is your template name listed under FILE NAME SUGGESTIONS?Philipp Michael

2 Answers

7
votes

You might have selected the wrong theme to be used for email templates:

enter image description here

If you choose "Current" and the mail is send while your are on a admin page, only swiftmailer templates from the admin theme will be used. So make sure you select the theme which contains your custom swiftmailer twig files. If this is your standard frontend theme, you can select "Default".

And of course you have to set Swiftmailer as Formatter.

0
votes

The template will be not used until you will specify content-type header in your module_mail hook. Like:

$headers = array(
    'MIME-Version'              => '1.0',
    'Content-Type'              => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
    'Content-Transfer-Encoding' => '8Bit',
    'X-Mailer'                  => 'Drupal',
);