Skip to content

[Mailer] Restore X-Transport after failure #46154

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 1 commit into from
Apr 27, 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
24 changes: 24 additions & 0 deletions src/Symfony/Component/Mailer/Tests/Transport/TransportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
}
8 changes: 7 additions & 1 deletion src/Symfony/Component/Mailer/Transport/Transports.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down