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