Skip to content

Commit 8abbe0e

Browse files
authored
Merge branch '5.x' into toyibat2
2 parents a11af54 + d445026 commit 8abbe0e

1 file changed

Lines changed: 81 additions & 21 deletions

File tree

docs/plugin_extensions/emails.rst

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,104 @@
11
Emails
22
######
33

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+
412
.. vale off
513
614
.. 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.
717

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
919
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+
************
1122

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.
1324

14-
.. vale on
25+
Email token capabilities consist of two parts:
1526

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+
=====================================
1732

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.
2334

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.
2637

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::
2939

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.
3241

33-
Email token capabilities consist of two parts: registering your custom tokens and rendering them.
42+
Rendering custom tokens
43+
=======================
3444

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
3790
3891
While Mautic supports :xref:`A/B testing` out of the box, you might have more complex needs to determine A/B test winner criteria.
3992

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
42102
43103
.. code-block:: PHP
44104

0 commit comments

Comments
 (0)