diff --git a/apps/settings/templates/settings/admin/additional-mail.php b/apps/settings/templates/settings/admin/additional-mail.php
index ecc2973f085f6..6d6c036e81300 100644
--- a/apps/settings/templates/settings/admin/additional-mail.php
+++ b/apps/settings/templates/settings/admin/additional-mail.php
@@ -34,6 +34,13 @@
?>
+
+
t('Email server'));?>
+
+
+ t('Mail delivery is disabled by instance config "%s".', ['mail_smtpmode'])); ?>
+
+
diff --git a/config/config.sample.php b/config/config.sample.php
index b39675909f69c..c8e29ca95e9d2 100644
--- a/config/config.sample.php
+++ b/config/config.sample.php
@@ -501,7 +501,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``.
*
@@ -511,6 +511,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 71d9aca9aeb8a..ce219d59010e6 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;
@@ -255,9 +256,10 @@ protected function getInstance(): MailerInterface {
return $this->instance;
}
- $transport = null;
-
switch ($this->config->getSystemValueString('mail_smtpmode', 'smtp')) {
+ case 'null':
+ $transport = new NullTransport();
+ break;
case 'sendmail':
$transport = $this->getSendMailInstance();
break;
@@ -267,7 +269,9 @@ protected function getInstance(): MailerInterface {
break;
}
- return new SymfonyMailer($transport);
+ $this->instance = new SymfonyMailer($transport);
+
+ return $this->instance;
}
/**
diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php
index be95307da6d2e..bc1e1805efeac 100644
--- a/tests/lib/Mail/MailerTest.php
+++ b/tests/lib/Mail/MailerTest.php
@@ -113,7 +113,27 @@ public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam
$this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance'));
}
- public function testGetInstanceDefault() {
+ 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')
->willReturnMap([
@@ -336,4 +356,10 @@ public function testLocalDomainInvalidUrl(): void {
self::assertInstanceOf(EsmtpTransport::class, $transport);
self::assertEquals('[127.0.0.1]', $transport->getLocalDomain());
}
+
+ public function testCaching(): void {
+ $symfonyMailer1 = self::invokePrivate($this->mailer, 'getInstance');
+ $symfonyMailer2 = self::invokePrivate($this->mailer, 'getInstance');
+ self::assertSame($symfonyMailer1, $symfonyMailer2);
+ }
}