]> source.dussan.org Git - nextcloud-server.git/commitdiff
feat(Mailer): add "null" SMTP transport mode
authorThomas Lehmann <t.lehmann@strato.de>
Tue, 29 Oct 2024 10:53:18 +0000 (11:53 +0100)
committerThomas Lehmann <147605810+thlehmann-ionos@users.noreply.github.com>
Tue, 19 Nov 2024 10:32:39 +0000 (11:32 +0100)
== 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 <t.lehmann@strato.de>
config/config.sample.php
lib/private/Mail/Mailer.php
tests/lib/Mail/MailerTest.php

index d5a3f0ee01775f451962842a02bb99c597e28964..96377b7dbf0ba93b42608b3e9d857d47017c558f 100644 (file)
@@ -521,7 +521,7 @@ $CONFIG = [
 '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``.
  *
@@ -531,6 +531,9 @@ $CONFIG = [
  * 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',
index b660ee0c02fd24c9432f84c06f21a80395e7663b..18636e183d0dce03b9c02846de9c28fbaa39c89c 100644 (file)
@@ -27,6 +27,7 @@ use Psr\Log\LoggerInterface;
 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;
@@ -256,6 +257,9 @@ class Mailer implements IMailer {
                }
 
                switch ($this->config->getSystemValueString('mail_smtpmode', 'smtp')) {
+                       case 'null':
+                               $transport = new NullTransport();
+                               break;
                        case 'sendmail':
                                $transport = $this->getSendMailInstance();
                                break;
index 84015f38d242cffe2d1a393d0bd0954116cbbe97..02a2605fc77e2d65f273556a90757c181a0c61bc 100644 (file)
@@ -114,6 +114,26 @@ class MailerTest extends TestCase {
                $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->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')