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

Skip to content

Commit 22d2b54

Browse files
committed
[Mailer] Add support for allowing some users even if recipients is defined in EnvelopeListener
1 parent 60d0315 commit 22d2b54

File tree

13 files changed

+93
-5
lines changed

13 files changed

+93
-5
lines changed

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -2117,12 +2117,22 @@ private function addMailerSection(ArrayNodeDefinition $rootNode, callable $enabl
21172117
->arrayNode('envelope')
21182118
->info('Mailer Envelope configuration')
21192119
->fixXmlConfig('recipient')
2120+
->fixXmlConfig('allowed_recipient')
21202121
->children()
21212122
->scalarNode('sender')->end()
21222123
->arrayNode('recipients')
21232124
->performNoDeepMerging()
21242125
->beforeNormalization()
2125-
->ifArray()
2126+
->ifArray()
2127+
->then(fn ($v) => array_filter(array_values($v)))
2128+
->end()
2129+
->prototype('scalar')->end()
2130+
->end()
2131+
->arrayNode('allowed_recipients')
2132+
->info('A list of regular expressions that allow recipients when "recipients" option is defined.')
2133+
->performNoDeepMerging()
2134+
->beforeNormalization()
2135+
->ifArray()
21262136
->then(fn ($v) => array_filter(array_values($v)))
21272137
->end()
21282138
->prototype('scalar')->end()

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

+1
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,7 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
26702670
$envelopeListener = $container->getDefinition('mailer.envelope_listener');
26712671
$envelopeListener->setArgument(0, $config['envelope']['sender'] ?? null);
26722672
$envelopeListener->setArgument(1, $config['envelope']['recipients'] ?? null);
2673+
$envelopeListener->setArgument(2, $config['envelope']['allowed_recipients'] ?? []);
26732674

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

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

+1
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@
764764
<xsd:sequence>
765765
<xsd:element name="sender" type="xsd:string" minOccurs="0" maxOccurs="1" />
766766
<xsd:element name="recipient" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
767+
<xsd:element name="allowed-recipient" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
767768
</xsd:sequence>
768769
</xsd:complexType>
769770

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/mailer_with_dsn.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
'envelope' => [
1414
'sender' => '[email protected]',
1515
'recipients' => ['[email protected]'],
16+
'allowed_recipients' => ['/^[email protected]$/'],
1617
],
1718
'headers' => [
1819
'from' => '[email protected]',

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/mailer_with_transports.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
'envelope' => [
1717
'sender' => '[email protected]',
1818
'recipients' => ['[email protected]', '[email protected]'],
19+
'allowed_recipients' => ['/^[email protected]$/', '/@example.com$/'],
1920
],
2021
'headers' => [
2122
'from' => '[email protected]',

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/mailer_with_dsn.xml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<framework:envelope>
1414
<framework:sender>[email protected]</framework:sender>
1515
<framework:recipient>[email protected]</framework:recipient>
16+
<framework:allowed-recipient>/^[email protected]$/</framework:allowed-recipient>
1617
</framework:envelope>
1718
<framework:header name="from">[email protected]</framework:header>
1819
<framework:header name="bcc">[email protected]</framework:header>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/mailer_with_transports.xml

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
<framework:sender>[email protected]</framework:sender>
1717
<framework:recipient>[email protected]</framework:recipient>
1818
<framework:recipient>[email protected]</framework:recipient>
19+
<framework:allowed-recipient>/^[email protected]$/</framework:allowed-recipient>
20+
<framework:allowed-recipient>/@example.com$/</framework:allowed-recipient>
1921
</framework:envelope>
2022
<framework:header name="from">[email protected]</framework:header>
2123
<framework:header name="bcc">[email protected]</framework:header>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/mailer_with_dsn.yml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ framework:
1010
1111
recipients:
1212
13+
allowed_recipients:
14+
1315
headers:
1416
1517

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/mailer_with_transports.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ framework:
1313
recipients:
1414
1515
16+
allowed_recipients:
17+
18+
- /@example.com$/
1619
headers:
1720
1821

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,7 @@ public static function provideMailer(): iterable
20432043
'mailer_with_dsn',
20442044
['main' => 'smtp://example.com'],
20452045
2046+
20462047
];
20472048
yield [
20482049
'mailer_with_transports',
@@ -2051,13 +2052,14 @@ public static function provideMailer(): iterable
20512052
'transport2' => 'smtp://example2.com',
20522053
],
20532054
2055+
['/^[email protected]$/', '/@example.com$/'],
20542056
];
20552057
}
20562058

20572059
/**
20582060
* @dataProvider provideMailer
20592061
*/
2060-
public function testMailer(string $configFile, array $expectedTransports, array $expectedRecipients)
2062+
public function testMailer(string $configFile, array $expectedTransports, array $expectedRecipients, array $expectedAllowedRecipients)
20612063
{
20622064
$container = $this->createContainerFromFile($configFile);
20632065

@@ -2070,6 +2072,7 @@ public function testMailer(string $configFile, array $expectedTransports, array
20702072
$l = $container->getDefinition('mailer.envelope_listener');
20712073
$this->assertSame('[email protected]', $l->getArgument(0));
20722074
$this->assertSame($expectedRecipients, $l->getArgument(1));
2075+
$this->assertSame($expectedAllowedRecipients, $l->getArgument(2));
20732076
$this->assertEquals(new Reference('messenger.default_bus', ContainerInterface::NULL_ON_INVALID_REFERENCE), $container->getDefinition('mailer.mailer')->getArgument(1));
20742077

20752078
$this->assertTrue($container->hasDefinition('mailer.message_listener'));

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

+19-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,9 +33,13 @@ 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
}
@@ -57,7 +62,18 @@ public function onMessage(MessageEvent $event): void
5762
}
5863

5964
if ($this->recipients) {
60-
$event->getEnvelope()->setRecipients($this->recipients);
65+
$recipients = $this->recipients;
66+
if ($this->allowedRecipients) {
67+
foreach ($event->getEnvelope()->getRecipients() as $recipient) {
68+
foreach ($this->allowedRecipients as $allowedRecipient) {
69+
if (preg_match($allowedRecipient, $recipient->getAddress())) {
70+
$recipients[] = $recipient;
71+
continue 2;
72+
}
73+
}
74+
}
75+
}
76+
$event->getEnvelope()->setRecipients($recipients);
6177
}
6278
}
6379

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]'], ['[email protected]'], ['/@example.com$/', '/@symfony.com$/']];
45+
}
46+
}

0 commit comments

Comments
 (0)