Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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
62 changes: 62 additions & 0 deletions
62
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/MailerTest.php
This file contains 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,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); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/bundles.php
This file contains 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,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(), | ||
]; |
9 changes: 9 additions & 0 deletions
9
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Mailer/config.yml
This file contains 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,9 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
|
||
framework: | ||
mailer: | ||
envelope: | ||
sender: sender@example.org | ||
recipients: | ||
- redirected@example.org |