Skip to content

Commit 6b73231

Browse files
committed
Provide an option to disable HTML emails
Signed-off-by: Morris Jobke <hey@morrisjobke.de>
1 parent f7d6eb9 commit 6b73231

5 files changed

Lines changed: 31 additions & 15 deletions

File tree

config/config.sample.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@
383383
*/
384384
'mail_smtppassword' => '',
385385

386+
/**
387+
* Replaces the default mail template layout. This can be utilized if the
388+
* options to modify the mail texts with the theming app is not enough.
389+
* The class must extend ``\OC\Mail\EMailTemplate``
390+
*/
391+
'mail_template_class' => '\OC\Mail\EMailTemplate',
392+
393+
/**
394+
* Email will be send by default with an HTML and a plain text body. This option
395+
* allows to only send plain text emails.
396+
*/
397+
'mail_send_plaintext_only' => false,
386398

387399
/**
388400
* Proxy Configurations
@@ -985,13 +997,6 @@
985997
*/
986998
'systemtags.managerFactory' => '\OC\SystemTag\ManagerFactory',
987999

988-
/**
989-
* Replaces the default mail template layout. This can be utilized if the
990-
* options to modify the mail texts with the theming app is not enough.
991-
* The class must extend ``\OC\Mail\EMailTemplate``
992-
*/
993-
'mail_template_class' => '\OC\Mail\EMailTemplate',
994-
9951000
/**
9961001
* Maintenance
9971002
*

lib/private/Mail/Mailer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public function __construct(IConfig $config,
8787
* @return Message
8888
*/
8989
public function createMessage() {
90-
return new Message(new \Swift_Message());
90+
$plainTextOnly = $this->config->getSystemValue('mail_send_plaintext_only', false);
91+
return new Message(new \Swift_Message(), $plainTextOnly);
9192
}
9293

9394
/**

lib/private/Mail/Message.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
class Message {
3434
/** @var Swift_Message */
3535
private $swiftMessage;
36+
/** @var bool */
37+
private $plainTextOnly;
3638

37-
/**
38-
* @param Swift_Message $swiftMessage
39-
*/
40-
function __construct(Swift_Message $swiftMessage) {
39+
public function __construct(Swift_Message $swiftMessage, $plainTextOnly) {
4140
$this->swiftMessage = $swiftMessage;
41+
$this->plainTextOnly = $plainTextOnly;
4242
}
4343

4444
/**
@@ -229,7 +229,9 @@ public function getPlainBody() {
229229
* @return $this
230230
*/
231231
public function setHtmlBody($body) {
232-
$this->swiftMessage->addPart($body, 'text/html');
232+
if (!$this->plainTextOnly) {
233+
$this->swiftMessage->addPart($body, 'text/html');
234+
}
233235
return $this;
234236
}
235237

@@ -247,7 +249,9 @@ public function getSwiftMessage() {
247249
* @return $this
248250
*/
249251
public function setBody($body, $contentType) {
250-
$this->swiftMessage->setBody($body, $contentType);
252+
if (!$this->plainTextOnly || $contentType !== 'text/html') {
253+
$this->swiftMessage->setBody($body, $contentType);
254+
}
251255
return $this;
252256
}
253257
}

tests/lib/Mail/MailerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public function testGetInstanceSendmail() {
9595
}
9696

9797
public function testCreateMessage() {
98+
$this->config
99+
->expects($this->any())
100+
->method('getSystemValue')
101+
->with('mail_send_plaintext_only', false)
102+
->will($this->returnValue(false));
98103
$this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
99104
}
100105

tests/lib/Mail/MessageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Test\Mail;
1010

1111
use OC\Mail\Message;
12+
use OCP\Mail\IEMailTemplate;
1213
use Swift_Message;
1314
use Test\TestCase;
1415

@@ -36,7 +37,7 @@ function setUp() {
3637
$this->swiftMessage = $this->getMockBuilder('\Swift_Message')
3738
->disableOriginalConstructor()->getMock();
3839

39-
$this->message = new Message($this->swiftMessage);
40+
$this->message = new Message($this->swiftMessage, false);
4041
}
4142

4243
/**

0 commit comments

Comments
 (0)