Skip to content
Permalink
Browse files

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
  • Loading branch information...
fabpot committed Jul 3, 2019
2 parents 802dc1b + 8e0c800 commit 7b9c0261539d784399589ae2b9da96193e6809f6
@@ -1510,6 +1510,22 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
->{!class_exists(FullStack::class) && class_exists(Mailer::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->children()
->scalarNode('dsn')->defaultValue('smtp://null')->end()
->arrayNode('envelope')
->info('Mailer Envelope configuration')
->children()
->scalarNode('sender')->end()
->arrayNode('recipients')
->performNoDeepMerging()
->beforeNormalization()
->ifArray()
->then(function ($v) {
return array_filter(array_values($v));
})
->end()
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
->end()
@@ -1922,6 +1922,13 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
$loader->load('mailer.xml');
$container->getDefinition('mailer.default_transport')->setArgument(0, $config['dsn']);
$recipients = $config['envelope']['recipients'] ?? null;
$sender = $config['envelope']['sender'] ?? null;
$envelopeListener = $container->getDefinition('mailer.envelope_listener');
$envelopeListener->setArgument(0, $sender);
$envelopeListener->setArgument(1, $recipients);
}
/**
@@ -25,5 +25,11 @@
<argument type="service" id="mailer.default_transport" />
<tag name="messenger.message_handler" />
</service>

<service id="mailer.envelope_listener" class="Symfony\Component\Mailer\EventListener\EnvelopeListener">
<argument /> <!-- sender -->
<argument /> <!-- recipients -->
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
@@ -0,0 +1,62 @@
<?php
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class MailerTest extends WebTestCase
{
public function testEnvelopeListener()
{
self::bootKernel(['test_case' => 'Mailer']);
$onDoSend = function (SentMessage $message) {
$envelope = $message->getEnvelope();
$this->assertEquals(
[new Address('redirected@example.org')],
$envelope->getRecipients()
);
$this->assertEquals('sender@example.org', $envelope->getSender()->getAddress());
};
$eventDispatcher = self::$container->get(EventDispatcherInterface::class);
$logger = self::$container->get('logger');
$testTransport = new class($eventDispatcher, $logger, $onDoSend) extends AbstractTransport {
/**
* @var callable
*/
private $onDoSend;
public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger, callable $onDoSend)
{
parent::__construct($eventDispatcher, $logger);
$this->onDoSend = $onDoSend;
}
protected function doSend(SentMessage $message): void
{
$onDoSend = $this->onDoSend;
$onDoSend($message);
}
};
$mailer = new Mailer($testTransport, null);
$message = (new Email())
->subject('Test subject')
->text('Hello world')
->from('from@example.org')
->to('to@example.org');
$mailer->send($message);
}
}
@@ -0,0 +1,18 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
return [
new FrameworkBundle(),
new TestBundle(),
];
@@ -0,0 +1,9 @@
imports:
- { resource: ../config/default.yml }

framework:
mailer:
envelope:
sender: sender@example.org
recipients:
- redirected@example.org

0 comments on commit 7b9c026

Please sign in to comment.
You can’t perform that action at this time.