diff --git a/src/Symfony/Component/Mailer/Tests/Transport/TransportsTest.php b/src/Symfony/Component/Mailer/Tests/Transport/TransportsTest.php index 10d46ca846b4e..e50db434ffc0c 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/TransportsTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/TransportsTest.php @@ -64,4 +64,28 @@ public function testTransportDoesNotExist() $this->expectExceptionMessage('The "foobar" transport does not exist (available transports: "foo", "bar").'); $transport->send($email); } + + public function testTransportRestoredAfterFailure() + { + $exception = new \Exception(); + + $fooTransport = $this->createMock(TransportInterface::class); + $fooTransport->method('send') + ->willThrowException($exception); + + $transport = new Transports([ + 'foo' => $fooTransport, + ]); + + $headers = (new Headers())->addTextHeader('X-Transport', 'foo'); + $email = new Message($headers, new TextPart('...')); + + $this->expectExceptionObject($exception); + + try { + $transport->send($email); + } finally { + $this->assertSame('foo', $email->getHeaders()->getHeaderBody('X-Transport')); + } + } } diff --git a/src/Symfony/Component/Mailer/Transport/Transports.php b/src/Symfony/Component/Mailer/Transport/Transports.php index c8f7970b32ef5..702fc5c784cd4 100644 --- a/src/Symfony/Component/Mailer/Transport/Transports.php +++ b/src/Symfony/Component/Mailer/Transport/Transports.php @@ -59,7 +59,13 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports)))); } - return $this->transports[$transport]->send($message, $envelope); + try { + return $this->transports[$transport]->send($message, $envelope); + } catch (\Throwable $e) { + $headers->addTextHeader('X-Transport', $transport); + + throw $e; + } } public function __toString(): string