Skip to content
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

Automatically use the configured sendmail_path #2236

Closed
wants to merge 4 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion manager-bundle/composer.json
Expand Up @@ -50,7 +50,7 @@
"symfony/routing": "4.4.*",
"symfony/security-bundle": "4.4.*",
"symfony/stopwatch": "4.4.*",
"symfony/swiftmailer-bundle": "^2.6.2 || ^3.1.5",
"symfony/swiftmailer-bundle": "^3.2.8",
"symfony/twig-bundle": "4.4.*",
"symfony/web-profiler-bundle": "4.4.*",
"symfony/yaml": "4.4.*",
Expand Down
25 changes: 25 additions & 0 deletions manager-bundle/src/ContaoManager/Plugin.php
Expand Up @@ -245,6 +245,7 @@ public function getExtensionConfig($extensionName, array $extensionConfigs, Plug

case 'swiftmailer':
$extensionConfigs = $this->checkMailerTransport($extensionConfigs, $container);
$extensionConfigs = $this->setSendmailCommand($extensionConfigs);

if (!isset($_SERVER['MAILER_URL'])) {
$container->setParameter('env(MAILER_URL)', $this->getMailerUrl($container));
Expand Down Expand Up @@ -372,6 +373,30 @@ private function getDatabaseUrl(ContainerBuilder $container): string
);
}

/**
* Sets the sendmail command for Swiftmailer to the sendmail_path defined by PHP.
*
* @return array<string,array<string,array<string,array<string,mixed>>>>
*/
private function setSendmailCommand(array $extensionConfigs): array
{
$sendmailPath = @ini_get('sendmail_path');

if (!$sendmailPath) {
return $extensionConfigs;
}

foreach ($extensionConfigs as $extensionConfig) {
if (isset($extensionConfig['command']) || isset($extensionConfig['mailers']['default']['command'])) {
return $extensionConfigs;
}
}

$extensionConfigs[] = ['command' => $sendmailPath];

return $extensionConfigs;
}

private function getMailerUrl(ContainerBuilder $container): string
{
if ('sendmail' === $container->getParameter('mailer_transport')) {
Expand Down
40 changes: 40 additions & 0 deletions manager-bundle/tests/ContaoManager/PluginTest.php
Expand Up @@ -570,6 +570,46 @@ public function getMailerParameters(): \Generator
];
}

public function testSetsTheSendmailCommand(): void
{
$container = $this->getContainer();

$extensionConfig = (new Plugin())->getExtensionConfig('swiftmailer', [], $container);

$this->assertTrue(!ini_get('sendmail_path') || isset($extensionConfig[0]['command']));
}

public function testDoesNotSetTheSendmailCommandIfAlreadyDefined(): void
{
$container = $this->getContainer();

$extensionConfigs = [[
'command' => '/foobar/sendmail -t',
]];

$expect = $extensionConfigs;

$extensionConfig = (new Plugin())->getExtensionConfig('swiftmailer', $extensionConfigs, $container);

$this->assertSame($expect, $extensionConfig);

$extensionConfigs = [
[
'mailers' => [
'default' => [
'command' => '/foobar/sendmail -t',
],
],
],
];

$expect = $extensionConfigs;

$extensionConfig = (new Plugin())->getExtensionConfig('swiftmailer', $extensionConfigs, $container);

$this->assertSame($expect, $extensionConfig);
}

public function testRetrievesTheConnectionParametersFromTheConfiguration(): void
{
$pluginLoader = $this->createMock(PluginLoader::class);
Expand Down