Skip to content

Commit 7b9c026

Browse files
committed
feature #32081 [WIP][Mailer] Overwrite envelope sender and recipients from config (Devristo)
This PR was squashed before being merged into the 4.4 branch (closes #32081). Discussion ---------- [WIP][Mailer] Overwrite envelope sender and recipients from config | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #31592 #31733 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> # Description This MR adds the following configuration, example: ```yaml # config/packages/mailer.yaml framework: mailer: envelope: sender: 'sender@example.org' recipients: ['redirected@example.org'] ``` In turn the `\Symfony\Component\Mailer\EventListener\EnvelopeListener` will be configured to alter the sender and recipient addresses before the message has been sent. Note: it will only alter the envelope, thus rerouting the message to the correct mailbox. However the message itself will still have the original 'from' and 'to' headers. # Todos - [x] Alter configuration and dependency injection - [x] Create test case - [ ] Update XML config schema? - [ ] Doc PR <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against branch 4.4. - Legacy code removals go to the master branch. --> Commits ------- 8e0c800 [WIP][Mailer] Overwrite envelope sender and recipients from config
2 parents 802dc1b + 8e0c800 commit 7b9c026

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,22 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
15101510
->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
15111511
->children()
15121512
->scalarNode('dsn')->defaultValue('smtp://null')->end()
1513+
->arrayNode('envelope')
1514+
->info('Mailer Envelope configuration')
1515+
->children()
1516+
->scalarNode('sender')->end()
1517+
->arrayNode('recipients')
1518+
->performNoDeepMerging()
1519+
->beforeNormalization()
1520+
->ifArray()
1521+
->then(function ($v) {
1522+
return array_filter(array_values($v));
1523+
})
1524+
->end()
1525+
->prototype('scalar')->end()
1526+
->end()
1527+
->end()
1528+
->end()
15131529
->end()
15141530
->end()
15151531
->end()

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,13 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
19221922

19231923
$loader->load('mailer.xml');
19241924
$container->getDefinition('mailer.default_transport')->setArgument(0, $config['dsn']);
1925+
1926+
$recipients = $config['envelope']['recipients'] ?? null;
1927+
$sender = $config['envelope']['sender'] ?? null;
1928+
1929+
$envelopeListener = $container->getDefinition('mailer.envelope_listener');
1930+
$envelopeListener->setArgument(0, $sender);
1931+
$envelopeListener->setArgument(1, $recipients);
19251932
}
19261933

19271934
/**

src/Symfony/Bundle/FrameworkBundle/Resources/config/mailer.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@
2525
<argument type="service" id="mailer.default_transport" />
2626
<tag name="messenger.message_handler" />
2727
</service>
28+
29+
<service id="mailer.envelope_listener" class="Symfony\Component\Mailer\EventListener\EnvelopeListener">
30+
<argument /> <!-- sender -->
31+
<argument /> <!-- recipients -->
32+
<tag name="kernel.event_subscriber"/>
33+
</service>
2834
</services>
2935
</container>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
4+
5+
use Psr\Log\LoggerInterface;
6+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7+
use Symfony\Component\Mailer\Mailer;
8+
use Symfony\Component\Mailer\SentMessage;
9+
use Symfony\Component\Mailer\Transport\AbstractTransport;
10+
use Symfony\Component\Mime\Address;
11+
use Symfony\Component\Mime\Email;
12+
13+
class MailerTest extends WebTestCase
14+
{
15+
public function testEnvelopeListener()
16+
{
17+
self::bootKernel(['test_case' => 'Mailer']);
18+
19+
$onDoSend = function (SentMessage $message) {
20+
$envelope = $message->getEnvelope();
21+
22+
$this->assertEquals(
23+
[new Address('redirected@example.org')],
24+
$envelope->getRecipients()
25+
);
26+
27+
$this->assertEquals('sender@example.org', $envelope->getSender()->getAddress());
28+
};
29+
30+
$eventDispatcher = self::$container->get(EventDispatcherInterface::class);
31+
$logger = self::$container->get('logger');
32+
33+
$testTransport = new class($eventDispatcher, $logger, $onDoSend) extends AbstractTransport {
34+
/**
35+
* @var callable
36+
*/
37+
private $onDoSend;
38+
39+
public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, callable $onDoSend)
40+
{
41+
parent::__construct($eventDispatcher, $logger);
42+
$this->onDoSend = $onDoSend;
43+
}
44+
45+
protected function doSend(SentMessage $message): void
46+
{
47+
$onDoSend = $this->onDoSend;
48+
$onDoSend($message);
49+
}
50+
};
51+
52+
$mailer = new Mailer($testTransport, null);
53+
54+
$message = (new Email())
55+
->subject('Test subject')
56+
->text('Hello world')
57+
->from('from@example.org')
58+
->to('to@example.org');
59+
60+
$mailer->send($message);
61+
}
62+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
mailer:
6+
envelope:
7+
sender: sender@example.org
8+
recipients:
9+
- redirected@example.org

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy