Skip to content

Commit dc59b0e

Browse files
[Mailer] Add a way to use a custom transport
1 parent 01273b9 commit dc59b0e

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+1
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,7 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
14131413
->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
14141414
->children()
14151415
->scalarNode('dsn')->defaultValue('smtp://null')->end()
1416+
->scalarNode('transport')->defaultNull()->end()
14161417
->end()
14171418
->end()
14181419
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -1776,7 +1776,11 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
17761776
}
17771777

17781778
$loader->load('mailer.xml');
1779-
$container->getDefinition('mailer.transport')->setArgument(0, $config['dsn']);
1779+
$transport = $container->getDefinition('mailer.transport');
1780+
$transport->setArgument(0, $config['dsn']);
1781+
if (null !== $config['transport']) {
1782+
$transport->setArgument(4, new Reference($config['transport']));
1783+
}
17801784
}
17811785

17821786
/**

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
352352
],
353353
'mailer' => [
354354
'dsn' => 'smtp://null',
355+
'transport' => null,
355356
'enabled' => !class_exists(FullStack::class) && class_exists(Mailer::class),
356357
],
357358
];

src/Symfony/Component/Mailer/Transport.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
*/
3232
class Transport
3333
{
34-
public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
34+
public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null, TransportInterface $customTransport = null): TransportInterface
3535
{
3636
// failover?
3737
$dsns = preg_split('/\s++\|\|\s++/', $dsn);
3838
if (\count($dsns) > 1) {
3939
$transports = [];
4040
foreach ($dsns as $dsn) {
41-
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger);
41+
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger, $customTransport);
4242
}
4343

4444
return new Transport\FailoverTransport($transports);
@@ -49,16 +49,16 @@ public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher
4949
if (\count($dsns) > 1) {
5050
$transports = [];
5151
foreach ($dsns as $dsn) {
52-
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger);
52+
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger, $customTransport);
5353
}
5454

5555
return new Transport\RoundRobinTransport($transports);
5656
}
5757

58-
return self::createTransport($dsn, $dispatcher, $client, $logger);
58+
return self::createTransport($dsn, $dispatcher, $client, $logger, $customTransport);
5959
}
6060

61-
private static function createTransport(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
61+
private static function createTransport(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null, TransportInterface $customTransport = null): TransportInterface
6262
{
6363
if (false === $parsedDsn = parse_url($dsn)) {
6464
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN is invalid.', $dsn));
@@ -169,6 +169,12 @@ private static function createTransport(string $dsn, EventDispatcherInterface $d
169169
}
170170

171171
throw new LogicException(sprintf('The "%s" scheme is not supported for mailer "%s".', $parsedDsn['scheme'], $parsedDsn['host']));
172+
case 'custom':
173+
if (!$customTransport) {
174+
throw new \LogicException('You must specify the transport class when using the "custom" provider.');
175+
}
176+
177+
return new $customTransport($user, $pass, $query, $client, $dispatcher, $logger);
172178
default:
173179
if ('smtp' === $parsedDsn['scheme']) {
174180
$transport = new Transport\Smtp\EsmtpTransport($parsedDsn['host'], $parsedDsn['port'] ?? 25, $query['encryption'] ?? null, $query['auth_mode'] ?? null, $dispatcher, $logger);

0 commit comments

Comments
 (0)