8000 [Mailer] Add support for allowing some users even if `recipients` is … · symfony/symfony@83cc068 · GitHub
[go: up one dir, main page]

Skip to content

Commit 83cc068

Browse files
committed
[Mailer] Add support for allowing some users even if recipients is defined in EnvelopeListener
1 parent f78f932 commit 83cc068

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,6 +2126,15 @@ private function addMailerSection(ArrayNodeDefinition $rootNode, callable $enabl
21262126
->end()
21272127
->prototype('scalar')->end()
21282128
->end()
2129+
->arrayNode('recipients_allowed')
2130+
->info('A list of regular expressions that allow recipients when "recipients" option is defined.')
2131+
->performNoDeepMerging()
2132+
->beforeNormalization()
2133+
->ifArray()
2134+
->then(fn ($v) => array_filter(array_values($v)))
2135+
->end()
2136+
->prototype('scalar')->end()
2137+
->end()
21292138
->end()
21302139
->end()
21312140
->arrayNode('headers')

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2669,6 +2669,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
26692669
$envelopeListener = $container->getDefinition('mailer.envelope_listener');
26702670
$envelopeListener->setArgument(0, $config['envelope']['sender'] ?? null);
26712671
$envelopeListener->setArgument(1, $config['envelope']['recipients'] ?? null);
2672+
$envelopeListener->setArgument(2, $config['envelope']['recipients_allowed'] ?? []);
26722673

26732674
if ($config['headers']) {
26742675
$headers = new Definition(Headers::class);

src/Symfony/Component/Mailer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Dispatch Postmark's "406 - Inactive recipient" API error code as a `PostmarkDeliveryEvent` instead of throwing an exception
88
* Add DSN param `auto_tls` to disable automatic STARTTLS
9+
* Add support for allowing some users even if `recipients` is defined in `EnvelopeListener`
910

1011
7.0
1112
---

src/Symfony/Component/Mailer/EventListener/EnvelopeListener.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Manipulates the Envelope of a Message.
2121
*
2222
* @author Fabien Potencier <fabien@symfony.com>
23+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2324
*/
2425
class EnvelopeListener implements EventSubscriberInterface
2526
{
@@ -30,17 +31,25 @@ class EnvelopeListener implements EventSubscriberInterface
3031
*/
3132
private ?array $recipients = null;
3233

34+
/**
35+
* @var string[]
36+
*/
37+
private array $allowedRecipients;
38+
3339
/**
3440
* @param array<Address|string> $recipients
41+
* @param string[] $allowedRecipients An array of regex to match the allowed recipients
3542
*/
36-
public function __construct(Address|string|null $sender = null, ?array $recipients = null)
43+
public function __construct(Address|string|null $sender = null, ?array $recipients = null, array $allowedRecipients = [])
3744
{
3845
if (null !== $sender) {
3946
$this->sender = Address::create($sender);
4047
}
4148
if (null !== $recipients) {
4249
$this->recipients = Address::createArray($recipients);
4350
}
51+
52+
$this->allowedRecipients = $allowedRecipients;
4453
}
4554

4655
public function onMessage(MessageEvent $event): void
@@ -57,7 +66,24 @@ public function onMessage(MessageEvent $event): void
5766
}
5867

5968
if ($this->recipients) {
60-
$event->getEnvelope()->setRecipients($this->recipients);
69+
$recipients = $this->recipients;
70+
if ($this->allowedRecipients) {
71+
$recipients = [];
72+
$addDefaults = false;
73+
foreach ($event->getEnvelope()->getRecipients() as $recipient) {
74+
foreach ($this->allowedRecipients as $allowedRecipient) {
75+
if (preg_match($allowedRecipient, $recipient->getAddress())) {
76+
$recipients[] = $recipient;
77+
continue 2;
78+
}
79+
}
80+
$addDefaults = true;
81+
}
82+
if ($addDefaults) {
83+
$recipients = array_merge($recipients, $this->recipients);
84+
}
85+
}
86+
$event->getEnvelope()->setRecipients($recipients);
6187
}
6288
}
6389

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Tests\EventListener;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Mailer\Envelope;
16+
use Symfony\Component\Mailer\Event\MessageEvent;
17+
use Symfony\Component\Mailer\EventListener\EnvelopeListener;
18+
use Symfony\Component\Mime\Address;
19+
use Symfony\Component\Mime\RawMessage;
20+
21+
class EnvelopeListenerTest extends TestCase
22+
{
23+
/**
24+
* @dataProvider provideRecipientsTests
25+
*/
26+
public function testRecipients(array $expected, ?array $recipients = null, array $allowedRecipients = [])
27+
{
28+
$listener = new EnvelopeListener(null, $recipients, $allowedRecipients);
29+
$message = new RawMessage('message');
30+
$envelope = new Envelope(new Address('sender@example.com'), [new Address('r1@example.com'), new Address('r2@symfony.com')]);
31+
$event = new MessageEvent($message, $envelope, 'default');
32+
33+
$listener->onMessage($event);
34+
35+
$recipients = array_map(fn (Address $a): string => $a->getAddress(), $event->getEnvelope()->getRecipients());
36+
$this->assertSame($expected, $recipients);
37+
}
38+
39+
public static function provideRecipientsTests(): iterable
40+
{
41+
yield [['r1@example.com', 'r2@symfony.com'], null, []];
42+
yield [['admin@admin.com'], ['admin@admin.com'], []];
43+
yield [['r1@example.com', 'admin@admin.com'], ['admin@admin.com'], ['/@example.com$/']];
44+
yield [['r1@example.com', 'r2@symfony.com'], ['admin@admin.com'], ['/@example.com$/', '/@symfony.com$/']];
45+
}
46+
}

0 commit comments

Comments
 (0)
0