Skip to content

Commit ad77916

Browse files
Add Custom Transport Factories
This might be useful for very custom use cases where we need to extract some data or do something before being able to create the actual transport.
1 parent dd5f326 commit ad77916

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

mailer.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,55 @@ As with the failover transport, round-robin retries deliveries until
215215
a transport succeeds (or all fail). In contrast to the failover transport,
216216
it *spreads* the load across all its transports.
217217

218+
Custom Transport Factories
219+
~~~~~~~~~~~~~~~~~~~~~~~~~~
220+
221+
There is a way to easily create your own custom transport factory in case you need to do something special creating the actual transport.
222+
223+
The new factory needs to implement `Symfony\Component\Mailer\Transport\TransportFactoryInterface`. To remove some boilerplate you can even extend from `Symfony\Component\Mailer\Transport\AbstractTransportFactory` which will simplify the new factory:
224+
225+
.. code-block:: php
226+
227+
final class CustomTransportFactory extends AbstractTransportFactory
228+
{
229+
public function create(Dsn $dsn): TransportInterface
230+
{
231+
// create and return the transport
232+
}
233+
234+
protected function getSupportedSchemes(): array
235+
{
236+
return ['custom_schema'];
237+
}
238+
}
239+
240+
Finally, declare the new factory in setting tag the tag `mailer.transport_factory`:
241+
242+
.. configuration-block::
243+
244+
.. code-block:: yaml
245+
246+
# config/services.yaml
247+
mailer.transport_factory.custom:
248+
class: App\CustomTransportFactory
249+
parent: mailer.transport_factory.abstract
250+
tags:
251+
- {name: mailer.transport_factory}
252+
253+
.. code-block:: xml
254+
255+
<!-- config/services.xml -->
256+
<service id="mailer.transport_factory.custom" class="App\CustomTransportFactory" parent="mailer.transport_factory.abstract">
257+
<tag name="mailer.transport_factory" />
258+
</service>
259+
260+
.. code-block:: php
261+
262+
// config/services.php
263+
$services->set('mailer.transport_factory.custom', App\CustomTransportFactory::class)
264+
->parent('mailer.transport_factory.abstract')
265+
->tag('mailer.transport_factory');
266+
218267
Creating & Sending Messages
219268
---------------------------
220269

0 commit comments

Comments
 (0)