8000 [Mailer][FrameworkBundle] Add a way to use a custom transport by kevin-verschaeve · Pull Request #31931 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Mailer][FrameworkBundle] Add a way to use a custom transport #31931

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

Closed
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,7 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->children()
->scalarNode('dsn')->defaultValue('smtp://null')->end()
->scalarNode('transport')->defaultNull()->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1916,7 +1916,9 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
}

$loader->load('mailer.xml');
$container->getDefinition('mailer.default_transport')->setArgument(0, $config['dsn']);
$transport = $container->getDefinition('mailer.default_transport');
$transport->setArgument(0, $config['dsn']);
$transport->setArgument(4, $config['transport']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
],
'mailer' => [
'dsn' => 'smtp://null',
'transport' => null,
'enabled' => !class_exists(FullStack::class) && class_exists(Mailer::class),
],
];
Expand Down
22 changes: 17 additions & 5 deletions src/Symfony/Component/Mailer/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
*/
class Transport
{
public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null, string $customTransportClass = null): TransportInterface
{
// failover?
$dsns = preg_split('/\s++\|\|\s++/', $dsn);
if (\count($dsns) > 1) {
$transports = [];
foreach ($dsns as $dsn) {
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger);
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger, $customTransportClass);
}

return new Transport\FailoverTransport($transports);
Expand All @@ -49,16 +49,16 @@ public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher
if (\count($dsns) > 1) {
$transports = [];
foreach ($dsns as $dsn) {
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger);
$transports[] = self::createTransport($dsn, $dispatcher, $client, $logger, $customTransportClass);
}

return new Transport\RoundRobinTransport($transports);
}

return self::createTransport($dsn, $dispatcher, $client, $logger);
return self::createTransport($dsn, $dispatcher, $client, $logger, $customTransportClass);
}

private static function createTransport(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
private static function createTransport(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null, string $customTransportClass = null): TransportInterface
{
if (false === $parsedDsn = parse_url($dsn)) {
throw new InvalidArgumentException(sprintf('The "%s" mailer DSN is invalid.', $dsn));
Expand Down Expand Up @@ -169,6 +169,18 @@ private static function createTransport(string $dsn, EventDispatcherInterface $d
}

throw new LogicException(sprintf('The "%s" scheme is not supported for mailer "%s".', $parsedDsn['scheme'], $parsedDsn['host']));
case 'custom':
if (!$customTransportClass) {
throw new \LogicException('You must specify the transport class when using the "custom" provider.');
}

$customTransport = new $customTransportClass($user, $pass, $query, $client, $dispatcher, $logger);

if (!$customTransport instanceof TransportInterface) {
throw new \LogicException(sprintf('The transport class must implements "%s".', TransportInterface::class));
}

return $customTransport;
default:
if ('smtp' === $parsedDsn['scheme']) {
$transport = new Transport\Smtp\EsmtpTransport($parsedDsn['host'], $parsedDsn['port'] ?? 25, $query['encryption'] ?? null, $query['auth_mode'] ?? null, $dispatcher, $logger);
Expand Down
0