diff options
author | Louis <louis@chmn.me> | 2024-11-19 15:02:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-19 15:02:22 +0100 |
commit | 73f3b9bad8c8e03ed48687a3683cb73874def19a (patch) | |
tree | 7aff334eaafb0661ee73aa7aae1824b51129c7cc | |
parent | 935f0d27acfdbb60b11924688aeaaeced62a1e6e (diff) | |
parent | 3f6c969d83faa23379d79ac678c75c22926a5fe1 (diff) | |
download | nextcloud-server-73f3b9bad8c8e03ed48687a3683cb73874def19a.tar.gz nextcloud-server-73f3b9bad8c8e03ed48687a3683cb73874def19a.zip |
Merge pull request #48977 from IONOS-Productivity/tl/dev/disable-mailing
feat: add "null" SMTP transport mode
-rw-r--r-- | apps/settings/templates/settings/admin/additional-mail.php | 8 | ||||
-rw-r--r-- | config/config.sample.php | 5 | ||||
-rw-r--r-- | lib/private/Mail/Mailer.php | 10 | ||||
-rw-r--r-- | tests/lib/Mail/MailerTest.php | 26 |
4 files changed, 45 insertions, 4 deletions
diff --git a/apps/settings/templates/settings/admin/additional-mail.php b/apps/settings/templates/settings/admin/additional-mail.php index ecc2973f085..6d6c036e813 100644 --- a/apps/settings/templates/settings/admin/additional-mail.php +++ b/apps/settings/templates/settings/admin/additional-mail.php @@ -34,6 +34,13 @@ $mail_sendmailmode = [ ?> <div class="section" id="mail_general_settings"> +<?php if ($_['mail_smtpmode'] === 'null') { ?> + <h2><?php p($l->t('Email server'));?></h2> + + <p> + <?php p($l->t('Mail delivery is disabled by instance config "%s".', ['mail_smtpmode'])); ?> + </p> +<?php } else { ?> <form id="mail_general_settings_form" class="mail_settings"> <h2><?php p($l->t('Email server'));?></h2> <a target="_blank" @@ -143,4 +150,5 @@ $mail_sendmailmode = [ <em><?php p($l->t('Test and verify email settings')); ?></em> <input type="submit" name="sendtestemail" id="sendtestemail" value="<?php p($l->t('Send email')); ?>"/> <span id="sendtestmail_msg" class="msg"></span> +<?php } ?> </div> diff --git a/config/config.sample.php b/config/config.sample.php index d5a3f0ee017..96377b7dbf0 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -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', diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index da3fcd35888..18636e183d0 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -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; @@ -255,9 +256,10 @@ class Mailer implements IMailer { 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 @@ class Mailer implements IMailer { 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 f215e385e3f..02a2605fc77 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -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') @@ -337,4 +357,10 @@ class MailerTest extends TestCase { 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); + } } |