@@ -21,8 +21,129 @@ Installation
21
21
22
22
.. include :: /components/require_autoload.rst.inc
23
23
24
+
25
+ Introduction
26
+ ------------
27
+
28
+
24
29
Usage
25
30
-----
26
31
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\C omponent\M ailer\M ailer;
33
+ use Symfony\C omponent\M ailer\T ransport\S mtp\S mtpTransport;
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\C omponent\M ailer\B ridge\G oogle\S mtp\G mailTransport;
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\C omponent\M ailer\T ransport;
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\C omponent\M ailer\M ailer;
123
+ use Symfony\C omponent\M ailer\M essenger\M essageHandler;
124
+ use Symfony\C omponent\M ailer\M essenger\S endEmailMessage;
125
+ use Symfony\C omponent\M ailer\S mtpEnvelope;
126
+ use Symfony\C omponent\M ailer\T ransport;
127
+ use Symfony\C omponent\M essenger\H andler\H andlersLocator;
128
+ use Symfony\C omponent\M essenger\M essageBus;
129
+ use Symfony\C omponent\M essenger\M iddleware\H andleMessageMiddleware;
130
+ use Symfony\C omponent\M ime\A ddress;
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