8000 bug #46154 [Mailer] Restore X-Transport after failure (zenas1210) · symfony/symfony@7f4c304 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f4c304

Browse files
committed
bug #46154 [Mailer] Restore X-Transport after failure (zenas1210)
This PR was merged into the 4.4 branch. Discussion ---------- [Mailer] Restore X-Transport after failure | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #44741 | License | MIT | Doc PR | Restores X-Transport header for retries after failure. Commits ------- 635e995 [Mailer] Restore X-Transport after failure
2 parents 18f3978 + 635e995 commit 7f4c304

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Symfony/Component/Mailer/Tests/Transport/TransportsTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,28 @@ public function testTransportDoesNotExist()
6464
$this->expectExceptionMessage('The "foobar" transport does not exist (available transports: "foo", "bar").');
6565
$transport->send($email);
6666
}
67+
68+
public function testTransportRestoredAfterFailure()
69+
{
70+
$exception = new \Exception();
71+
72+
$fooTransport = $this->createMock(TransportInterface::class);
73+
$fooTransport->method('send')
74+
->willThrowException($exception);
75+
76+
$transport = new Transports([
77+
'foo' => $fooTransport,
78+
]);
79+
80+
$headers = (new Headers())->addTextHeader('X-Transport', 'foo');
81+
$email = new Message($headers, new TextPart('...'));
82+
83+
$this->expectExceptionObject($exception);
84+
85+
try {
86+
$transport->send($email);
87+
} finally {
88+
$this->assertSame('foo', $email->getHeaders()->getHeaderBody('X-Transport'));
89+
}
90+
}
6791
}

src/Symfony/Component/Mailer/Transport/Transports.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ public function send(RawMessage $message, Envelope $envelope = null): ?SentMessa
5959
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
6060
}
6161

62-
return $this->transports[$transport]->send($message, $envelope);
62+
try {
63+
return $this->transports[$transport]->send($message, $envelope);
64+
} catch (\Throwable $e) {
65+
$headers->addTextHeader('X-Transport', $transport);
66+
67+
throw $e;
68+
}
6369
}
6470

6571
public function __toString(): string

0 commit comments

Comments
 (0)
0