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

Add support for headers in native mailer #304

Merged
merged 2 commits into from Jun 20, 2019
Merged
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
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"