diff --git a/lib/Service/MailService.php b/lib/Service/MailService.php index b5e331af4c..c58e0796a7 100644 --- a/lib/Service/MailService.php +++ b/lib/Service/MailService.php @@ -298,7 +298,9 @@ public function sendCodeToSign(string $email, string $name, string $code): void } $message->useTemplate($emailTemplate); try { - $this->mailer->send($message); + if ($this->mailer->send($message) !== []) { + throw new \RuntimeException('Mail transport reported rejected recipients'); + } } catch (\Exception $e) { $this->logger->error('Mail with code to sign document could not be sent: ' . $e->getMessage()); throw new LibresignException('Mail with code to sign document could not be sent', 1); diff --git a/tests/php/Unit/Service/MailServiceTest.php b/tests/php/Unit/Service/MailServiceTest.php index c3dc40c7ee..2934c3d077 100644 --- a/tests/php/Unit/Service/MailServiceTest.php +++ b/tests/php/Unit/Service/MailServiceTest.php @@ -362,6 +362,34 @@ public function testRequesterStrategyThrowsWhenFallbackAlsoFails(): void { $this->service->notifyUnsignedUser($this->mockSignRequest(), 'a@b.coop'); } + public function testSendCodeToSignAcceptsSuccessfulSubmission(): void { + $this->mockEmailTemplate(); + $message = $this->mockSystemMessage(); + $message->expects($this->once()) + ->method('setTo') + ->with(['recipient@example.com']); + $this->mailer->expects($this->once()) + ->method('send') + ->with($message) + ->willReturn([]); + + $this->service->sendCodeToSign('recipient@example.com', '', '123456'); + } + + public function testSendCodeToSignReportsRejectedRecipients(): void { + $this->mockEmailTemplate(); + $message = $this->mockSystemMessage(); + $this->mailer->expects($this->once()) + ->method('send') + ->with($message) + ->willReturn(['recipient@example.com']); + $this->logger->expects($this->once())->method('error'); + + $this->expectException(LibresignException::class); + $this->expectExceptionMessage('Mail with code to sign document could not be sent'); + $this->service->sendCodeToSign('recipient@example.com', 'Recipient', '123456'); + } + private function mockSignRequest(string $displayName = 'John Doe'): SignRequest&MockObject { $signRequest = $this->createMock(SignRequest::class); $signRequest