diff --git a/guides/plugins/plugins/content/mail/add-data-to-mails.md b/guides/plugins/plugins/content/mail/add-data-to-mails.md index 6cb2509cb..ede6e78e2 100644 --- a/guides/plugins/plugins/content/mail/add-data-to-mails.md +++ b/guides/plugins/plugins/content/mail/add-data-to-mails.md @@ -27,8 +27,9 @@ To be precise, you have to extend the `send` method, whose last parameter is the So let's do that, here's an example of a decorated mail service: -```php -// /src/Service/AddDataToMails.php +::: code-group + +```php [PLUGIN_ROOT/src/Service/AddDataToMails.php] {{ myCustomData }} to any mail template, it should then print "Example data". You can use any kind of data here, e.g. an array of data. ### Register your decorator @@ -77,8 +80,9 @@ Of course you still have to register the decoration to the service container. Be Here's the respective example `services.xml`: -```xml -// /src/Resources/config/services.xml +::: code-group + +```xml [PLUGIN_ROOT/src/Resources/config/services.xml] ``` + +::: + +## Adding data via subscriber + +In many cases, adding mail data via an event subscriber is a suitable solution. This way, you avoid the overhead of decorating the mail service. Simply create an event subscriber and listen to the `MailBeforeValidateEvent` event. There, you can safely add template or mail data. +Here is a small example: + +::: code-group + +```php [PLUGIN_ROOT/src/Subscriber/MyMailSubscriber.php] + 'beforeMailValidate', + ]; + } + + public function beforeMailValidate( + MailBeforeValidateEvent $event + ): void { + $context = $event->getContext(); + $data = $event->getData(); // Get mail data + $templateData = $event->getTemplateData(); // Get mail template data + + $event->addTemplateData('key', 'value'); // Example of adding data to the mail template + } +} +``` + +:::