diff options
author | Christoph Wurst <ChristophWurst@users.noreply.github.com> | 2021-06-08 15:43:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-08 15:43:39 +0200 |
commit | 745543d8c04c3e49515ebd9c89bed34312514862 (patch) | |
tree | 61585bb1499b353e79500fecb7905a086ff53cc4 | |
parent | 662ab937e0d30947727be1462f8744681fdd2e49 (diff) | |
parent | 747325fc435cd0d295ba495507facd0bc145d086 (diff) | |
download | nextcloud-server-745543d8c04c3e49515ebd9c89bed34312514862.tar.gz nextcloud-server-745543d8c04c3e49515ebd9c89bed34312514862.zip |
Merge pull request #27306 from nextcloud/enh/noid/set-local-domain-for-swiftmailer
Set local domain for swiftmailer
-rw-r--r-- | lib/private/Mail/Mailer.php | 11 | ||||
-rw-r--r-- | tests/lib/Mail/MailerTest.php | 40 |
2 files changed, 49 insertions, 2 deletions
diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 893a3f0be91..1189907eac9 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -44,11 +44,11 @@ use OCP\IL10N; use OCP\ILogger; use OCP\IURLGenerator; use OCP\L10N\IFactory; +use OCP\Mail\Events\BeforeMessageSent; use OCP\Mail\IAttachment; use OCP\Mail\IEMailTemplate; use OCP\Mail\IMailer; use OCP\Mail\IMessage; -use OCP\Mail\Events\BeforeMessageSent; /** * Class Mailer provides some basic functions to create a mail message that can be used in combination with @@ -292,6 +292,15 @@ class Mailer implements IMailer { $transport->setStreamOptions($streamingOptions); } + $overwriteCliUrl = parse_url( + $this->config->getSystemValueString('overwrite.cli.url', ''), + PHP_URL_HOST + ); + + if (!empty($overwriteCliUrl)) { + $transport->setLocalDomain($overwriteCliUrl); + } + return $transport; } diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index a11a1ab0914..335f373f8c1 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -22,8 +22,8 @@ use OCP\ILogger; use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Mail\Events\BeforeMessageSent; -use Test\TestCase; use Swift_SwiftException; +use Test\TestCase; class MailerTest extends TestCase { /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ @@ -218,4 +218,42 @@ class MailerTest extends TestCase { $mailer = self::invokePrivate($this->mailer, 'getInstance'); $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions())); } + + public function testLocalDomain(): void { + $this->config->method('getSystemValue') + ->willReturnMap([ + ['mail_smtpmode', 'smtp', 'smtp'] + ]); + $this->config->method('getSystemValueString') + ->with('overwrite.cli.url', '') + ->willReturn('https://some.valid.url.com:8080'); + + /** @var \Swift_Mailer $mailer */ + $mailer = self::invokePrivate($this->mailer, 'getInstance'); + self::assertInstanceOf(\Swift_Mailer::class, $mailer); + + /** @var \Swift_Transport_EsmtpTransport $transport */ + $transport = $mailer->getTransport(); + self::assertInstanceOf(\Swift_Transport_EsmtpTransport::class, $transport); + self::assertEquals('some.valid.url.com', $transport->getLocalDomain()); + } + + public function testLocalDomainInvalidUrl(): void { + $this->config->method('getSystemValue') + ->willReturnMap([ + ['mail_smtpmode', 'smtp', 'smtp'] + ]); + $this->config->method('getSystemValueString') + ->with('overwrite.cli.url', '') + ->willReturn('https:only.slash.does.not.work:8080'); + + /** @var \Swift_Mailer $mailer */ + $mailer = self::invokePrivate($this->mailer, 'getInstance'); + self::assertInstanceOf(\Swift_Mailer::class, $mailer); + + /** @var \Swift_Transport_EsmtpTransport $transport */ + $transport = $mailer->getTransport(); + self::assertInstanceOf(\Swift_Transport_EsmtpTransport::class, $transport); + self::assertEquals('[127.0.0.1]', $transport->getLocalDomain()); + } } |