Merge pull request #9231 from nextcloud/13-9205

[stable13] Provide an option to disable HTML emails
This commit is contained in:
Morris Jobke 2018-04-18 22:38:33 +02:00 committed by GitHub
commit 26c153667a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 16 deletions

View File

@ -390,6 +390,18 @@ $CONFIG = array(
*/ */
'mail_smtppassword' => '', '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 * Proxy Configurations
@ -983,13 +995,6 @@ $CONFIG = array(
*/ */
'systemtags.managerFactory' => '\OC\SystemTag\ManagerFactory', '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 * Maintenance
* *

View File

@ -92,7 +92,8 @@ class Mailer implements IMailer {
* @return IMessage * @return IMessage
*/ */
public function createMessage() { 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);
} }
/** /**

View File

@ -38,12 +38,12 @@ use Swift_Message;
class Message implements IMessage { class Message implements IMessage {
/** @var Swift_Message */ /** @var Swift_Message */
private $swiftMessage; private $swiftMessage;
/** @var bool */
private $plainTextOnly;
/** public function __construct(Swift_Message $swiftMessage, $plainTextOnly) {
* @param Swift_Message $swiftMessage
*/
public function __construct(Swift_Message $swiftMessage) {
$this->swiftMessage = $swiftMessage; $this->swiftMessage = $swiftMessage;
$this->plainTextOnly = $plainTextOnly;
} }
/** /**
@ -245,7 +245,9 @@ class Message implements IMessage {
* @return $this * @return $this
*/ */
public function setHtmlBody($body) { public function setHtmlBody($body) {
$this->swiftMessage->addPart($body, 'text/html'); if (!$this->plainTextOnly) {
$this->swiftMessage->addPart($body, 'text/html');
}
return $this; return $this;
} }
@ -263,7 +265,9 @@ class Message implements IMessage {
* @return $this * @return $this
*/ */
public function setBody($body, $contentType) { public function setBody($body, $contentType) {
$this->swiftMessage->setBody($body, $contentType); if (!$this->plainTextOnly || $contentType !== 'text/html') {
$this->swiftMessage->setBody($body, $contentType);
}
return $this; return $this;
} }
@ -274,7 +278,9 @@ class Message implements IMessage {
public function useTemplate(IEMailTemplate $emailTemplate) { public function useTemplate(IEMailTemplate $emailTemplate) {
$this->setSubject($emailTemplate->renderSubject()); $this->setSubject($emailTemplate->renderSubject());
$this->setPlainBody($emailTemplate->renderText()); $this->setPlainBody($emailTemplate->renderText());
$this->setHtmlBody($emailTemplate->renderHtml()); if (!$this->plainTextOnly) {
$this->setHtmlBody($emailTemplate->renderHtml());
}
return $this; return $this;
} }
} }

View File

@ -95,6 +95,11 @@ class MailerTest extends TestCase {
} }
public function testCreateMessage() { 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()); $this->assertInstanceOf('\OC\Mail\Message', $this->mailer->createMessage());
} }

View File

@ -9,6 +9,7 @@
namespace Test\Mail; namespace Test\Mail;
use OC\Mail\Message; use OC\Mail\Message;
use OCP\Mail\IEMailTemplate;
use Swift_Message; use Swift_Message;
use Test\TestCase; use Test\TestCase;
@ -36,7 +37,7 @@ class MessageTest extends TestCase {
$this->swiftMessage = $this->getMockBuilder('\Swift_Message') $this->swiftMessage = $this->getMockBuilder('\Swift_Message')
->disableOriginalConstructor()->getMock(); ->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>'); $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);
}
} }