@@ -16,8 +16,140 @@ Installation
16
16
17
17
.. include :: /components/require_autoload.rst.inc
18
18
19
+
20
+ Introduction
21
+ ------------
22
+
23
+ Symfony mailer is an experimental component introduced in 4.3 which
24
+ will eventually replace swiftmailer.
25
+
26
+
19
27
Usage
20
28
-----
21
29
22
- We're currently working on the documentation of this component that was just
23
- added to Symfony. We'll publish it in a few days.
30
+ The Mailer component has two main classes: a ``Transport `` and the ``Mailer `` itself::
31
+
32
+ use Symfony\Component\Mailer\Mailer;
33
+ use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
34
+
35
+ $transport = new SmtpTransport('localhost');
36
+ $mailer = new Mailer($transport);
37
+ $mailer->send($email);
38
+
39
+ The `$email ` object is created via the :doc: `Mime component </components/mime >`.
40
+
41
+ Transport
42
+ ---------
43
+
44
+ The only transport that comes pre-installed with mailer is Smtp.
45
+
46
+ Below is the list of other popular providers with built in support.
47
+
48
+ ================== =============================================
49
+ Service Install with
50
+ ================== =============================================
51
+ Amazon SES ``composer require symfony/amazon-mailer ``
52
+ Gmail ``composer require symfony/google-mailer ``
53
+ MailChimp ``composer require symfony/mailchimp-mailer ``
54
+ Mailgun ``composer require symfony/mailgun-mailer ``
55
+ Postmark ``composer require symfony/postmark-mailer ``
56
+ SendGrid ``composer require symfony/sendgrid-mailer ``
57
+ ================== =============================================
58
+
59
+ For example, suppose you want to use Google's Gmail. First, install it:
60
+
61
+ .. code-block :: terminal
62
+
63
+ $ composer require symfony/google-mailer
64
+
65
+ .. code-block :: php
66
+
67
+ use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
68
+
69
+ $transport = new GmailTransport('user', 'pass');
70
+ $mailer = new Mailer($transport);
71
+ $mailer->send($email);
72
+
73
+ Use a DSN
74
+ ---------
75
+
76
+ The mailer component provides a convenient way to create transport object from DSN string::
77
+
78
+ use Symfony\Component\Mailer\Transport;
79
+
80
+ $transport = Transport::fromDsn($dsn);
81
+
82
+ Where ``$dsn `` as one of the form below.
83
+
84
+ - ``smtp://user:pass@gmail ``
85
+ - ``smtp://key@sendgrid ``
86
+ - ``smtp://null ``
87
+ - ``smtp://user:pass@mailgun ``
88
+ - ``http://key:domain@mailgun ``
89
+ - ``api://id@postmark ``
90
+
91
+ This provides a unified behaviour across all providers.
92
+ Easily switch from SMTP in development to a "real" provider in production with same API.
93
+
94
+ Failover transport
95
+ ------------------
96
+
97
+ You can create failover transport with the help of `|| ` operator::
98
+
99
+ $dsn = 'api://id@postmark || smtp://key@sendgrid';
100
+
101
+ So if the first transport fails, the mailer will attempt to send through the second transport.
102
+
103
+ Round Robin
104
+ -----------
105
+
106
+ If you want to send emails by using multiple transports in a round-robin fashion, you can use the
107
+ ``&& `` operator between the transports::
108
+
109
+ $dsn = 'api://id@postmark && smtp://key@sendgrid'
110
+
111
+ Async
112
+ -----
113
+
114
+ If you want to use the async functionality you need to install the :doc: `Messenger component </components/messenger >`.
115
+
116
+ .. code-block :: terminal
117
+
118
+ $ composer require symfony/messenger
119
+
120
+ Then, instantiate and pass a ``MessageBus `` as a second argument to ``Mailer ``::
121
+
122
+ use Symfony\Component\Mailer\Mailer;
123
+ use Symfony\Component\Mailer\Messenger\MessageHandler;
124
+ use Symfony\Component\Mailer\Messenger\SendEmailMessage;
125
+ use Symfony\Component\Mailer\SmtpEnvelope;
126
+ use Symfony\Component\Mailer\Transport;
127
+ use Symfony\Component\Messenger\Handler\HandlersLocator;
128
+ use Symfony\Component\Messenger\MessageBus;
129
+ use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware;
130
+ use Symfony\Component\Mime\Address;
131
+
132
+ $dsn = 'change-dsn-accordingly';
133
+
134
+ $transport = Transport::fromDsn($dsn);
135
+ $handler = new MessageHandler($transport);
136
+
137
+ $bus = new MessageBus([
138
+ new HandleMessageMiddleware(new HandlersLocator([
139
+ SendEmailMessage::class => [$handler],
140
+ ])),
141
+ ]);
142
+
143
+ $mailer = new Mailer($transport, $bus);
144
+
145
+ $mailer->send($email, new SmtpEnvelope(
146
+
147
+ [
148
+
149
+ ]
150
+ ));
151
+
152
+ Learn More
153
+ -----------
154
+
155
+ To learn more about how to use the mailer component, refer to the :doc: `Symfony Framework Mailer documentation </mailer >`.
0 commit comments