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