From c9af36a9ab05e808df526a2054c30364ee02241c Mon Sep 17 00:00:00 2001
From: Joas Schilling <coding@schilljs.com>
Date: Fri, 15 Sep 2017 10:55:27 +0200
Subject: [PATCH] Introduce a public interface for Message

Signed-off-by: Joas Schilling <coding@schilljs.com>
---
 lib/private/Mail/Mailer.php  |  7 +--
 lib/private/Mail/Message.php | 15 ++++++-
 lib/public/Mail/IMailer.php  |  9 ++--
 lib/public/Mail/IMessage.php | 84 ++++++++++++++++++++++++++++++++++++
 4 files changed, 106 insertions(+), 9 deletions(-)
 create mode 100644 lib/public/Mail/IMessage.php

diff --git a/lib/private/Mail/Mailer.php b/lib/private/Mail/Mailer.php
index 45405157d26..43fdb07b810 100644
--- a/lib/private/Mail/Mailer.php
+++ b/lib/private/Mail/Mailer.php
@@ -29,6 +29,7 @@ use OCP\IURLGenerator;
 use OCP\Mail\IEMailTemplate;
 use OCP\Mail\IMailer;
 use OCP\ILogger;
+use OCP\Mail\IMessage;
 
 /**
  * Class Mailer provides some basic functions to create a mail message that can be used in combination with
@@ -84,7 +85,7 @@ class Mailer implements IMailer {
 	/**
 	 * Creates a new message object that can be passed to send()
 	 *
-	 * @return Message
+	 * @return IMessage
 	 */
 	public function createMessage() {
 		return new Message(new \Swift_Message());
@@ -124,13 +125,13 @@ class Mailer implements IMailer {
 	 * Send the specified message. Also sets the from address to the value defined in config.php
 	 * if no-one has been passed.
 	 *
-	 * @param Message $message Message to send
+	 * @param IMessage|Message $message Message to send
 	 * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
 	 * therefore should be considered
 	 * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
 	 * has been supplied.)
 	 */
-	public function send(Message $message) {
+	public function send(IMessage $message) {
 		$debugMode = $this->config->getSystemValue('mail_smtpdebug', false);
 
 		if (empty($message->getFrom())) {
diff --git a/lib/private/Mail/Message.php b/lib/private/Mail/Message.php
index 2d3c49d0ce1..b4d1e4dbe7a 100644
--- a/lib/private/Mail/Message.php
+++ b/lib/private/Mail/Message.php
@@ -23,6 +23,8 @@
 
 namespace OC\Mail;
 
+use OCP\Mail\IEMailTemplate;
+use OCP\Mail\IMessage;
 use Swift_Message;
 
 /**
@@ -30,7 +32,7 @@ use Swift_Message;
  *
  * @package OC\Mail
  */
-class Message {
+class Message implements IMessage {
 	/** @var Swift_Message */
 	private $swiftMessage;
 
@@ -250,4 +252,15 @@ class Message {
 		$this->swiftMessage->setBody($body, $contentType);
 		return $this;
 	}
+
+	/**
+	 * @param IEMailTemplate $emailTemplate
+	 * @return $this
+	 */
+	public function useTemplate(IEMailTemplate $emailTemplate) {
+		$this->setSubject($emailTemplate->renderSubject());
+		$this->setPlainBody($emailTemplate->renderText());
+		$this->setHtmlBody($emailTemplate->renderHtml());
+		return $this;
+	}
 }
diff --git a/lib/public/Mail/IMailer.php b/lib/public/Mail/IMailer.php
index c283d346745..35189c22a69 100644
--- a/lib/public/Mail/IMailer.php
+++ b/lib/public/Mail/IMailer.php
@@ -23,7 +23,6 @@
  */
 
 namespace OCP\Mail;
-use OC\Mail\Message;
 
 /**
  * Class IMailer provides some basic functions to create a mail message that can be used in combination with
@@ -34,7 +33,7 @@ use OC\Mail\Message;
  * 	$mailer = \OC::$server->getMailer();
  * 	$message = $mailer->createMessage();
  * 	$message->setSubject('Your Subject');
- * 	$message->setFrom(['cloud@domain.org' => 'ownCloud Notifier']);
+ * 	$message->setFrom(['cloud@domain.org' => 'Nextcloud Notifier']);
  * 	$message->setTo(['recipient@domain.org' => 'Recipient']);
  * 	$message->setPlainBody('The message text');
  * 	$message->setHtmlBody('The <strong>message</strong> text');
@@ -49,7 +48,7 @@ interface IMailer {
 	/**
 	 * Creates a new message object that can be passed to send()
 	 *
-	 * @return Message
+	 * @return IMessage
 	 * @since 8.1.0
 	 */
 	public function createMessage();
@@ -68,14 +67,14 @@ interface IMailer {
 	 * Send the specified message. Also sets the from address to the value defined in config.php
 	 * if no-one has been passed.
 	 *
-	 * @param Message $message Message to send
+	 * @param IMessage $message Message to send
 	 * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and
 	 * therefore should be considered
 	 * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address
 	 * has been supplied.)
 	 * @since 8.1.0
 	 */
-	public function send(Message $message);
+	public function send(IMessage $message);
 
 	/**
 	 * Checks if an e-mail address is valid
diff --git a/lib/public/Mail/IMessage.php b/lib/public/Mail/IMessage.php
new file mode 100644
index 00000000000..20e4ea19c4c
--- /dev/null
+++ b/lib/public/Mail/IMessage.php
@@ -0,0 +1,84 @@
+<?php
+/**
+ * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\Mail;
+
+/**
+ * Class Message
+ *
+ * @package OCP\Mail
+ * @since 13.0.0
+ */
+interface IMessage {
+	/**
+	 * Set the from address of this message.
+	 *
+	 * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
+	 *
+	 * @param array $addresses Example: array('sender@domain.org', 'other@domain.org' => 'A name')
+	 * @return $this
+	 * @since 13.0.0
+	 */
+	public function setFrom(array $addresses);
+
+	/**
+	 * Set the Reply-To address of this message
+	 *
+	 * @param array $addresses
+	 * @return $this
+	 * @since 13.0.0
+	 */
+	public function setReplyTo(array $addresses);
+
+	/**
+	 * Set the to addresses of this message.
+	 *
+	 * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
+	 * @return $this
+	 * @since 13.0.0
+	 */
+	public function setTo(array $recipients);
+
+	/**
+	 * Set the CC recipients of this message.
+	 *
+	 * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
+	 * @return $this
+	 * @since 13.0.0
+	 */
+	public function setCc(array $recipients);
+
+	/**
+	 * Set the BCC recipients of this message.
+	 *
+	 * @param array $recipients Example: array('recipient@domain.org', 'other@domain.org' => 'A name')
+	 * @return $this
+	 * @since 13.0.0
+	 */
+	public function setBcc(array $recipients);
+
+	/**
+	 * @param IEMailTemplate $emailTemplate
+	 * @return $this
+	 * @since 13.0.0
+	 */
+	public function useTemplate(IEMailTemplate $emailTemplate);
+}
-- 
GitLab