diff --git a/src/Core/Content/Mail/Service/MailerTransportFactory.php b/src/Core/Content/Mail/Service/MailerTransportFactory.php index 6b4ccac68d9..72400df9e78 100644 --- a/src/Core/Content/Mail/Service/MailerTransportFactory.php +++ b/src/Core/Content/Mail/Service/MailerTransportFactory.php @@ -53,7 +53,7 @@ public function create(?SystemConfigService $configService = null): TransportInt case 'smtp': return $this->createSmtpTransport($configService); case 'local': - return new SendmailTransport('/usr/sbin/sendmail ' . ($configService->getString('core.mailerSettings.sendMailOptions') ?: '-bs')); + return new SendmailTransport($this->getSendMailCommandLineArgument($configService)); default: throw new \RuntimeException(sprintf('Invalid mail agent given "%s"', $emailAgent)); } @@ -86,4 +86,21 @@ private function getEncryption(SystemConfigService $configService): ?string return null; } } + + private function getSendMailCommandLineArgument(SystemConfigService $configService): string + { + $command = '/usr/sbin/sendmail '; + + $option = $configService->getString('core.mailerSettings.sendMailOptions'); + + if ($option === '') { + $option = '-t'; + } + + if ($option !== '-bs' && $option !== '-t') { + throw new \RuntimeException(sprintf('Given sendmail option "%s" is invalid', $option)); + } + + return $command . $option; + } } diff --git a/src/Core/Content/Test/MailTemplate/Service/MailerTransportFactoryTest.php b/src/Core/Content/Test/MailTemplate/Service/MailerTransportFactoryTest.php index 013cb15ff64..8fbb9a20995 100644 --- a/src/Core/Content/Test/MailTemplate/Service/MailerTransportFactoryTest.php +++ b/src/Core/Content/Test/MailTemplate/Service/MailerTransportFactoryTest.php @@ -70,6 +70,25 @@ public function testFactoryWithConfig(): void static::assertEquals(\get_class($original), \get_class($mailer)); } + + public function testFactoryWithLocalAndInvalidConfig(): void + { + $original = new SendmailTransport(); + + $factory = $this->getContainer()->get('mailer.transport_factory'); + + static::expectException(\RuntimeException::class); + static::expectExceptionMessage('Given sendmail option "-t && echo bla" is invalid'); + + $mailer = $factory->create( + new ConfigService([ + 'core.mailerSettings.emailAgent' => 'local', + 'core.mailerSettings.sendMailOptions' => '-t && echo bla', + ]) + ); + + static::assertEquals(\get_class($original), \get_class($mailer)); + } } class ConfigService extends SystemConfigService