Skip to content

Commit 704cc0c

Browse files
hariktweaverryan
authored andcommitted
Initial mailer component docs
1 parent 2c8fdef commit 704cc0c

File tree

1 file changed

+123
-2
lines changed

1 file changed

+123
-2
lines changed

components/mailer.rst

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,129 @@ Installation
2121
2222
.. include:: /components/require_autoload.rst.inc
2323

24+
25+
Introduction
26+
------------
27+
28+
2429
Usage
2530
-----
2631

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.
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+
Refer :doc:`Mime component </components/mime>` how to create `$email` object.
40+
41+
42+
Transport
43+
---------
44+
45+
By default, the only transport available in the mailer component is Smtp.
46+
Below is the list of other popular providers with built in support.
47+
- Amazon SES : symfony/amazon-mailer
48+
- Google Gmail : symfony/google-mailer
49+
- Mandrill : symfony/mailchimp-mailer
50+
- Mailgun : symfony/mailgun-mailer
51+
- Postmark : symfony/postmark-mailer
52+
- Sendgrid : symfony/sendgrid-mailer
53+
54+
For example to use google's gmail as a transport you need to install symfony/google-mailer.
55+
56+
.. code-block:: terminal
57+
58+
$ composer require symfony/google-mailer
59+
60+
.. include:: /components/require_autoload.rst.inc
61+
62+
63+
use Symfony\Component\Mailer\Bridge\Google\Smtp\GmailTransport;
64+
65+
$transport = new GmailTransport('user', 'pass');
66+
$transport->send($email);
67+
68+
Use a Dsn
69+
---------
70+
71+
The mailer component provides a convenient way to create transport object from dsn string.
72+
73+
use Symfony\Component\Mailer\Transport;
74+
75+
$transport = Transport::fromDsn($dsn);
76+
77+
Where `$dns` as one of the form below.
78+
- smtp://user:pass@gmail
79+
- smtp://key@sendgrid
80+
- smtp://null
81+
- smtp://user:pass@mailgun
82+
- http://key:domain@mailgun
83+
- api://id@postmark
84+
85+
This provides a unified behaviour across all providers.
86+
Easily switch from SMTP in dev to a "real" provider in production with same API.
87+
88+
Failover transport
89+
------------------
90+
91+
You can create failover transport with the help of `||` operator.
92+
93+
Eg :
94+
95+
$dsn = 'api://id@postmark || smtp://key@sendgrid';
96+
97+
So if it fails at one transport, the mailer will attempt to send through the other transport.
98+
99+
RoundRobin
100+
----------
101+
102+
If you want to send mails via multiple transports, you can use the `&&` operator between the transports.
103+
104+
Eg :
105+
106+
$dsn = 'api://id@postmark && smtp://key@sendgrid'
107+
108+
109+
Async
110+
-----
111+
112+
If you want to use the async functionality you need to install `messenger` component.
113+
By default, `$bus` is null and if it is not configured, mailer is always using sync functionality.
114+
And async when `$bus` is configured for EnvelopedMessage.
115+
116+
.. code-block:: terminal
117+
118+
$ composer require symfony/messenger
119+
120+
.. include:: /components/require_autoload.rst.inc
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 = 'smtp://null';
133+
$transport = Transport::fromDsn($dsn);
134+
$handler = new MessageHandler($transport);
135+
136+
$bus = new MessageBus([
137+
new HandleMessageMiddleware(new HandlersLocator([
138+
SendEmailMessage::class => ['message_hander' => $handler],
139+
])),
140+
]);
141+
142+
$mailer = new Mailer($transport, $bus);
143+
144+
$mailer->send($email, new SmtpEnvelope(
145+
new Address('sender@example.com'),
146+
[
147+
new Address('recepient@example.com'),
148+
]
149+
));

0 commit comments

Comments
 (0)