From 2fa6378bbfbb5a46dff1ffaf76301ef66e7aa9df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Schl=C3=A4pfer?= Date: Sun, 11 Oct 2020 15:28:34 +0200 Subject: [PATCH 1/2] [Mailer] return `SentMessage` from `Mailer::send` if available --- src/Symfony/Component/Mailer/Mailer.php | 6 ++---- src/Symfony/Component/Mailer/MailerInterface.php | 2 +- src/Symfony/Component/Mailer/Tests/MailerTest.php | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Mailer/Mailer.php b/src/Symfony/Component/Mailer/Mailer.php index d15aa558f7075..1c6e1c47f765a 100644 --- a/src/Symfony/Component/Mailer/Mailer.php +++ b/src/Symfony/Component/Mailer/Mailer.php @@ -36,12 +36,10 @@ public function __construct(TransportInterface $transport, MessageBusInterface $ $this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher; } - public function send(RawMessage $message, Envelope $envelope = null): void + public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage { if (null === $this->bus) { - $this->transport->send($message, $envelope); - - return; + return $this->transport->send($message, $envelope); } if (null !== $this->dispatcher) { diff --git a/src/Symfony/Component/Mailer/MailerInterface.php b/src/Symfony/Component/Mailer/MailerInterface.php index eb44cf640c263..7d1808a26cca4 100644 --- a/src/Symfony/Component/Mailer/MailerInterface.php +++ b/src/Symfony/Component/Mailer/MailerInterface.php @@ -26,5 +26,5 @@ interface MailerInterface /** * @throws TransportExceptionInterface */ - public function send(RawMessage $message, Envelope $envelope = null): void; + public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage; } diff --git a/src/Symfony/Component/Mailer/Tests/MailerTest.php b/src/Symfony/Component/Mailer/Tests/MailerTest.php index dd9d5a95ad489..59fd34716134e 100644 --- a/src/Symfony/Component/Mailer/Tests/MailerTest.php +++ b/src/Symfony/Component/Mailer/Tests/MailerTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Mailer\Exception\LogicException; use Symfony\Component\Mailer\Mailer; +use Symfony\Component\Mailer\SentMessage; use Symfony\Component\Mailer\Transport\TransportInterface; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Mime\RawMessage; @@ -21,11 +22,23 @@ class MailerTest extends TestCase { - public function testSendingRawMessages() + public function testSendingRawMessagesWithMessenger() { $this->expectException(LogicException::class); $transport = new Mailer($this->createMock(TransportInterface::class), $this->createMock(MessageBusInterface::class), $this->createMock(EventDispatcherInterface::class)); $transport->send(new RawMessage('Some raw email message')); } + + public function testSendingRawMessagesWithoutMessenger() + { + $transportMock = $this->createMock(TransportInterface::class); + $transportMock + ->expects(self::once()) + ->method('send') + ->willReturn($this->createMock(SentMessage::class)); + $transport = new Mailer($transportMock, null, $this->createMock(EventDispatcherInterface::class)); + $result = $transport->send(new RawMessage('Some raw email message without messenger')); + self::assertInstanceOf(SentMessage::class, $result); + } } From f38b43f95c6f69506c634fd74e704fc0c42ffec5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Schl=C3=A4pfer?= Date: Sun, 11 Oct 2020 15:31:47 +0200 Subject: [PATCH 2/2] [Mailer] update CHANGELOG.md about returning SentMessage after sending --- src/Symfony/Component/Mailer/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Mailer/CHANGELOG.md b/src/Symfony/Component/Mailer/CHANGELOG.md index cca04f11aa78e..02cd19e1e16b9 100644 --- a/src/Symfony/Component/Mailer/CHANGELOG.md +++ b/src/Symfony/Component/Mailer/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * added `NativeTransportFactory` to configure a transport based on php.ini settings * added `local_domain`, `restart_threshold`, `restart_threshold_sleep` and `ping_threshold` options for `smtp` * added `command` option for `sendmail` + * return `SentMessage` after `Mailer::send` if not using messenger and available from transport 4.4.0 -----