From 7f454fea8e374c3bea173f4b98dbcee41a005384 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Mon, 16 Apr 2018 15:55:39 +0200 Subject: [PATCH] Provide an option to disable HTML emails Signed-off-by: Morris Jobke --- .../unit/CalDAV/Schedule/IMipPluginTest.php | 6 +++--- config/config.sample.php | 19 ++++++++++++------- lib/private/Mail/Mailer.php | 3 ++- lib/private/Mail/Message.php | 16 ++++++++++------ tests/lib/Mail/MailerTest.php | 5 +++++ tests/lib/Mail/MessageTest.php | 3 ++- 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php b/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php index 56eb00406d..3fe4897502 100644 --- a/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php +++ b/apps/dav/tests/unit/CalDAV/Schedule/IMipPluginTest.php @@ -36,7 +36,7 @@ use Test\TestCase; class IMipPluginTest extends TestCase { public function testDelivery() { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); + $mailMessage = new \OC\Mail\Message(new \Swift_Message(), false); /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */ $mailer = $this->getMockBuilder('OC\Mail\Mailer')->disableOriginalConstructor()->getMock(); $mailer->method('createMessage')->willReturn($mailMessage); @@ -68,7 +68,7 @@ class IMipPluginTest extends TestCase { } public function testFailedDelivery() { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); + $mailMessage = new \OC\Mail\Message(new \Swift_Message(), false); /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */ $mailer = $this->getMockBuilder('OC\Mail\Mailer')->disableOriginalConstructor()->getMock(); $mailer->method('createMessage')->willReturn($mailMessage); @@ -103,7 +103,7 @@ class IMipPluginTest extends TestCase { * @dataProvider dataNoMessageSendForPastEvents */ public function testNoMessageSendForPastEvents($veventParams, $expectsMail) { - $mailMessage = new \OC\Mail\Message(new \Swift_Message()); + $mailMessage = new \OC\Mail\Message(new \Swift_Message(), false); /** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */ $mailer = $this->getMockBuilder('OC\Mail\Mailer')->disableOriginalConstructor()->getMock(); $mailer->method('createMessage')->willReturn($mailMessage); diff --git a/config/config.sample.php b/config/config.sample.php index 57811f2e66..b7f8bd15d0 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -383,6 +383,18 @@ $CONFIG = array( */ 'mail_smtppassword' => '', +/** + * Replaces the default mail template layout. This can be utilized if the + * options to modify the mail texts with the theming app is not enough. + * The class must extend ``\OC\Mail\EMailTemplate`` + */ +'mail_template_class' => '\OC\Mail\EMailTemplate', + +/** + * Email will be send by default with an HTML and a plain text body. This option + * allows to only send plain text emails. + */ +'mail_send_plaintext_only' => false, /** * Proxy Configurations @@ -985,13 +997,6 @@ $CONFIG = array( */ 'systemtags.managerFactory' => '\OC\SystemTag\ManagerFactory', -/** - * Replaces the default mail template layout. This can be utilized if the - * options to modify the mail texts with the theming app is not enough. - * The class must extend ``\OC\Mail\EMailTemplate`` - */ -'mail_template_class' => '\OC\Mail\EMailTemplate', - /** * Maintenance * diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php index 0e42e745e4..5e7f1179c6 100644 --- a/lib/private/Mail/Mailer.php +++ b/lib/private/Mail/Mailer.php @@ -87,7 +87,8 @@ class Mailer implements IMailer { * @return Message */ public function createMessage() { - return new Message(new \Swift_Message()); + $plainTextOnly = $this->config->getSystemValue('mail_send_plaintext_only', false); + return new Message(new \Swift_Message(), $plainTextOnly); } /** diff --git a/lib/private/Mail/Message.php b/lib/private/Mail/Message.php index 3184f26848..6fb1253ebe 100644 --- a/lib/private/Mail/Message.php +++ b/lib/private/Mail/Message.php @@ -33,12 +33,12 @@ use Swift_Message; class Message { /** @var Swift_Message */ private $swiftMessage; + /** @var bool */ + private $plainTextOnly; - /** - * @param Swift_Message $swiftMessage - */ - function __construct(Swift_Message $swiftMessage) { + public function __construct(Swift_Message $swiftMessage, $plainTextOnly) { $this->swiftMessage = $swiftMessage; + $this->plainTextOnly = $plainTextOnly; } /** @@ -229,7 +229,9 @@ class Message { * @return $this */ public function setHtmlBody($body) { - $this->swiftMessage->addPart($body, 'text/html'); + if (!$this->plainTextOnly) { + $this->swiftMessage->addPart($body, 'text/html'); + } return $this; } @@ -247,7 +249,9 @@ class Message { * @return $this */ public function setBody($body, $contentType) { - $this->swiftMessage->setBody($body, $contentType); + if (!$this->plainTextOnly || $contentType !== 'text/html') { + $this->swiftMessage->setBody($body, $contentType); + } return $this; } } diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php index f647b966f1..abe3054afd 100644 --- a/tests/lib/Mail/MailerTest.php +++ b/tests/lib/Mail/MailerTest.php @@ -95,6 +95,11 @@ class MailerTest extends TestCase { } public function testCreateMessage() { + $this->config + ->expects($this->any()) + ->method('getSystemValue') + ->with('mail_send_plaintext_only', false) + ->will($this->returnValue(false)); $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage()); } diff --git a/tests/lib/Mail/MessageTest.php b/tests/lib/Mail/MessageTest.php index 691168ce24..646341b0df 100644 --- a/tests/lib/Mail/MessageTest.php +++ b/tests/lib/Mail/MessageTest.php @@ -9,6 +9,7 @@ namespace Test\Mail; use OC\Mail\Message; +use OCP\Mail\IEMailTemplate; use Swift_Message; use Test\TestCase; @@ -36,7 +37,7 @@ class MessageTest extends TestCase { $this->swiftMessage = $this->getMockBuilder('\Swift_Message') ->disableOriginalConstructor()->getMock(); - $this->message = new Message($this->swiftMessage); + $this->message = new Message($this->swiftMessage, false); } /**