|
1 | 1 | Emails |
2 | 2 | ###### |
3 | 3 |
|
| 4 | +There are multiple ways to extend the way Mautic works with Emails. This document describes the following options for extending Mautic's Email capabilities: |
| 5 | + |
| 6 | +* Email tokens |
| 7 | +* A/B testing |
| 8 | +* Monitored inbox Integration |
| 9 | +* Email transport or Email providers |
| 10 | +* Email stat helpers |
| 11 | + |
4 | 12 | .. vale off |
5 | 13 |
|
6 | 14 | .. note:: |
| 15 | + |
| 16 | + Extending generally works by hooking into events using event listeners or subscribers. Read more about them in the :doc:`listeners and subscribers</plugins/event_listeners>` section. |
7 | 17 |
|
8 | | - The content for this page requires a major update. The legacy page contains outdated and potentially inaccurate information. You can still access it in the :xref:`legacy repository`. |
| 18 | +.. vale on |
9 | 19 |
|
10 | | - If you're interested in helping develop the new content for this page and others, consider joining the documentation efforts. |
| 20 | +Email tokens |
| 21 | +************ |
11 | 22 |
|
12 | | - Please read the :xref:`dev docs contributing guidelines` and :xref:`Contributing to Mautic’s documentation` to get started. |
| 23 | +Email tokens are placeholders that you can insert into an Email. Dynamically generated content replaces these tokens once Mautic sends the Email or the User views it in the browser. |
13 | 24 |
|
14 | | -.. vale on |
| 25 | +Email token capabilities consist of two parts: |
15 | 26 |
|
16 | | -There are multiple ways to extend the way Mautic works with Emails. This document describes the following options for extending Mautic's Email capabilities: |
| 27 | +* Registering custom tokens |
| 28 | +* Rendering custom tokens |
| 29 | + |
| 30 | +Registering custom tokens in builders |
| 31 | +===================================== |
17 | 32 |
|
18 | | -- Email tokens |
19 | | -- A/B testing |
20 | | -- Monitored Inbox Integration |
21 | | -- Email transport/Email providers |
22 | | -- Email stat helpers |
| 33 | +Registering tokens leverages the ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_BUILD`` event. The event is dispatched before displaying the email builder form, to allow adding of tokens. |
23 | 34 |
|
24 | | -Email tokens and A/B testing |
25 | | ----------------------------- |
| 35 | +An event listener receives the ``Mautic\EmailBundle\Event\EmailBuilderEvent``. |
| 36 | +Use its ``$event->addToken($token, $htmlContent)`` to add your token. |
26 | 37 |
|
27 | | -Email tokens are placeholders that you can insert into an Email. |
28 | | -They get replaced by Dynamic Content once the Email gets sent or viewed in the browser. |
| 38 | +.. note:: |
29 | 39 |
|
30 | | -You can find examples of both Email token handling and A/B testing in the code example below. |
31 | | -Both leverage the ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_BUILD`` event. Read more about :doc:`listeners and subscribers</plugins/event_listeners>`. |
| 40 | + You can either hard-code your tokens' textual description in ``$htmlContent`` or use a translatable string. |
32 | 41 |
|
33 | | -Email token capabilities consist of two parts: registering your custom tokens and rendering them. |
| 42 | +Rendering custom tokens |
| 43 | +======================= |
34 | 44 |
|
35 | | -- ``$event->addToken($uniqueId, $htmlContent)`` allows you to show the Email token in the email builder, so that users can easily add the token to their emails. |
36 | | -- ``$event->getContent()`` and ``$event->setContent()`` are used for replacing the Email token with actual Dynamic Content once the Email gets send or viewed in the browser. |
| 45 | +To render custom tokens, use the ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_SEND`` event when Mautic sends the Email, or the ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_DISPLAY`` event when the Email displays in a browser such as after the Contact clicks the ``{webview_url}`` link. |
| 46 | + |
| 47 | +An event listener receives in both cases the ``Mautic\EmailBundle\Event\EmailSendEvent``. You can replace a custom token using the events ``$event->addToken($token, $contentToReplaceToken)``. |
| 48 | + |
| 49 | +Basic token replacement |
| 50 | +======================= |
| 51 | + |
| 52 | +.. code-block:: PHP |
| 53 | +
|
| 54 | + <?php |
| 55 | + |
| 56 | + // plugins/HelloWorldBundle/EventListener/EmailSubscriber.php |
| 57 | + class EmailSubscriber implements EventSubscriberInterface |
| 58 | + { |
| 59 | + |
| 60 | + public static function getSubscribedEvents(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + EmailEvents::EMAIL_ON_BUILD => ['onEmailBuild', 0], |
| 64 | + EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 0], |
| 65 | + EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0], |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
| 69 | + public function onEmailBuild(EmailBuilderEvent $event): void |
| 70 | + { |
| 71 | + $event->addToken('{my_custom_token}', 'My Custom Token'); |
| 72 | + } |
| 73 | + |
| 74 | + public function onEmailGenerate(EmailSendEvent $event): void |
| 75 | + { |
| 76 | + $event->addToken('{my_custom_token}', 'Hello <b>World!</b>'); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | +.. note:: |
| 81 | + |
| 82 | + For more complex replacements, use the event's ``$event->getContent()`` and ``$event->setContent()`` methods. |
| 83 | + |
| 84 | +.. vale off |
| 85 | +
|
| 86 | +Email A/B testing |
| 87 | +***************** |
| 88 | + |
| 89 | +.. vale on |
37 | 90 |
|
38 | 91 | While Mautic supports :xref:`A/B testing` out of the box, you might have more complex needs to determine A/B test winner criteria. |
39 | 92 |
|
40 | | -- ``$event->addAbTestWinnerCriteria()`` allows you to do exactly that. Using your custom logic, you can decide the winner of such criteria. An example is shown below. |
41 | | -- ``$event->setAbTestResults()`` is where you set the actual A/B test results. More details are in the code example below. |
| 93 | +* Use ``$event->addAbTestWinnerCriteria()`` to apply your custom logic when deciding the winner based on specific criteria. |
| 94 | +* Set the actual A/B test results with ``$event->setAbTestResults()``. |
| 95 | + |
| 96 | +.. vale off |
| 97 | +
|
| 98 | +A/B testing examples |
| 99 | +==================== |
| 100 | + |
| 101 | +.. vale on |
42 | 102 |
|
43 | 103 | .. code-block:: PHP |
44 | 104 |
|
|
0 commit comments