From 766465ec19252a8b92ee4626f3ece081c5799ad5 Mon Sep 17 00:00:00 2001 From: Thomas Lehmann Date: Tue, 29 Oct 2024 11:53:18 +0100 Subject: [PATCH] feat: add "null" SMTP transport mode == Goal Allow disabling mail delivery altogether. == Usecase If mails ought to be send by other means than rendering messages from templates and sending them via SMTP-like protocols. Example: listening to specific Nextcloud events and pass parameters to a centralized (i.e. REST-based) API that sends e-mails. Signed-off-by: Thomas Lehmann --- config/config.sample.php | 5 ++++- lib/private/Mail/Mailer.php | 4 ++++ tests/lib/Mail/MailerTest.php | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/config/config.sample.php b/config/config.sample.php index 23e9cb5940afe..f13dc8326ae64 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -519,7 +519,7 @@ 'mail_smtpdebug' => false, /** - * Which mode to use for sending mail: ``sendmail``, ``smtp`` or ``qmail``. + * Which mode to use for sending mail: ``sendmail``, ``smtp``, ``qmail`` or ``null``. * * If you are using local or remote SMTP, set this to ``smtp``. * @@ -529,6 +529,9 @@ * For ``qmail`` the binary is /var/qmail/bin/sendmail, and it must be installed * on your Unix system. * + * Use the string ``null`` to send no mails (disable mail delivery). This can be + * useful if mails should be sent via APIs and rendering messages is not necessary. + * * Defaults to ``smtp`` */ 'mail_smtpmode' => 'smtp', diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 46301f5fc0ed0..678ac254bbc91 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -27,6 +27,7 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\Mailer as SymfonyMailer; use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mailer\Transport\NullTransport; use Symfony\Component\Mailer\Transport\SendmailTransport; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream; @@ -250,6 +251,9 @@ public function validateMailAddress(string $email): bool { protected function getInstance(): MailerInterface { switch ($this->config->getSystemValueString('mail_smtpmode', 'smtp')) { + case 'null': + $transport = new NullTransport(); + break; case 'sendmail': $transport = $this->getSendMailInstance(); break; diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index f215e385e3f75..012a7caec6d3b 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -21,6 +21,7 @@ use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use Symfony\Component\Mailer\Mailer as SymfonyMailer; +use Symfony\Component\Mailer\Transport\NullTransport; use Symfony\Component\Mailer\Transport\SendmailTransport; use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; use Symfony\Component\Mime\Email; @@ -114,6 +115,28 @@ public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam $this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance')); } + public function testEventForNullTransport(): void { + $this->config + ->expects($this->exactly(1)) + ->method('getSystemValueString') + ->with('mail_smtpmode', 'smtp') + ->willReturn('null'); + + // $message = $this->createMock(Message::class); + $message = $this->createMock(Message::class); + $message->expects($this->once()) + ->method('getSymfonyEmail') + ->willReturn((new Email())->to("foo@bar.com")->from("bar@foo.com")->text("")); + + + $event = new BeforeMessageSent($message); + $this->dispatcher->expects($this->once()) + ->method('dispatchTyped') + ->with($this->equalTo($event)); + + $this->mailer->send($message); + } + public function testGetInstanceDefault(): void { $this->config ->method('getSystemValue')