Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82ab4e4

Browse files
committed
[Mailer] Add support for allowing some users even if recipients is defined in EnvelopeListener
1 parent 9e810de commit 82ab4e4

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed

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

+9
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

+1
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

+1
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

+27-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Manipulates the Envelope of a Message.
2121
*
2222
* @author Fabien Potencier <[email protected]>
23+
* @author Grégoire Pineau <[email protected]>
2324
*/
2425
class EnvelopeListener implements EventSubscriberInterface
2526
{
@@ -32,15 +33,21 @@ class EnvelopeListener implements EventSubscriberInterface
3233

3334
/**
3435
* @param array<Address|string> $recipients
36+
* @param string[] $allowedRecipients An array of regex to match the allowed recipients
3537
*/
36-
public function __construct(Address|string|null $sender = null, ?array $recipients = null)
37-
{
38+
public function __construct(
39+
Address|string|null $sender = null,
40+
?array $recipients = null,
41+
private array $allowedRecipients = [],
42+
) {
3843
if (null !== $sender) {
3944
$this->sender = Address::create($sender);
4045
}
4146
if (null !== $recipients) {
4247
$this->recipients = Address::createArray($recipients);
4348
}
49+
50+
$this->allowedRecipients = $allowedRecipients;
4451
}
4552

4653
public function onMessage(MessageEvent $event): void
@@ -57,7 +64,24 @@ public function onMessage(MessageEvent $event): void
5764
}
5865

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

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 <[email protected]>
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('[email protected]'), [new Address('[email protected]'), new Address('[email protected]')]);
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 [['[email protected]', '[email protected]'], null, []];
42+
yield [['[email protected]'], ['[email protected]'], []];
43+
yield [['[email protected]', '[email protected]'], ['[email protected]'], ['/@example.com$/']];
44+
yield [['[email protected]', '[email protected]'], ['[email protected]'], ['/@example.com$/', '/@symfony.com$/']];
45+
}
46+
}

0 commit comments

Comments
 (0)