From 72d97b44a752dc850f35c5ba830ae49bf3471815 Mon Sep 17 00:00:00 2001 From: Branko Kokanovic Date: Fri, 30 Nov 2018 21:06:44 +0100 Subject: [PATCH 1/3] Expose Swift Mailer streaming options in config, fixes #12702 Signed-off-by: Branko Kokanovic --- config/config.sample.php | 7 +++++++ lib/private/Mail/Mailer.php | 4 ++++ tests/lib/Mail/MailerTest.php | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/config/config.sample.php b/config/config.sample.php index 7a86070c18..299e67c98f 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -421,6 +421,13 @@ $CONFIG = array( */ 'mail_send_plaintext_only' => false, +/** + * This depends on ``mail_smtpmode``. Array of additional streams options that + * will be passed to underlying Swift mailer implementation. + * Defaults to an empty array. + */ +'mail_smtpstreamoptions' => array(), + /** * Which mode is used for sendmail/qmail: ``smtp`` or ``pipe``. * diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 7a8b4ad259..2dad72b5b1 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -259,6 +259,10 @@ class Mailer implements IMailer { if (!empty($smtpSecurity)) { $transport->setEncryption($smtpSecurity); } + $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', array()); + if (is_array($streamingOptions) && count($streamingOptions) > 0) { + $transport->setStreamOptions($streamingOptions); + } return $transport; } diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index 4117498885..8c309335ab 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -167,4 +167,26 @@ class MailerTest extends TestCase { $this->assertSame(EMailTemplate::class, get_class($this->mailer->createEMailTemplate('tests.MailerTest'))); } + + public function testStreamingOptions() { + $this->config->method('getSystemValue') + ->will($this->returnValueMap([ + ['mail_smtpmode', 'smtp', 'smtp'], + ['mail_smtpstreamoptions', array(), array('foo' => 1)] + ])); + $mailer = self::invokePrivate($this->mailer, 'getInstance'); + $this->assertEquals(1, count($mailer->getTransport()->getStreamOptions())); + $this->assertTrue(isset($mailer->getTransport()->getStreamOptions()['foo'])); + + } + + public function testStreamingOptionsWrongType() { + $this->config->method('getSystemValue') + ->will($this->returnValueMap([ + ['mail_smtpmode', 'smtp', 'smtp'], + ['mail_smtpstreamoptions', array(), 'bar'] + ])); + $mailer = self::invokePrivate($this->mailer, 'getInstance'); + $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions())); + } } From 8101059062a94365d3b86eb6663376188dda5512 Mon Sep 17 00:00:00 2001 From: Branko Kokanovic Date: Mon, 17 Dec 2018 20:00:42 +0100 Subject: [PATCH 2/3] Addressing comments, count to empty Signed-off-by: Branko Kokanovic --- lib/private/Mail/Mailer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 2dad72b5b1..03ffcd9d60 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -260,7 +260,7 @@ class Mailer implements IMailer { $transport->setEncryption($smtpSecurity); } $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', array()); - if (is_array($streamingOptions) && count($streamingOptions) > 0) { + if (is_array($streamingOptions) && !empty($streamingOptions)) { $transport->setStreamOptions($streamingOptions); } From 7cef9b0248d5d2e026f4d74668e36c3642796e8e Mon Sep 17 00:00:00 2001 From: Branko Kokanovic Date: Mon, 17 Dec 2018 23:34:48 +0100 Subject: [PATCH 3/3] Addressing comments, array() to [] Signed-off-by: Branko Kokanovic --- lib/private/Mail/Mailer.php | 2 +- tests/lib/Mail/MailerTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 03ffcd9d60..d103e1380c 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -259,7 +259,7 @@ class Mailer implements IMailer { if (!empty($smtpSecurity)) { $transport->setEncryption($smtpSecurity); } - $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', array()); + $streamingOptions = $this->config->getSystemValue('mail_smtpstreamoptions', []); if (is_array($streamingOptions) && !empty($streamingOptions)) { $transport->setStreamOptions($streamingOptions); } diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index 8c309335ab..1913cc1176 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -172,7 +172,7 @@ class MailerTest extends TestCase { $this->config->method('getSystemValue') ->will($this->returnValueMap([ ['mail_smtpmode', 'smtp', 'smtp'], - ['mail_smtpstreamoptions', array(), array('foo' => 1)] + ['mail_smtpstreamoptions', [], ['foo' => 1]] ])); $mailer = self::invokePrivate($this->mailer, 'getInstance'); $this->assertEquals(1, count($mailer->getTransport()->getStreamOptions())); @@ -184,7 +184,7 @@ class MailerTest extends TestCase { $this->config->method('getSystemValue') ->will($this->returnValueMap([ ['mail_smtpmode', 'smtp', 'smtp'], - ['mail_smtpstreamoptions', array(), 'bar'] + ['mail_smtpstreamoptions', [], 'bar'] ])); $mailer = self::invokePrivate($this->mailer, 'getInstance'); $this->assertEquals(0, count($mailer->getTransport()->getStreamOptions()));