summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2018-04-16 15:55:39 +0200
committerMorris Jobke <hey@morrisjobke.de>2018-04-18 16:59:34 +0200
commit01278056a568d987a5119573d9831832bd122d6f (patch)
treeddfa7dbb6d70a268899f9140ac66c1e7c123bd96
parentfa65aaf1fc8836e1a0e5ee1ffe38ee032a487892 (diff)
downloadnextcloud-server-01278056a568d987a5119573d9831832bd122d6f.tar.gz
nextcloud-server-01278056a568d987a5119573d9831832bd122d6f.zip
Provide an option to disable HTML emails
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
-rw-r--r--config/config.sample.php19
-rw-r--r--lib/private/Mail/Mailer.php3
-rw-r--r--lib/private/Mail/Message.php20
-rw-r--r--tests/lib/Mail/MailerTest.php5
-rw-r--r--tests/lib/Mail/MessageTest.php49
5 files changed, 80 insertions, 16 deletions
diff --git a/config/config.sample.php b/config/config.sample.php
index 5f29933ec65..1f86aebc5d9 100644
--- a/config/config.sample.php
+++ b/config/config.sample.php
@@ -390,6 +390,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
@@ -984,13 +996,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
*
* These options are for halting user activity when you are performing server
diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php
index 0590f3db12a..542d452afe8 100644
--- a/lib/private/Mail/Mailer.php
+++ b/lib/private/Mail/Mailer.php
@@ -92,7 +92,8 @@ class Mailer implements IMailer {
* @return IMessage
*/
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 4d9458fbd40..4b42b640356 100644
--- a/lib/private/Mail/Message.php
+++ b/lib/private/Mail/Message.php
@@ -38,12 +38,12 @@ use Swift_Message;
class Message implements IMessage {
/** @var Swift_Message */
private $swiftMessage;
+ /** @var bool */
+ private $plainTextOnly;
- /**
- * @param Swift_Message $swiftMessage
- */
- public function __construct(Swift_Message $swiftMessage) {
+ public function __construct(Swift_Message $swiftMessage, $plainTextOnly) {
$this->swiftMessage = $swiftMessage;
+ $this->plainTextOnly = $plainTextOnly;
}
/**
@@ -245,7 +245,9 @@ class Message implements IMessage {
* @return $this
*/
public function setHtmlBody($body) {
- $this->swiftMessage->addPart($body, 'text/html');
+ if (!$this->plainTextOnly) {
+ $this->swiftMessage->addPart($body, 'text/html');
+ }
return $this;
}
@@ -263,7 +265,9 @@ class Message implements IMessage {
* @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;
}
@@ -274,7 +278,9 @@ class Message implements IMessage {
public function useTemplate(IEMailTemplate $emailTemplate) {
$this->setSubject($emailTemplate->renderSubject());
$this->setPlainBody($emailTemplate->renderText());
- $this->setHtmlBody($emailTemplate->renderHtml());
+ if (!$this->plainTextOnly) {
+ $this->setHtmlBody($emailTemplate->renderHtml());
+ }
return $this;
}
}
diff --git a/tests/lib/Mail/MailerTest.php b/tests/lib/Mail/MailerTest.php
index f647b966f1f..abe3054afd6 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 691168ce24c..336b5e8c3ca 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);
}
/**
@@ -180,4 +181,50 @@ class MessageTest extends TestCase {
$this->message->setHtmlBody('<blink>Fancy Body</blink>');
}
+ public function testPlainTextRenderOption() {
+ /** @var \PHPUnit_Framework_MockObject_MockObject|Swift_Message $swiftMessage */
+ $swiftMessage = $this->getMockBuilder('\Swift_Message')
+ ->disableOriginalConstructor()->getMock();
+ /** @var \PHPUnit_Framework_MockObject_MockObject|IEMailTemplate $template */
+ $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
+ ->disableOriginalConstructor()->getMock();
+
+ $message = new Message($swiftMessage, true);
+
+ $template
+ ->expects($this->never())
+ ->method('renderHTML');
+ $template
+ ->expects($this->once())
+ ->method('renderText');
+ $template
+ ->expects($this->once())
+ ->method('renderSubject');
+
+ $message->useTemplate($template);
+ }
+
+ public function testBothRenderingOptions() {
+ /** @var \PHPUnit_Framework_MockObject_MockObject|Swift_Message $swiftMessage */
+ $swiftMessage = $this->getMockBuilder('\Swift_Message')
+ ->disableOriginalConstructor()->getMock();
+ /** @var \PHPUnit_Framework_MockObject_MockObject|IEMailTemplate $template */
+ $template = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
+ ->disableOriginalConstructor()->getMock();
+
+ $message = new Message($swiftMessage, false);
+
+ $template
+ ->expects($this->once())
+ ->method('renderHTML');
+ $template
+ ->expects($this->once())
+ ->method('renderText');
+ $template
+ ->expects($this->once())
+ ->method('renderSubject');
+
+ $message->useTemplate($template);
+ }
+
}