8000 Support native transport and other mailer transport options (see #2554) · contao/core-bundle@135377d · GitHub
[go: up one dir, main page]

Skip to content

Commit 135377d

Browse files
fritzmgleofeyer
andauthored
Support native transport and other mailer transport options (see #2554)
Description ----------- | Q | A | -----------------| --- | Fixed issues | Fixes #2164 | Docs PR or issue | - This PR adds support for the `native://default` mailer transport, which uses the PHP `sendmail_path` configuration. Some notes: * This feature is available in Symfony `5.2.0` and up. Since the `symfony/mailer` dependency is independent from other Symfony packages, it can be allowed to be installed, while everything else stays at Symfony `4.4.*`. * Since the actual transport factory for the native transport is however defined by the `symfony/framework-bundle`, an additional compiler pass is necessary, to add said transport factory to the container, if supported and not already present. This can be removed once the core requires at least Symfony `5.2.*`. * Changing the `symfony/mailer` dependency to include `5.2.*` also allows you to use [additional options](symfony/symfony#37432) for your transport (like a local domain for an SMTP relay or a custom sendmail command). It would be important to include this in the upcoming Contao 4.11 version, as otherwise there exists no possibility of using a custom sendmail command and thus you are stuck with the hardcoded one. Commits ------- 5ea20a71 support native transport 53836ae6 use class_exists 3bbd552f update ContaoCoreBundleTest b084330c check for native mailer support in PluginTest too 4df690a8 update AddNativeTransportFactoryPassTest for prefer-lowest 72e72d27 Apply suggestions from code review Co-authored-by: Leo Feyer <github@contao.org> ad36b75c merge with master e98b617f removed superfluous function 1969b8e1 fix merge error 9fbdaf8e CS 49187f05 Merge branch 'master' into use-native-mailer
1 parent cc4744c commit 135377d

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

src/ContaoCoreBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Contao\CoreBundle\DependencyInjection\Compiler\AddAssetsPackagesPass;
1616
use Contao\CoreBundle\DependencyInjection\Compiler\AddAvailableTransportsPass;
1717
use Contao\CoreBundle\DependencyInjection\Compiler\AddCronJobsPass;
18+
use Contao\CoreBundle\DependencyInjection\Compiler\AddNativeTransportFactoryPass;
1819
use Contao\CoreBundle\DependencyInjection\Compiler\AddPackagesPass;
1920
use Contao\CoreBundle\DependencyInjection\Compiler\AddResourcesPathsPass;
2021
use Contao\CoreBundle\DependencyInjection\Compiler\AddSessionBagsPass;
@@ -112,5 +113,6 @@ public function build(ContainerBuilder $container): void
112113
$container->addCompilerPass(new AddCronJobsPass());
113114
$container->addCompilerPass(new AddAvailableTransportsPass());
114115
$container->addCompilerPass(new RegisterRouteEnhancersPass('contao.routing.page_router', 'contao.page_router_enhancer'));
116+
$container->addCompilerPass(new AddNativeTransportFactoryPass());
115117
}
116118
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Contao.
7+
*
8+
* (c) Leo Feyer
9+
*
10+
* @license LGPL-3.0-or-later
11+
*/
12+
13+
namespace Contao\CoreBundle\DependencyInjection\Compiler;
14+
15+
use Symfony\Component\DependencyInjection\ChildDefinition;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\Mailer\Transport\NativeTransportFactory;
19+
20+
/**
21+
* @internal
22+
*/
23+
class AddNativeTransportFactoryPass implements CompilerPassInterface
24+
{
25+
public function process(ContainerBuilder $container): void
26+
{
27+
if ($container->hasDefinition('mailer.transport_factory.native') || !class_exists(NativeTransportFactory::class)) {
28+
return;
29+
}
30+
31+
$definition = new ChildDefinition('mailer.transport_factory.abstract');
32+
$definition
33+
->setClass(NativeTransportFactory::class)
34+
->addTag('mailer.transport_factory')
35+
;
36+
37+
$container->setDefinition('mailer.transport_factory.native', $definition);
38+
}
39+
}

tests/ContaoCoreBundleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Contao\CoreBundle\DependencyInjection\Compiler\AddAssetsPackagesPass;
1717
use Contao\CoreBundle\DependencyInjection\Compiler\AddAvailableTransportsPass;
1818
use Contao\CoreBundle\DependencyInjection\Compiler\AddCronJobsPass;
19+
use Contao\CoreBundle\DependencyInjection\Compiler\AddNativeTransportFactoryPass;
1920
use Contao\CoreBundle\DependencyInjection\Compiler\AddPackagesPass;
2021
use Contao\CoreBundle\DependencyInjection\Compiler\AddResourcesPathsPass;
2122
use Contao\CoreBundle\DependencyInjection\Compiler\AddSessionBagsPass;
@@ -71,6 +72,7 @@ public function testAddsTheCompilerPasses(): void
7172
AddCronJobsPass::class,
7273
AddAvailableTransportsPass::class,
7374
RegisterRouteEnhancersPass::class,
75+
AddNativeTransportFactoryPass::class,
7476
];
7577

7678
$security = $this->createMock(SecurityExtension::class);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Contao.
7+
*
8+
* (c) Leo Feyer
9+
*
10+
* @license LGPL-3.0-or-later
11+
*/
12+
13+
namespace Contao\CoreBundle\Tests\DependencyInjection\Compiler;
14+
15+
use Contao\CoreBundle\DependencyInjection\Compiler\AddNativeTransportFactoryPass;
16+
use Contao\CoreBundle\Tests\TestCase;
17+
use Symfony\Component\DependencyInjection\ChildDefinition;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\Mailer\Transport\NativeTransportFactory;
20+
21+
class AddNativeTransportFactoryPassTest extends TestCase
22+
{
23+
public function testAddsTheNativeTransportFactoryIfSupported(): void
24+
{
25+
$container = new ContainerBuilder();
26+
27+
$pass = new AddNativeTransportFactoryPass();
28+
$pass->process($container);
29+
30+
if (class_exists(NativeTransportFactory::class)) {
31+
$this->assertTrue($container->hasDefinition('mailer.transport_factory.native'));
32+
33+
/** @var ChildDefinition $definition */
34+
$definition = $container->getDefinition('mailer.transport_factory.native');
35+
36+
$this->assertTrue($definition->hasTag('mailer.transport_factory'));
37+
$this->assertSame('mailer.transport_factory.abstract', $definition->getParent());
38+
} else {
39+
$this->assertFalse($container->hasDefinition('mailer.transport_factory.native'));
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)
0