Skip to content

[Mailer] Add custom transport factories #16373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,54 @@ Other Options

This option was introduced in Symfony 5.2.

Custom Transport Factories
~~~~~~~~~~~~~~~~~~~~~~~~~~

There is a way to easily create your own custom transport factory in case you need to do something special creating the actual transport.

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::


final class CustomTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
// create and return the transport
}

protected function getSupportedSchemes(): array
{
return ['custom_schema'];
}
}

Finally, declare the new factory in setting tag the tag `mailer.transport_factory`:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
mailer.transport_factory.custom:
class: App\CustomTransportFactory
parent: mailer.transport_factory.abstract
tags:
- {name: mailer.transport_factory}

.. code-block:: xml

<!-- config/services.xml -->
<service id="mailer.transport_factory.custom" class="App\CustomTransportFactory" parent="mailer.transport_factory.abstract">
<tag name="mailer.transport_factory"/>
</service>

.. code-block:: php

// config/services.php
$services->set('mailer.transport_factory.custom', App\CustomTransportFactory::class)
->parent('mailer.transport_factory.abstract')
->tag('mailer.transport_factory');

Creating & Sending Messages
---------------------------

Expand Down