@@ -46,7 +46,6 @@ The notifier component supports the following channels:
46
46
API's tokens.
47
47
48
48
.. _notifier-sms-channel :
49
- .. _notifier-texter-dsn :
50
49
51
50
SMS Channel
52
51
~~~~~~~~~~~
@@ -169,8 +168,47 @@ configure the ``texter_transports``:
169
168
;
170
169
};
171
170
171
+ .. _sending-sms :
172
+
173
+ The :class: `Symfony\\ Component\\ Notifier\\ TexterInterface ` class allows you to
174
+ send SMS messages::
175
+
176
+ // src/Controller/SecurityController.php
177
+ namespace App\Controller;
178
+
179
+ use Symfony\Component\Notifier\Message\SmsMessage;
180
+ use Symfony\Component\Notifier\TexterInterface;
181
+ use Symfony\Component\Routing\Annotation\Route;
182
+
183
+ class SecurityController
184
+ {
185
+ /**
186
+ * @Route("/login/success")
187
+ */
188
+ public function loginSuccess(TexterInterface $texter)
189
+ {
190
+ $sms = new SmsMessage(
191
+ // the phone number to send the SMS message to
192
+ '+1411111111',
193
+ // the message
194
+ 'A new login was detected!'
195
+ );
196
+
197
+ $sentMessage = $texter->send($sms);
198
+
199
+ // ...
200
+ }
201
+ }
202
+
203
+ The ``send() `` method returns a variable of type
204
+ :class: `Symfony\\ Component\\ Notifier\\ Message\\ SentMessage ` which provides
205
+ information such as the message ID and the original message contents.
206
+
207
+ .. versionadded :: 5.2
208
+
209
+ The ``SentMessage `` class was introduced in Symfony 5.2.
210
+
172
211
.. _notifier-chat-channel :
173
- .. _notifier-chatter-dsn :
174
212
175
213
Chat Channel
176
214
~~~~~~~~~~~~
@@ -273,6 +311,39 @@ Chatters are configured using the ``chatter_transports`` setting:
273
311
;
274
312
};
275
313
314
+ The :class: `Symfony\\ Component\\ Notifier\\ ChatterInterface ` class allows
315
+ you to send messages to chat services::
316
+
317
+ // src/Controller/CheckoutController.php
318
+ namespace App\Controller;
319
+
320
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
321
+ use Symfony\Component\Notifier\ChatterInterface;
322
+ use Symfony\Component\Notifier\Message\ChatMessage;
323
+ use Symfony\Component\Routing\Annotation\Route;
324
+
325
+ class CheckoutController extends AbstractController
326
+ {
327
+ /**
328
+ * @Route("/checkout/thankyou")
329
+ */
330
+ public function thankyou(ChatterInterface $chatter)
331
+ {
332
+ $message = (new ChatMessage('You got a new invoice for 15 EUR.'))
333
+ // if not set explicitly, the message is send to the
334
+ // default transport (the first one configured)
335
+ ->transport('slack');
336
+
337
+ $sentMessage = $chatter->send($message);
338
+
339
+ // ...
340
+ }
341
+ }
342
+
343
+ The ``send() `` method returns a variable of type
344
+ :class: `Symfony\\ Component\\ Notifier\\ Message\\ SentMessage ` which provides
345
+ information such as the message ID and the original message contents.
346
+
276
347
.. _notifier-email-channel :
277
348
278
349
Email Channel
@@ -748,18 +819,88 @@ all configured texter and chatter transports only in the ``dev`` (and/or
748
819
chatter_transports :
749
820
slack : ' null://null'
750
821
822
+ .. index ::
823
+ single: Notifier; Events
824
+
825
+ Using Events
826
+ ------------
827
+
828
+ .. versionadded :: 5.4
829
+
830
+ The ``MessageEvent ``, ``FailedMessageEvent `` and ``SentMessageEvent `` were
831
+ introduced in Symfony 5.4.
832
+
833
+ The :class: `Symfony\\ Component\\ Notifier\\ Transport` ` class of the Notifier component
834
+ allows you to optionally hook into the lifecycle via events.
835
+
836
+ The ``MessageEvent::class `` Event
837
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
838
+
839
+ **Typical Purposes **: Doing something before the message is send (like logging
840
+ which message is going to be send, or displaying something about the event
841
+ to be executed.
842
+
843
+ Just before send the message, the event class ``MessageEvent `` is
844
+ dispatched. Listeners receive a
845
+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ MessageEvent ` event::
846
+
847
+ use Symfony\Component\Notifier\Event\MessageEvent;
848
+
849
+ $dispatcher->addListener(MessageEvent::class, function (MessageEvent $event) {
850
+ // gets the message instance
851
+ $message = $event->getMessage();
852
+
853
+ // log something
854
+ $this->logger(sprintf('Message with subject: %s will be send to %s, $message->getSubject(), $message->getRecipientId()'));
855
+ });
856
+
857
+ The ``FailedMessageEvent `` Event
858
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
859
+
860
+ **Typical Purposes **: Doing something before the exception is thrown (Retry to send the message or log additional information).
861
+
862
+ Whenever an exception is thrown while sending the message, the event class ``FailedMessageEvent `` is
863
+ dispatched. A listener can do anything useful before the exception is thrown.
864
+
865
+ Listeners receive a
866
+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ FailedMessageEvent ` event::
867
+
868
+ use Symfony\Component\Notifier\Event\FailedMessageEvent;
869
+
870
+ $dispatcher->addListener(FailedMessageEvent::class, function (FailedMessageEvent $event) {
871
+ // gets the message instance
872
+ $message = $event->getMessage();
873
+
874
+ // gets the error instance
875
+ $error = $event->getError();
876
+
877
+ // log something
878
+ $this->logger(sprintf('The message with subject: %s has not been sent successfully. The error is: %s, $message->getSubject(), $error->getMessage()'));
879
+ });
880
+
881
+ The ``SentMessageEvent `` Event
882
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
883
+
884
+ **Typical Purposes **: To perform some action when the message is successfully sent (like retrieve the id returned
885
+ when the message is sent).
886
+
887
+ After the message has been successfully sent, the event class ``SentMessageEvent `` is
888
+ dispatched. Listeners receive a
889
+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ SentMessageEvent ` event::
890
+
891
+ use Symfony\Component\Notifier\Event\SentMessageEvent;
892
+
893
+ $dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) {
894
+ // gets the message instance
895
+ $message = $event->getOriginalMessage();
896
+
897
+ // log something
898
+ $this->logger(sprintf('The message has been successfully sent and have id: %s, $message->getMessageId()'));
899
+ });
900
+
751
901
.. TODO
752
902
.. - Using the message bus for asynchronous notification
753
903
.. - Describe notifier monolog handler
754
904
.. - Describe notification_on_failed_messages integration
755
905
756
- Learn more
757
- ----------
758
-
759
- .. toctree ::
760
- :maxdepth: 1
761
- :glob:
762
-
763
- notifier/*
764
-
765
906
.. _`RFC 3986` : https://www.ietf.org/rfc/rfc3986.txt
0 commit comments