8000 [Mailer] Add a way to use a custom transport · symfony/symfony@dd07ad3 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd07ad3

Browse files
[Mailer] Add a way to use a custom transport
1 parent 482c357 commit dd07ad3

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
15251525
->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
15261526
->children()
15271527
->scalarNode('dsn')->defaultValue('smtp://null')->end()
1528+
->scalarNode('transport')->defaultNull()->end()
15281529
->end()
15291530
->end()
15301531
->end()

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

8000
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1916,7 +1916,11 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
19161916
}
19171917

19181918
$loader->load('mailer.xml');
1919-
$container->getDefinition('mailer.transport')->setArgument(0, $config['dsn']);
1919+
$transport = $container->getDefinition('mailer.transport');
1920+
$transport->setArgument(0, $config['dsn']);
1921+
if (null !== $config['transport']) {
1922+
$transport->setArgument(4, new Reference($config['transport']));
1923+
}
19201924
}
19211925

19221926
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
374374
],
375375
'mailer' => [
376376
'dsn' => 'smtp://null',
377+
'transport' => null,
377378
'enabled' => !class_exists(FullStack::class) && class_exists(Mailer::class),
378379
],
379380
];

src/Symfony/Component/Mailer/Transport.php

Lines changed: 11 additions & 5 deletions
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)
0