From 7ecbc9e52d4f410ba08e82aea1adccb48ff3f23f Mon Sep 17 00:00:00 2001 From: Anthony Bocci Date: Sat, 18 May 2019 20:39:12 +0200 Subject: [PATCH 1/2] Add support for headers in native mailer The native mailer is able to have additional headers, but it wasn't possible to give it any headers in the configuration. Swift mailer may have a content_type key but not native mailer. A new "headers" key is now allowed in the handler configuration, so a list of headers may be given to the handler. Only native mailer supports it. Related to #272 --- DependencyInjection/Configuration.php | 5 +++++ DependencyInjection/MonologExtension.php | 3 +++ Resources/config/schema/monolog-1.0.xsd | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 48d789a5..d49964b1 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -175,6 +175,7 @@ * - subject: string * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true + * - [headers]: optional array containing additional headers * * - socket: * - connection_string: string @@ -594,6 +595,10 @@ public function getConfigTreeBuilder() ->end() ->scalarNode('subject')->end() // swift_mailer and native_mailer ->scalarNode('content_type')->defaultNull()->end() // swift_mailer + ->arrayNode('headers') // native_mailer + ->canBeUnset() + ->scalarPrototype()->end() + ->end() ->scalarNode('mailer')->defaultValue('mailer')->end() // swift_mailer ->arrayNode('email_prototype') // swift_mailer ->canBeUnset() diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 285e9b81..500a041b 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -540,6 +540,9 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['level'], $handler['bubble'], )); + if (!empty($handler['headers'])) { + $definition->addMethodCall('addHeader', $handler['headers']); + } break; case 'socket': diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index cf0eac87..6c23e93a 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -88,6 +88,7 @@ + @@ -186,4 +187,10 @@ + + + + + + From 19eaa0f1b45d72e944535cbda7935da461b9b7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 12 Jun 2019 10:52:59 +0200 Subject: [PATCH 2/2] Fix 'Add support for headers in native mailer' --- DependencyInjection/Configuration.php | 3 ++- DependencyInjection/MonologExtension.php | 2 +- Resources/config/schema/monolog-1.0.xsd | 8 ++++---- .../FixtureMonologExtensionTest.php | 11 +++++++++++ .../Fixtures/xml/native_mailer.xml | 15 +++++++++++++++ .../Fixtures/yml/native_mailer.yml | 10 ++++++++++ 6 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 Tests/DependencyInjection/Fixtures/xml/native_mailer.xml create mode 100644 Tests/DependencyInjection/Fixtures/yml/native_mailer.yml diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index d49964b1..388d1ab0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -175,7 +175,7 @@ * - subject: string * - [level]: level name or int value, defaults to DEBUG * - [bubble]: bool, defaults to true - * - [headers]: optional array containing additional headers + * - [headers]: optional array containing additional headers: ['Foo: Bar', '...'] * * - socket: * - connection_string: string @@ -348,6 +348,7 @@ public function getConfigTreeBuilder() ->fixXmlConfig('excluded_http_code') ->fixXmlConfig('tag') ->fixXmlConfig('accepted_level') + ->fixXmlConfig('header') ->canBeUnset() ->children() ->scalarNode('type') diff --git a/DependencyInjection/MonologExtension.php b/DependencyInjection/MonologExtension.php index 500a041b..ac1866aa 100644 --- a/DependencyInjection/MonologExtension.php +++ b/DependencyInjection/MonologExtension.php @@ -541,7 +541,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler $handler['bubble'], )); if (!empty($handler['headers'])) { - $definition->addMethodCall('addHeader', $handler['headers']); + $definition->addMethodCall('addHeader', [$handler['headers']]); } break; diff --git a/Resources/config/schema/monolog-1.0.xsd b/Resources/config/schema/monolog-1.0.xsd index 6c23e93a..38a6bba9 100644 --- a/Resources/config/schema/monolog-1.0.xsd +++ b/Resources/config/schema/monolog-1.0.xsd @@ -28,6 +28,7 @@ + @@ -88,7 +89,6 @@ - @@ -189,8 +189,8 @@ - - - + + + diff --git a/Tests/DependencyInjection/FixtureMonologExtensionTest.php b/Tests/DependencyInjection/FixtureMonologExtensionTest.php index 23c0cfd7..9a40ee3c 100644 --- a/Tests/DependencyInjection/FixtureMonologExtensionTest.php +++ b/Tests/DependencyInjection/FixtureMonologExtensionTest.php @@ -216,6 +216,17 @@ public function testPsr3MessageProcessingDisabled() $this->assertNotContains(array('pushProcessor', array(new Reference('monolog.processor.psr_log_message'))), $methodCalls, 'The PSR-3 processor should not be enabled', false, false); } + public function testNativeMailer() + { + $container = $this->getContainer('native_mailer'); + + $logger = $container->getDefinition('monolog.handler.mailer'); + $methodCalls = $logger->getMethodCalls(); + + $this->assertCount(2, $methodCalls); + $this->assertSame(['addHeader', [['Foo: bar', 'Baz: inga']]], $methodCalls[1]); + } + protected function getContainer($fixture) { $container = new ContainerBuilder(); diff --git a/Tests/DependencyInjection/Fixtures/xml/native_mailer.xml b/Tests/DependencyInjection/Fixtures/xml/native_mailer.xml new file mode 100644 index 00000000..8c3e8242 --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/xml/native_mailer.xml @@ -0,0 +1,15 @@ + + + + + + +
Foo: bar
+
Baz: inga
+
+
+
diff --git a/Tests/DependencyInjection/Fixtures/yml/native_mailer.yml b/Tests/DependencyInjection/Fixtures/yml/native_mailer.yml new file mode 100644 index 00000000..4950a38f --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/yml/native_mailer.yml @@ -0,0 +1,10 @@ +monolog: + handlers: + mailer: + type: native_mailer + from_email: foo@example.com + to_email: bar@exemple.com + subject: a subject + headers: + - "Foo: bar" + - "Baz: inga"