Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add "null" SMTP transport mode #48977

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading