-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extends email token documentation in emails.rst #194
base: 5.x
Are you sure you want to change the base?
Changes from 1 commit
a2cd67b
575653e
8b86a24
e8cab39
9921b6a
bd6c74a
e62f66f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,25 +9,83 @@ There are multiple ways to extend the way Mautic works with Emails. This documen | |
- Email transport/Email providers | ||
- Email stat helpers | ||
|
||
Email tokens and A/B testing | ||
---------------------------- | ||
.. Note:: Extending generally works by hooking into events using event listeners or subscribers. Read more about :doc:`listeners and subscribers</plugins/event_listeners>`. | ||
|
||
Email tokens | ||
------------ | ||
|
||
Email tokens are placeholders that you can insert into an Email. | ||
They get replaced by Dynamic Content once the Email gets sent or viewed in the browser. | ||
|
||
You can find examples of both Email token handling and A/B testing in the code example below. | ||
Both leverage the ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_BUILD`` event. Read more about :doc:`listeners and subscribers</plugins/event_listeners>`. | ||
|
||
Email token capabilities consist of two parts: registering your custom tokens and rendering them. | ||
|
||
- ``$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. | ||
- ``$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. | ||
Registering custom tokens in builders | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Registering tokens leverage the ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_BUILD`` event. | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
The event is dispatched before displaying the email builder form to allow adding of tokens. | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An event listener receives the ``Mautic\EmailBundle\Event\EmailBuilderEvent``. | ||
Use its ``$event->addToken($token, $htmlContent)`` to add your token. | ||
|
||
.. Note:: You can either hard code your tokens textual description in ``$htmlContent`` or use a translatable string. | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Rendering custom tokens | ||
^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
Registering tokens leverage the | ||
- ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_SEND`` event when the email is sent or the | ||
- ``\Mautic\EmailBundle\EmailEvents::EMAIL_ON_SEND`` event when the email is rendered for viewing in a browser, i.e., after the Lead clicked the ``{webview_url}`` link. | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
An event listener receives in both cases the ``Mautic\EmailBundle\Event\EmailSendEvent``. | ||
Replacing a custom token is as easy as using the events ``$event->addToken($token, $contentToReplaceToken)``. | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
Example | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
^^^^^^^ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heading also needs fixing here please. |
||
|
||
.. code-block:: PHP | ||
|
||
<?php | ||
|
||
// plugins/HelloWorldBundle/EventListener/EmailSubscriber.php | ||
class EmailSubscriber implements EventSubscriberInterface | ||
{ | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
EmailEvents::EMAIL_ON_BUILD => ['onEmailBuild', 0], | ||
EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 0], | ||
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0], | ||
]; | ||
} | ||
|
||
public function onEmailBuild(EmailBuilderEvent $event): void | ||
{ | ||
$event->addToken('{my_custom_token}', 'My Custom Token'); | ||
} | ||
|
||
public function onEmailGenerate(EmailSendEvent $event): void | ||
{ | ||
$event->addToken('{my_custom_token}', 'Hello <b>World!</b>'); | ||
} | ||
} | ||
|
||
.. Note:: If you need more complex replacing you can use the event's ``$event->getContent()`` and ``$event->setContent()`` methods. See the example in the following section. | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
Email A/B testing | ||
----------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heading nesting needs fixing here too, I think perhaps it should be an H2 heading? |
||
|
||
While Mautic supports :xref:`A/B testing` out of the box, you might have more complex needs to determine A/B test winner criteria. | ||
|
||
- ``$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. | ||
- ``$event->setAbTestResults()`` is where you set the actual A/B test results. More details are in the code example below. | ||
|
||
Example | ||
putzwasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
^^^^^^^ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heading nesting needs fixing, and it must reach the end of the text for the heading. |
||
|
||
.. code-block:: PHP | ||
|
||
<?php | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the correct headings:
This is a H2 so it should be *** not ---