Skip to content

Commit

Permalink
feature #304 Add support for headers in native mailer (anthonybocci, …
Browse files Browse the repository at this point in the history
…lyrixx)

This PR was merged into the 3.x-dev branch.

Discussion
----------

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

Commits
-------

19eaa0f Fix 'Add support for headers in native mailer'
7ecbc9e Add support for headers in native mailer
  • Loading branch information
lyrixx committed Jun 20, 2019
2 parents 5fe15c2 + 19eaa0f commit ed0ada6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -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: ['Foo: Bar', '...']
*
* - socket:
* - connection_string: string
Expand Down Expand Up @@ -347,6 +348,7 @@ public function getConfigTreeBuilder()
->fixXmlConfig('excluded_http_code')
->fixXmlConfig('tag')
->fixXmlConfig('accepted_level')
->fixXmlConfig('header')
->canBeUnset()
->children()
->scalarNode('type')
Expand Down Expand Up @@ -594,6 +596,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()
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/MonologExtension.php
Expand Up @@ -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':
Expand Down
7 changes: 7 additions & 0 deletions Resources/config/schema/monolog-1.0.xsd
Expand Up @@ -28,6 +28,7 @@
<xsd:element name="tag" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="accepted-level" type="level" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="to-email" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="header" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="priority" type="xsd:integer" />
Expand Down Expand Up @@ -186,4 +187,10 @@
</xsd:choice>
<xsd:attribute name="code" type="xsd:integer" />
</xsd:complexType>

<xsd:complexType name="headers">
<xsd:sequence>
<xsd:any minOccurs="0" processContents="lax"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
11 changes: 11 additions & 0 deletions Tests/DependencyInjection/FixtureMonologExtensionTest.php
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions Tests/DependencyInjection/Fixtures/xml/native_mailer.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>

<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<config>
<handler name="mailer" type="native_mailer" to-email="foo@example.com" from-email="bar@example.com" subject="a subject">
<header>Foo: bar</header>
<header>Baz: inga</header>
</handler>
</config>
</srv:container>
10 changes: 10 additions & 0 deletions 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"

0 comments on commit ed0ada6

Please sign in to comment.