Skip to content

Commit

Permalink
[WIP][Mailer] Overwrite envelope sender and recipients from config
Browse files Browse the repository at this point in the history
  • Loading branch information
Devristo authored and fabpot committed Jul 3, 2019
1 parent 411ad97 commit 8e0c800
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
62 changes: 62 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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(),
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: ../config/default.yml }

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

0 comments on commit 8e0c800

Please sign in to comment.