Skip to content

Commit

Permalink
feat: add "null" STMP transport mode
Browse files Browse the repository at this point in the history
== 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>
  • Loading branch information
thlehmann-ionos committed Oct 29, 2024
1 parent 11f0b0f commit 91ac85f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
*
Expand All @@ -529,6 +529,9 @@
* For ``qmail`` the binary is /var/qmail/bin/sendmail, and it must be installed
* on your Unix system.
*
* Use the ``null`` to send no mails (disable mail delivery). This can be useful
* if mails should be sent via APIs and rendering messages it not necessary.
*
* Defaults to ``smtp``
*/
'mail_smtpmode' => 'smtp',
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public function createEMailTemplate(string $emailId, array $data = []): IEMailTe
* @return string[] $failedRecipients
*/
public function send(IMessage $message): array {
if ($this->config->getSystemValueString('mail_smtpmode', 'smtp') === 'null') {
// The null transport indicates no mail shall be sent
return [];
}

$debugMode = $this->config->getSystemValueBool('mail_smtpdebug', false);

if (!($message instanceof Message)) {
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/Mail/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ public function testGetSendmailInstanceSendMailQmail($sendmailMode, $binaryParam
$this->assertEquals($sendmail, self::invokePrivate($this->mailer, 'getSendMailInstance'));
}

public function testGetSendmailInstanceSendMailNull(): void {
$this->config
->expects($this->exactly(1))
->method('getSystemValueString')
->with('mail_smtpmode', 'smtp')
->willReturn('null');

$this->dispatcher->expects($this->never())
->method('dispatchTyped');

$this->mailer->send($this->createMock(Message::class));
}

public function testGetInstanceDefault(): void {
$this->config
->method('getSystemValue')
Expand Down

0 comments on commit 91ac85f

Please sign in to comment.