Skip to content

Commit 34f23d0

Browse files
andreyboloninwouterj
authored andcommitted
add notifier.rst
1 parent 4810fbf commit 34f23d0

File tree

1 file changed

+152
-6
lines changed

1 file changed

+152
-6
lines changed

components/notifier.rst

Lines changed: 152 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.. index::
2-
single: Notifier
32
single: Notifications
43
single: Components; Notifier
54

@@ -14,6 +13,9 @@ The Notifier Component
1413
The Notifier component was introduced in Symfony 5.0 as an
1514
:doc:`experimental feature </contributing/code/experimental>`.
1615

16+
If you're using the Symfony Framework, read the
17+
:doc:`Symfony Framework Notifier documentation </notifier>`.
18+
1719
Installation
1820
------------
1921

@@ -23,11 +25,155 @@ Installation
2325
2426
.. include:: /components/require_autoload.rst.inc
2527

26-
27-
Usage
28+
Email
2829
-----
2930

30-
.. caution::
31+
The Notifier component has notify you when something goes wrong::
32+
33+
use Symfony\Bridge\Twig\Mime\NotificationEmail;
34+
use Symfony\Component\Mailer\Mailer;
35+
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
36+
37+
$transport = new EsmtpTransport('localhost');
38+
$mailer = new Mailer($transport);
39+
40+
$email = (new NotificationEmail())
41+
42+
43+
->exception($exception);
44+
45+
$mailer->send($email);
46+
47+
The ``$email`` object is created via the :doc:`Mime component </components/mime>`.
48+
49+
And configurable email template::
50+
51+
$email = (new NotificationEmail())
52+
->htmlEmail('email/system.html.twig')
53+
->textEmail('email/system.txt.twig');
54+
55+
With template::
56+
57+
{% extends "@email/system.html.twig" %}
58+
{% block style %}
59+
{{ parent() }}
60+
.container.body_alert {
61+
border-top: 30px solid #ec5840;
62+
}
63+
{% endblock %}
64+
{% block lines %}
65+
This is an automated email for the MyApp application.
66+
{{ parent() }}
67+
{% endblock %}
68+
{% block action %}
69+
{{ parent() }}
70+
<spacer size="16"></spacer>
71+
<button class="secondary" href="https://myapp.com/">Go to MyApp</button>
72+
{% endblock %}
73+
{% block exception %}{% endblock %}
74+
{% block footer_content %}
75+
<p><small>&copy; MyApp</small></p>
76+
{% endblock %}
77+
78+
79+
SMS
80+
---------
81+
82+
Sending SMS Messages the easy way::
83+
84+
/**
85+
* @Route("/checkout/thankyou")
86+
*/
87+
public function thankyou(Texter $texter /* ... */) {
88+
$sms = new SmsMessage('+1415999888', 'Revenue has just increased by 1€ per year!');
89+
$texter->send($sms);
90+
91+
return $this->render('checkout/thankyou.html.twig', [
92+
// ...
93+
]);
94+
}
95+
96+
Below is the list of other popular provider with built-in support:
97+
98+
==================
99+
Service
100+
==================
101+
Telegram
102+
Nexmo
103+
Slack
104+
Twilio
105+
==================
106+
107+
SMS low-level API::
108+
109+
$sms = new SmsMessage('+1415999888', 'Revenue has just increased!');
110+
$twilio = Transport::fromDsn('twilio://SID:TOKEN@default?from=FROM');
111+
$twilio->send($sms);
112+
$nexmo = Transport::fromDsn('nexmo://KEY:SECRET@default?from=FROM');
113+
$nexmo->send($sms);
114+
115+
SMS... higher-level API::
116+
117+
$texter = new Texter($twilio, $bus);
118+
$texter->send($sms);
119+
$transports = new Transports(['twilio' => $twilio, 'nexmo' => $nexmo]);
120+
$texter = new Texter($transports, $bus);
121+
$texter->send($sms);
122+
$sms->setTransport('nexmo');
123+
$texter->send($sms);
124+
$bus->dispatch($sms);
125+
126+
$dsn = 'failover(twilio://SID:TOKEN@default?from=FROM nexmo://KEY:SECRET@default?from=FROM)';
127+
128+
Message
129+
---------
130+
131+
Sending Messages the easy way::
132+
133+
/**
134+
* @Route("/checkout/thankyou")
135+
*/
136+
public function thankyou(Chatter $chatter /* ... */)
137+
{
138+
$message = new ChatMessage('Revenue increased by 1€ per year...');
139+
$chatter->send($message);
140+
return $this->render('checkout/thankyou.html.twig', [
141+
// ...
142+
]);
143+
}
144+
145+
Messages low-level API::
146+
147+
$message = new ChatMessage('Revenue increased by 1€ per year...');
148+
$slack = Transport::fromDsn('slack://TOKEN@default?channel=CHANNEL');
149+
$slack->send($sms);
150+
$telegram = Transport::fromDsn('telegram://TOKEN@default?channel=CHAT_ID');
151+
$telegram->send($sms);
152+
153+
Messages higher-level API::
154+
155+
$transports = Transport::fromDsns([
156+
'slack' => 'slack://TOKEN@default?channel=CHANNEL',
157+
'telegram' => 'telegram://TOKEN@default?channel=CHAT_ID'
158+
]);
159+
$chatter = new Chatter($transports, $bus);
160+
$chatter->send($message);
161+
$message->setTransport('telegram');
162+
$chatter->send($message);
163+
$bus->dispatch($message);
164+
165+
$options = (new SlackOptions())
166+
->iconEmoji('tada')
167+
->iconUrl('https://symfony.com')
168+
->username('SymfonyNext')
169+
->channel($channel)
170+
->block((new SlackSectionBlock())->text('Some Text'))
171+
->block(new SlackDividerBlock())
172+
->block((new SlackSectionBlock())
173+
->text('Some Text in another block')
174+
->accessory(new SlackImageBlockElement('http://placekitten.com/700/500', 'kitten'))
175+
)
176+
;
177+
$message = new ChatMessage('Default Text', $options);
31178

32-
We're still working on the docs of this component. Check this page again
33-
in a few days.
179+
$dsn = 'all(slack://TOKEN@default?channel=CHANNEL telegram://TOKEN@default?channel=CHAT_ID)';

0 commit comments

Comments
 (0)