Skip to content

Commit

Permalink
Add Symfony Mailer support
Browse files Browse the repository at this point in the history
  • Loading branch information
acasademont authored and lyrixx committed Oct 2, 2020
1 parent f27c499 commit 4bea89c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
25 changes: 19 additions & 6 deletions DependencyInjection/Configuration.php
Expand Up @@ -177,6 +177,15 @@
* - [bubble]: bool, defaults to true
* - [headers]: optional array containing additional headers: ['Foo: Bar', '...']
*
* - symfony_mailer:
* - from_email: optional if email_prototype is given
* - to_email: optional if email_prototype is given
* - subject: optional if email_prototype is given
* - [email_prototype]: service id of a message, defaults to a default message with the three fields above
* - [mailer]: mailer service id, defaults to mailer.mailer
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - socket:
* - connection_string: string
* - [timeout]: float
Expand Down Expand Up @@ -595,22 +604,22 @@ public function getConfigTreeBuilder()
->performNoDeepMerging()
->prototype('scalar')->end()
->end()
->scalarNode('from_email')->end() // swift_mailer, native_mailer and flowdock
->arrayNode('to_email') // swift_mailer and native_mailer
->scalarNode('from_email')->end() // swift_mailer, native_mailer, symfony_mailer and flowdock
->arrayNode('to_email') // swift_mailer, native_mailer and symfony_mailer
->prototype('scalar')->end()
->beforeNormalization()
->ifString()
->then(function ($v) { return [$v]; })
->end()
->end()
->scalarNode('subject')->end() // swift_mailer and native_mailer
->scalarNode('content_type')->defaultNull()->end() // swift_mailer
->scalarNode('subject')->end() // swift_mailer, native_mailer and symfony_mailer
->scalarNode('content_type')->defaultNull()->end() // swift_mailer and symfony_mailer
->arrayNode('headers') // native_mailer
->canBeUnset()
->scalarPrototype()->end()
->end()
->scalarNode('mailer')->defaultValue('mailer')->end() // swift_mailer
->arrayNode('email_prototype') // swift_mailer
->scalarNode('mailer')->defaultNull()->end() // swift_mailer and symfony_mailer
->arrayNode('email_prototype') // swift_mailer and symfony_mailer
->canBeUnset()
->beforeNormalization()
->ifString()
Expand Down Expand Up @@ -822,6 +831,10 @@ public function getConfigTreeBuilder()
->ifTrue(function ($v) { return 'native_mailer' === $v['type'] && (empty($v['from_email']) || empty($v['to_email']) || empty($v['subject'])); })
->thenInvalid('The sender, recipient and subject have to be specified to use a NativeMailerHandler')
->end()
->validate()
->ifTrue(function ($v) { return 'symfony_mailer' === $v['type'] && empty($v['email_prototype']) && (empty($v['from_email']) || empty($v['to_email']) || empty($v['subject'])); })
->thenInvalid('The sender, recipient and subject or an email prototype have to be specified to use the Symfony MailerHandler')
->end()
->validate()
->ifTrue(function ($v) { return 'service' === $v['type'] && !isset($v['id']); })
->thenInvalid('The id has to be specified to use a service as handler')
Expand Down
29 changes: 27 additions & 2 deletions DependencyInjection/MonologExtension.php
Expand Up @@ -514,6 +514,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
break;

case 'swift_mailer':
$mailer = $handler['mailer'] ?: 'mailer';
if (isset($handler['email_prototype'])) {
if (!empty($handler['email_prototype']['method'])) {
$prototype = [new Reference($handler['email_prototype']['id']), $handler['email_prototype']['method']];
Expand All @@ -525,7 +526,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$messageFactory->setLazy(true);
$messageFactory->setPublic(false);
$messageFactory->setArguments([
new Reference($handler['mailer']),
new Reference($mailer),
$handler['from_email'],
$handler['to_email'],
$handler['subject'],
Expand All @@ -538,7 +539,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
$prototype = [new Reference($messageFactoryId), 'createMessage'];
}
$definition->setArguments([
new Reference($handler['mailer']),
new Reference($mailer),
$prototype,
$handler['level'],
$handler['bubble'],
Expand All @@ -562,6 +563,29 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
}
break;

case 'symfony_mailer':
$mailer = $handler['mailer'] ?: 'mailer.mailer';
if (isset($handler['email_prototype'])) {
if (!empty($handler['email_prototype']['method'])) {
$prototype = [new Reference($handler['email_prototype']['id']), $handler['email_prototype']['method']];
} else {
$prototype = new Reference($handler['email_prototype']['id']);
}
} else {
$prototype = (new Definition('Symfony\Component\Mime\Email'))
->setPublic(false)
->addMethodCall('from', [$handler['from_email']])
->addMethodCall('to', $handler['to_email'])
->addMethodCall('subject', [$handler['subject']]);
}
$definition->setArguments([
new Reference($mailer),
$prototype,
$handler['level'],
$handler['bubble'],
]);
break;

case 'socket':
$definition->setArguments([
$handler['connection_string'],
Expand Down Expand Up @@ -923,6 +947,7 @@ private function getHandlerClassByType($handlerType)
'debug' => 'Symfony\Bridge\Monolog\Handler\DebugHandler',
'swift_mailer' => 'Symfony\Bridge\Monolog\Handler\SwiftMailerHandler',
'native_mailer' => 'Monolog\Handler\NativeMailerHandler',
'symfony_mailer' => 'Symfony\Bridge\Monolog\Handler\MailerHandler',
'socket' => 'Monolog\Handler\SocketHandler',
'pushover' => 'Monolog\Handler\PushoverHandler',
'raven' => 'Monolog\Handler\RavenHandler',
Expand Down

0 comments on commit 4bea89c

Please sign in to comment.