- Issue created by @ExTexan
- Status changed to Fixed
over 1 year ago 12:01pm 11 August 2023 - ๐ฌ๐งUnited Kingdom adamps
The documentation we have is here https://www.drupal.org/docs/contributed-modules/drupal-symfony-mailer/mi... โ however I guess you have mostly gone beyond that.
swiftmailer passed $message to the template, but this module does not because $message is the old mailer API and this module has a new one.
When you try to do it the new way, you need to write an email builder. Because you have hook_mail it's trying to do automatic conversion from the old API.
The body element can do anything a template can do.
Marking as fixed, because that's the limit of how much of my time I wish to give you for free๐.
- ๐ฉ๐ฐDenmark FrittenKeeZ
Marking as fixed, because that's the limit of how much of my time I wish to give you for free๐.
Seeing as the documentation is clearly lacking, both in terms of migrating from Swiftmailer and how to use Twig and custom params with the new EmailBuilder, I can't possible think of a reason why you would mark this as fixed - you might as well just close this issue as you don't seem interested in getting that functionality to work.
- ๐ฉ๐ฐDenmark FrittenKeeZ
@ExTexan I managed to get it working by manually rendering the twig files and parse the markup along to body within hook_mail.
$module_path = \Drupal::service('extension.list.module')->getPath('my_module'); $template = $module_path . '/templates/email-body--' . str_replace('_', '-', $key) . '.html.twig'; $markup = twig_render_template($template, ['message' => compact('params'), 'theme_hook_original' => 'email_body']); $message['body'] = [$markup]; $message['format'] = 'text/html';
Then I added the wrapper template file which sole content is {{ body | raw }}
/** * Implements hook_theme(). */ function my_module_theme($existing, $type, $theme, $path) { // Copied from email-wrap theme hook. return [ 'email_wrap__my_module' => [ 'template' => 'email-wrap--my-module', 'variables' => [ 'email' => NULL, 'body' => '', 'is_html' => TRUE, ], ], ]; }
- ๐บ๐ธUnited States ExTexan
@FrittenKeeZ,
Thanks for the helpful posts. Seeing your messages here made me realize I had neglected to come back and post the solution we settled on.
I added this line at the top of all twig templates:
{% set params = _context.email.params.legacy_message.params %}
Then in the rest of the template, I changed all occurrences of "message.params.my_field" to "params.my_field".
No other code changes were needed elsewhere in the site.
Automatically closed - issue fixed for 2 weeks with no activity.
- Status changed to Fixed
about 1 year ago 8:05am 13 October 2023 - ๐ฎ๐ณIndia jaspreet21
@ExTexan,
Thanks for the code and the explanation, can you please enlighten with more detail on
{% set params = _context.email.params.legacy_message.params %}
Usage of this above and how are you sending email now.
My understanding is you have used this above set parms in the Twig you were already using but not just have renamed them or changed the 'swiftmailer' prefix to 'email' to accommodate the new change.
How ever I am not sure which mechanism are you using to send the emails now and also where have you printed this $email = {Drupal\symfony_mailer\Email}
Please help, thanks.
- ๐น๐ณTunisia A-Marwen
in case someone wants to pronts the parameters or the webfom fields from inside the email.html.twig add this preprocess to yout .theme file :
/** * Implements hook_preprocess_HOOK */ function d10_preprocess_email(&$variables) { /** @var Drupal\symfony_mailer\Email $email*/ $email = $variables['email']; // {% set params = _context.email.params.legacy_message.params %} /** @var Drupal\webform\Entity\WebformSubmission $webform_submission */ $params = $email->getParam('legacy_message')['params']; $webFormSubmission = $params['webform_submission']; // collecting the webform fields into an array. $variables['params'] = $webFormSubmission->getData(); $variables['form'] = $webFormSubmission->getData(); }
then in the email.html.twig just use form.{webformfieldName} EX :
<ul> <li>{{ form.entreprise_email }}</li> <li>{{ form.message }}</li> <li>{{ form.domain }}</li> </ul>