Skip to content

Commit

Permalink
feat(Mailer): add "null" SMTP 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 Nov 19, 2024
1 parent 40211f3 commit e4c013d
Show file tree
Hide file tree
Showing 3 changed files with 28 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 @@ -521,7 +521,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 @@ -531,6 +531,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',
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -256,6 +257,9 @@ protected function getInstance(): MailerInterface {
}

switch ($this->config->getSystemValueString('mail_smtpmode', 'smtp')) {
case 'null':
$transport = new NullTransport();
break;
case 'sendmail':
$transport = $this->getSendMailInstance();
break;
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/Mail/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ 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->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')
Expand Down

0 comments on commit e4c013d

Please sign in to comment.