Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Mailer] add reject to MessageEvent to stop sending mail #48409

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 3 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `MessageEvent::reject()` to allow rejecting an email before sending it

6.2
---

Expand Down
14 changes: 13 additions & 1 deletion src/Symfony/Component/Mailer/Event/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ final class MessageEvent extends Event
private Envelope $envelope;
private string $transport;
private bool $queued;
private bool $rejected = false;

/** @var StampInterface[] */
private array $stamps = [];
Expand Down Expand Up @@ -70,10 +71,21 @@ public function isQueued(): bool
return $this->queued;
}

public function isRejected(): bool
{
return $this->rejected;
}

public function reject(): void
{
$this->rejected = true;
$this->stopPropagation();
}

public function addStamp(StampInterface $stamp): void
{
if (!$this->queued) {
throw new LogicException(sprintf('Cannot call "%s()" on a message that is not meant to be queued', __METHOD__));
throw new LogicException(sprintf('Cannot call "%s()" on a message that is not meant to be queued.', __METHOD__));
}

$this->stamps[] = $stamp;
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public function send(RawMessage $message, Envelope $envelope = null): void
$event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
$this->dispatcher->dispatch($event);
$stamps = $event->getStamps();

if ($event->isRejected()) {
return;
}
}

try {
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Mailer/Tests/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@

use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Mailer\Envelope as MailerEnvelope;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mailer\Transport\NullTransport;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\RawMessage;

Expand Down Expand Up @@ -78,4 +83,34 @@ public function dispatch($message, array $stamps = []): Envelope
self::assertCount(1, $bus->stamps);
self::assertSame([$stamp], $bus->stamps);
}

public function testRejectMessage()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(MessageEvent::class, fn (MessageEvent $event) => $event->reject(), 255);
$dispatcher->addListener(MessageEvent::class, fn () => throw new \RuntimeException('Should never be called.'));

$transport = new class($dispatcher, $this) extends AbstractTransport {
public function __construct(EventDispatcherInterface $dispatcher, private TestCase $test)
{
parent::__construct($dispatcher);
}

protected function doSend(SentMessage $message): void
{
$this->test->fail('This should never be called as message is rejected.');
}

public function __toString(): string
{
return 'fake://';
}
};
$mailer = new Mailer($transport);

$message = new RawMessage('');
$envelope = new MailerEnvelope(new Address('[email protected]'), [new Address('[email protected]')]);
$mailer->send($message, $envelope);
$this->assertTrue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mailer\EventListener\MessageListener;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mailer\Transport\NullTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;
Expand Down Expand Up @@ -78,4 +82,32 @@ public function testRenderedTemplatedEmail()
$sentMessage = $transport->send((new TemplatedEmail())->to('[email protected]')->from('[email protected]')->htmlTemplate('tpl'));
$this->assertMatchesRegularExpression('/Some message/', $sentMessage->getMessage()->toString());
}

public function testRejectMessage()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(MessageEvent::class, fn (MessageEvent $event) => $event->reject(), 255);
$dispatcher->addListener(MessageEvent::class, fn () => throw new \RuntimeException('Should never be called.'));

$transport = new class($dispatcher, $this) extends AbstractTransport {
public function __construct(EventDispatcherInterface $dispatcher, private TestCase $test)
{
parent::__construct($dispatcher);
}

protected function doSend(SentMessage $message): void
{
$this->test->fail('This should never be called as message is rejected.');
}

public function __toString(): string
{
return 'fake://';
}
};

$message = new RawMessage('');
$envelope = new Envelope(new Address('[email protected]'), [new Address('[email protected]')]);
$this->assertNull($transport->send($message, $envelope));
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Mailer/Transport/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa

$event = new MessageEvent($message, $envelope, (string) $this);
$this->dispatcher->dispatch($event);
if ($event->isRejected()) {
return null;
}

$envelope = $event->getEnvelope();
$message = $event->getMessage();

Expand Down