Skip to content

Commit 48ef6e2

Browse files
committed
prevent mailer from sending messages marked as "draft"
1 parent 35e4788 commit 48ef6e2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Symfony/Component/Mailer/Mailer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use Psr\EventDispatcher\EventDispatcherInterface;
1515
use Symfony\Component\Mailer\Event\MessageEvent;
16+
use Symfony\Component\Mailer\Exception\LogicException;
1617
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
1718
use Symfony\Component\Mailer\Transport\TransportInterface;
1819
use Symfony\Component\Messenger\MessageBusInterface;
20+
use Symfony\Component\Mime\Message;
1921
use Symfony\Component\Mime\RawMessage;
2022

2123
/**
@@ -36,6 +38,10 @@ public function __construct(TransportInterface $transport, MessageBusInterface $
3638

3739
public function send(RawMessage $message, Envelope $envelope = null): void
3840
{
41+
if ($message instanceof Message && '1' === $message->getHeaders()->getHeaderBody('X-Unsent')) {
42+
throw new LogicException('Cannot send messages marked as "draft".');
43+
}
44+
3945
if (null === $this->bus) {
4046
$this->transport->send($message, $envelope);
4147

src/Symfony/Component/Mailer/Tests/MailerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@ public function dispatch($message, array $stamps = []): Envelope
6969
self::assertCount(1, $bus->messages);
7070
self::assertSame($email, $bus->messages[0]->getMessage());
7171
}
72+
73+
public function testCannotSendDraftEmails()
74+
{
75+
$transport = new Mailer($this->createMock(TransportInterface::class), $this->createMock(MessageBusInterface::class), $this->createMock(EventDispatcherInterface::class));
76+
77+
$this->expectException(LogicException::class);
78+
79+
$email = new Email();
80+
$email->getHeaders()->addTextHeader('X-Unsent', '1');
81+
82+
$transport->send($email);
83+
}
7284
}

0 commit comments

Comments
 (0)