Skip to content

Commit 24f27fb

Browse files
committed
feature #354 Add Symfony Mailer support (acasademont)
This PR was squashed before being merged into the 3.x-dev branch. Discussion ---------- Add Symfony Mailer support Add support for the new symfony mailer directly on the bundle after the MailerHandle was added to the bridge on symfony/symfony#33456 Also this can make symfony/symfony-docs#13077 move forward Tests are failing due to incompatible phpunit versions, not sure how to fix this... Commits ------- 4bea89c Add Symfony Mailer support
2 parents f27c499 + 4bea89c commit 24f27fb

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

DependencyInjection/Configuration.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@
177177
* - [bubble]: bool, defaults to true
178178
* - [headers]: optional array containing additional headers: ['Foo: Bar', '...']
179179
*
180+
* - symfony_mailer:
181+
* - from_email: optional if email_prototype is given
182+
* - to_email: optional if email_prototype is given
183+
* - subject: optional if email_prototype is given
184+
* - [email_prototype]: service id of a message, defaults to a default message with the three fields above
185+
* - [mailer]: mailer service id, defaults to mailer.mailer
186+
* - [level]: level name or int value, defaults to DEBUG
187+
* - [bubble]: bool, defaults to true
188+
*
180189
* - socket:
181190
* - connection_string: string
182191
* - [timeout]: float
@@ -595,22 +604,22 @@ public function getConfigTreeBuilder()
595604
->performNoDeepMerging()
596605
->prototype('scalar')->end()
597606
->end()
598-
->scalarNode('from_email')->end() // swift_mailer, native_mailer and flowdock
599-
->arrayNode('to_email') // swift_mailer and native_mailer
607+
->scalarNode('from_email')->end() // swift_mailer, native_mailer, symfony_mailer and flowdock
608+
->arrayNode('to_email') // swift_mailer, native_mailer and symfony_mailer
600609
->prototype('scalar')->end()
601610
->beforeNormalization()
602611
->ifString()
603612
->then(function ($v) { return [$v]; })
604613
->end()
605614
->end()
606-
->scalarNode('subject')->end() // swift_mailer and native_mailer
607-
->scalarNode('content_type')->defaultNull()->end() // swift_mailer
615+
->scalarNode('subject')->end() // swift_mailer, native_mailer and symfony_mailer
616+
->scalarNode('content_type')->defaultNull()->end() // swift_mailer and symfony_mailer
608617
->arrayNode('headers') // native_mailer
609618
->canBeUnset()
610619
->scalarPrototype()->end()
611620
->end()
612-
->scalarNode('mailer')->defaultValue('mailer')->end() // swift_mailer
613-
->arrayNode('email_prototype') // swift_mailer
621+
->scalarNode('mailer')->defaultNull()->end() // swift_mailer and symfony_mailer
622+
->arrayNode('email_prototype') // swift_mailer and symfony_mailer
614623
->canBeUnset()
615624
->beforeNormalization()
616625
->ifString()
@@ -822,6 +831,10 @@ public function getConfigTreeBuilder()
822831
->ifTrue(function ($v) { return 'native_mailer' === $v['type'] && (empty($v['from_email']) || empty($v['to_email']) || empty($v['subject'])); })
823832
->thenInvalid('The sender, recipient and subject have to be specified to use a NativeMailerHandler')
824833
->end()
834+
->validate()
835+
->ifTrue(function ($v) { return 'symfony_mailer' === $v['type'] && empty($v['email_prototype']) && (empty($v['from_email']) || empty($v['to_email']) || empty($v['subject'])); })
836+
->thenInvalid('The sender, recipient and subject or an email prototype have to be specified to use the Symfony MailerHandler')
837+
->end()
825838
->validate()
826839
->ifTrue(function ($v) { return 'service' === $v['type'] && !isset($v['id']); })
827840
->thenInvalid('The id has to be specified to use a service as handler')

DependencyInjection/MonologExtension.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
514514
break;
515515

516516
case 'swift_mailer':
517+
$mailer = $handler['mailer'] ?: 'mailer';
517518
if (isset($handler['email_prototype'])) {
518519
if (!empty($handler['email_prototype']['method'])) {
519520
$prototype = [new Reference($handler['email_prototype']['id']), $handler['email_prototype']['method']];
@@ -525,7 +526,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
525526
$messageFactory->setLazy(true);
526527
$messageFactory->setPublic(false);
527528
$messageFactory->setArguments([
528-
new Reference($handler['mailer']),
529+
new Reference($mailer),
529530
$handler['from_email'],
530531
$handler['to_email'],
531532
$handler['subject'],
@@ -538,7 +539,7 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
538539
$prototype = [new Reference($messageFactoryId), 'createMessage'];
539540
}
540541
$definition->setArguments([
541-
new Reference($handler['mailer']),
542+
new Reference($mailer),
542543
$prototype,
543544
$handler['level'],
544545
$handler['bubble'],
@@ -562,6 +563,29 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
562563
}
563564
break;
564565

566+
case 'symfony_mailer':
567+
$mailer = $handler['mailer'] ?: 'mailer.mailer';
568+
if (isset($handler['email_prototype'])) {
569+
if (!empty($handler['email_prototype']['method'])) {
570+
$prototype = [new Reference($handler['email_prototype']['id']), $handler['email_prototype']['method']];
571+
} else {
572+
$prototype = new Reference($handler['email_prototype']['id']);
573+
}
574+
} else {
575+
$prototype = (new Definition('Symfony\Component\Mime\Email'))
576+
->setPublic(false)
577+
->addMethodCall('from', [$handler['from_email']])
578+
->addMethodCall('to', $handler['to_email'])
579+
->addMethodCall('subject', [$handler['subject']]);
580+
}
581+
$definition->setArguments([
582+
new Reference($mailer),
583+
$prototype,
584+
$handler['level'],
585+
$handler['bubble'],
586+
]);
587+
break;
588+
565589
case 'socket':
566590
$definition->setArguments([
567591
$handler['connection_string'],
@@ -923,6 +947,7 @@ private function getHandlerClassByType($handlerType)
923947
'debug' => 'Symfony\Bridge\Monolog\Handler\DebugHandler',
924948
'swift_mailer' => 'Symfony\Bridge\Monolog\Handler\SwiftMailerHandler',
925949
'native_mailer' => 'Monolog\Handler\NativeMailerHandler',
950+
'symfony_mailer' => 'Symfony\Bridge\Monolog\Handler\MailerHandler',
926951
'socket' => 'Monolog\Handler\SocketHandler',
927952
'pushover' => 'Monolog\Handler\PushoverHandler',
928953
'raven' => 'Monolog\Handler\RavenHandler',

0 commit comments

Comments
 (0)