Skip to content

Commit

Permalink
Merge pull request #48977 from IONOS-Productivity/tl/dev/disable-mailing
Browse files Browse the repository at this point in the history
feat: add "null" SMTP transport mode
  • Loading branch information
artonge authored Nov 19, 2024
2 parents 935f0d2 + 3f6c969 commit 73f3b9b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
8 changes: 8 additions & 0 deletions apps/settings/templates/settings/admin/additional-mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
?>

<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"
Expand Down Expand Up @@ -143,4 +150,5 @@
<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>
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
10 changes: 7 additions & 3 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 @@ -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;
Expand All @@ -267,7 +269,9 @@ protected function getInstance(): MailerInterface {
break;
}

return new SymfonyMailer($transport);
$this->instance = new SymfonyMailer($transport);

return $this->instance;
}

/**
Expand Down
26 changes: 26 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 Expand Up @@ -337,4 +357,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);
}
}

0 comments on commit 73f3b9b

Please sign in to comment.