-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Mailer] Add support for allowing some users even if recipients
is defined in EnvelopeListener
#54044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Mailer] Add support for allowing some users even if recipients
is defined in EnvelopeListener
#54044
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
'envelope' => [ | ||
'sender' => '[email protected]', | ||
'recipients' => ['[email protected]'], | ||
'allowed_recipients' => ['foobar@example\.org'], | ||
], | ||
'headers' => [ | ||
'from' => '[email protected]', | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
'envelope' => [ | ||
'sender' => '[email protected]', | ||
'recipients' => ['[email protected]', '[email protected]'], | ||
'allowed_recipients' => ['foobar@example\.org', '.*@example\.com'], | ||
], | ||
'headers' => [ | ||
'from' => '[email protected]', | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
<framework:envelope> | ||
<framework:sender>[email protected]</framework:sender> | ||
<framework:recipient>[email protected]</framework:recipient> | ||
<framework:allowed-recipient>foobar@example\.org</framework:allowed-recipient> | ||
</framework:envelope> | ||
<framework:header name="from">[email protected]</framework:header> | ||
<framework:header name="bcc">[email protected]</framework:header> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
<framework:sender>[email protected]</framework:sender> | ||
<framework:recipient>[email protected]</framework:recipient> | ||
<framework:recipient>[email protected]</framework:recipient> | ||
<framework:allowed-recipient>foobar@example\.org</framework:allowed-recipient> | ||
<framework:allowed-recipient>.*@example\.com</framework:allowed-recipient> | ||
</framework:envelope> | ||
<framework:header name="from">[email protected]</framework:header> | ||
<framework:header name="bcc">[email protected]</framework:header> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ framework: | |
sender: [email protected] | ||
recipients: | ||
- [email protected] | ||
allowed_recipients: | ||
- foobar@example\.org | ||
headers: | ||
from: [email protected] | ||
bcc: [[email protected], [email protected]] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ framework: | |
recipients: | ||
- [email protected] | ||
- [email protected] | ||
allowed_recipients: | ||
- foobar@example\.org | ||
- .*@example\.com | ||
headers: | ||
from: [email protected] | ||
bcc: [[email protected], [email protected]] | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2043,6 +2043,7 @@ public static function provideMailer(): iterable | |
'mailer_with_dsn', | ||
['main' => 'smtp://example.com'], | ||
['[email protected]'], | ||
['foobar@example\.org'], | ||
]; | ||
yield [ | ||
'mailer_with_transports', | ||
|
@@ -2051,13 +2052,14 @@ public static function provideMailer(): iterable | |
'transport2' => 'smtp://example2.com', | ||
], | ||
['[email protected]', '[email protected]'], | ||
['foobar@example\.org', '.*@example\.com'], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideMailer | ||
*/ | ||
public function testMailer(string $configFile, array $expectedTransports, array $expectedRecipients) | ||
public function testMailer(string $configFile, array $expectedTransports, array $expectedRecipients, array $expectedAllowedRecipients) | ||
{ | ||
$container = $this->createContainerFromFile($configFile); | ||
|
||
|
@@ -2070,6 +2072,7 @@ public function testMailer(string $configFile, array $expectedTransports, array | |
$l = $container->getDefinition('mailer.envelope_listener'); | ||
$this->assertSame('[email protected]', $l->getArgument(0)); | ||
$this->assertSame($expectedRecipients, $l->getArgument(1)); | ||
$this->assertSame($expectedAllowedRecipients, $l->getArgument(2)); | ||
$this->assertEquals(new Reference('messenger.default_bus', ContainerInterface::NULL_ON_INVALID_REFERENCE), $container->getDefinition('mailer.mailer')->getArgument(1)); | ||
|
||
$this->assertTrue($container->hasDefinition('mailer.message_listener')); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
* Manipulates the Envelope of a Message. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
* @author Grégoire Pineau <[email protected]> | ||
*/ | ||
class EnvelopeListener implements EventSubscriberInterface | ||
{ | ||
|
@@ -32,9 +33,13 @@ class EnvelopeListener implements EventSubscriberInterface | |
|
||
/** | ||
* @param array<Address|string> $recipients | ||
* @param string[] $allowedRecipients An array of regex to match the allowed recipients | ||
*/ | ||
public function __construct(Address|string|null $sender = null, ?array $recipients = null) | ||
{ | ||
public function __construct( | ||
Address|string|null $sender = null, | ||
?array $recipients = null, | ||
private array $allowedRecipients = [], | ||
) { | ||
if (null !== $sender) { | ||
$this->sender = Address::create($sender); | ||
} | ||
|
@@ -57,7 +62,27 @@ public function onMessage(MessageEvent $event): void | |
} | ||
|
||
if ($this->recipients) { | ||
$event->getEnvelope()->setRecipients($this->recipients); | ||
$recipients = $this->recipients; | ||
if ($this->allowedRecipients) { | ||
foreach ($event->getEnvelope()->getRecipients() as $recipient) { | ||
foreach ($this->allowedRecipients as $allowedRecipient) { | ||
if (!preg_match('{\A'.$allowedRecipient.'\z}', $recipient->getAddress())) { | ||
continue; | ||
} | ||
// dedup | ||
foreach ($recipients as $r) { | ||
if ($r->getName() === $recipient->getName() && $r->getAddress() === $recipient->getAddress()) { | ||
continue 2; | ||
} | ||
} | ||
|
||
$recipients[] = $recipient; | ||
continue 2; | ||
} | ||
} | ||
} | ||
lyrixx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$event->getEnvelope()->setRecipients($recipients); | ||
} | ||
} | ||
|
||
|
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Mailer/Tests/EventListener/EnvelopeListenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Tests\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Mailer\Envelope; | ||
use Symfony\Component\Mailer\Event\MessageEvent; | ||
use Symfony\Component\Mailer\EventListener\EnvelopeListener; | ||
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\RawMessage; | ||
|
||
class EnvelopeListenerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider provideRecipientsTests | ||
*/ | ||
public function testRecipients(array $expected, ?array $recipients = null, array $allowedRecipients = []) | ||
{ | ||
$listener = new EnvelopeListener(null, $recipients, $allowedRecipients); | ||
$message = new RawMessage('message'); | ||
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]'), new Address('[email protected]')]); | ||
$event = new MessageEvent($message, $envelope, 'default'); | ||
|
||
$listener->onMessage($event); | ||
|
||
$recipients = array_map(fn (Address $a): string => $a->getAddress(), $event->getEnvelope()->getRecipients()); | ||
$this->assertSame($expected, $recipients); | ||
} | ||
|
||
public static function provideRecipientsTests(): iterable | ||
{ | ||
yield [['[email protected]', '[email protected]'], null, []]; | ||
lyrixx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
yield [['[email protected]'], ['[email protected]'], []]; | ||
yield [['[email protected]', '[email protected]'], ['[email protected]'], ['.*@example\.com']]; | ||
yield [['[email protected]', '[email protected]', '[email protected]'], ['[email protected]'], ['.*@example\.com', '.*@symfony\.com']]; | ||
yield [['[email protected]', '[email protected]'], ['[email protected]'], ['.*@example\.com', '.*@symfony\.com']]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.