Skip to content

Commit dcc3404

Browse files
committed
minor #16373 [Mailer] Add custom transport factories (AlbertMorenoDEV)
This PR was submitted for the 4.4 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [Mailer] 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. The feature apparently was added here: symfony/symfony#31946 Commits ------- 72b69c1 [Mailer] Add custom transport factories
2 parents d52bced + 72b69c1 commit dcc3404

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

mailer.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,54 @@ Other Options
356356

357357
This option was introduced in Symfony 5.2.
358358

359+
Custom Transport Factories
360+
~~~~~~~~~~~~~~~~~~~~~~~~~~
361+
362+
There is a way to easily create your own custom transport factory in case you need to do something special creating the actual transport.
363+
364+
The new factory needs to implement :class:`Symfony\\Component\\Mailer\\Transport\\TransportFactoryInterface`. To remove some boilerplate you can even extend from :class:`Symfony\\Component\\Mailer\\Transport\\AbstractTransportFactory` which will simplify the new factory::
365+
366+
367+
final class CustomTransportFactory extends AbstractTransportFactory
368+
{
369+
public function create(Dsn $dsn): TransportInterface
370+
{
371+
// create and return the transport
372+
}
373+
374+
protected function getSupportedSchemes(): array
375+
{
376+
return ['custom_schema'];
377+
}
378+
}
379+
380+
Finally, declare the new factory in setting tag the tag `mailer.transport_factory`:
381+
382+
.. configuration-block::
383+
384+
.. code-block:: yaml
385+
386+
# config/services.yaml
387+
mailer.transport_factory.custom:
388+
class: App\CustomTransportFactory
389+
parent: mailer.transport_factory.abstract
390+
tags:
391+
- {name: mailer.transport_factory}
392+
393+
.. code-block:: xml
394+
395+
<!-- config/services.xml -->
396+
<service id="mailer.transport_factory.custom" class="App\CustomTransportFactory" parent="mailer.transport_factory.abstract">
397+
<tag name="mailer.transport_factory"/>
398+
</service>
399+
400+
.. code-block:: php
401+
402+
// config/services.php
403+
$services->set('mailer.transport_factory.custom', App\CustomTransportFactory::class)
404+
->parent('mailer.transport_factory.abstract')
405+
->tag('mailer.transport_factory');
406+
359407
Creating & Sending Messages
360408
---------------------------
361409

0 commit comments

Comments
 (0)