Feature-93689-PSR-14EventsOnSendingMessagesWithMailer.rst 3.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
.. include:: /Includes.rst.txt

.. _feature-93689-1654629861

===============================================================
Feature: #93689 - PSR-14 Events on sending messages with Mailer
===============================================================

See :issue:`93689`

Description
===========

TYPO3's :php:`MailerInterface` implementation :php:`Mailer` is used for sending
messages, e.g. in the EXT:form email finishers. To allow further handling and
manipulation of the message sending process, two new PSR-14 Events have been
introduced.

The :php:`BeforeMailerSentMessageEvent` is dispatched before the message
is sent by the mailer and can be used to manipulate the :php:`RawMessage`
and the :php:`Envelope`. Usually an :php:`Email` or `FluidEmail` instance
is given as :php:`RawMessage`. Additionally the :php:`MailerInstance` is
given, which depending on the implementation - usually
:php:`TYPO3\CMS\Core\Mail\Mailer` - contains the :php:`Transport` object,
which can be retrieved using the :php:`getTransport()` method.

The :php:`AfterMailerSentMessageEvent` is dispatched as soon as the
message has been sent via the corresponding :php:`TransportInterface`.
The Event receives the current :php:`MailerInstance`, which depending
on the implementation - usually :php:`TYPO3\CMS\Core\Mail\Mailer` -
contains the :php:`SentMessage` object, which can be retrieved using
the :php:`getSentMessage()` method.

Registration of the Event listeners in your extensions' :file:`Services.yaml`:

.. code-block:: yaml

  MyVendor\MyPackage\EventListener\MailerSentMessageEventListener:
    tags:
      - name: event.listener
        identifier: 'my-package/modify-message'
        method: 'modifyMessage'
      - name: event.listener
        identifier: 'my-package/process-sent-message'
        method: 'processSentMessage'

The corresponding event listener class:

.. code-block:: php

    use Psr\Log\LoggerAwareInterface;
    use Psr\Log\LoggerAwareTrait;
    use Symfony\Component\Mime\Address;
    use Symfony\Component\Mime\Email;
    use TYPO3\CMS\Core\Mail\Event\AfterMailerSentMessageEvent;
    use TYPO3\CMS\Core\Mail\Event\BeforeMailerSentMessageEvent;
    use TYPO3\CMS\Core\Mail\Mailer;

    final class MailerSentMessageEventListener implementsLoggerAwareInterface
    {
        use LoggerAwareTrait;

        public function modifyMessage(BeforeMailerSentMessageEvent $event): void
        {
            $message = $event->getMessage();

            // If $message is an Email implementation, add an additional recipient
            if ($message instanceof Email) {
                $message->addCc(new Address('kasperYYYY@typo3.org'));
            }
        }

        public function processSentMessage(AfterMailerSentMessageEvent $event): void
        {
            $mailer = $event->getMailer();
            if ($mailer instanceof Mailer) {
                $sentMessage = $mailer->getSentMessage();
                if ($sentMessage !== null) {
                    $this->logger->debug($sentMessage->getDebug());
                }
            }
        }
    }

Impact
======

With the new PSR-14 events, it's now possible to manipulate messages before
they are sent by the mailer. Additionally, after the mailer has sent messages,
further processing can be performed.

.. index:: PHP-API, ext:core