From f109c1a10c9f5acf0d5d3e4f864e3702f99db937 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Sep 2017 10:51:55 +0200 Subject: [PATCH 1/5] Allow templating of email subjects Signed-off-by: Joas Schilling --- lib/private/Mail/EMailTemplate.php | 20 ++++++++++++++++++++ lib/public/Mail/IEMailTemplate.php | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/private/Mail/EMailTemplate.php b/lib/private/Mail/EMailTemplate.php index da3341b6709..e4e28a9294e 100644 --- a/lib/private/Mail/EMailTemplate.php +++ b/lib/private/Mail/EMailTemplate.php @@ -50,6 +50,8 @@ class EMailTemplate implements IEMailTemplate { /** @var array */ protected $data; + /** @var string */ + protected $subject = ''; /** @var string */ protected $htmlBody = ''; /** @var string */ @@ -358,6 +360,15 @@ EOF; $this->data = $data; } + /** + * Sets the subject of the email + * + * @param string $subject + */ + public function setSubject($subject) { + $this->subject = $subject; + } + /** * Adds a header to the email */ @@ -595,6 +606,15 @@ EOF; $this->plainBody .= str_replace('
', PHP_EOL, $text); } + /** + * Returns the rendered email subject as string + * + * @return string + */ + public function renderSubject() { + return $this->subject; + } + /** * Returns the rendered HTML email as string * diff --git a/lib/public/Mail/IEMailTemplate.php b/lib/public/Mail/IEMailTemplate.php index 04a3905c2e3..c1766076c44 100644 --- a/lib/public/Mail/IEMailTemplate.php +++ b/lib/public/Mail/IEMailTemplate.php @@ -52,6 +52,15 @@ namespace OCP\Mail; */ interface IEMailTemplate { + /** + * Sets the subject of the email + * + * @param string $subject + * + * @since 13.0.0 + */ + public function setSubject($subject); + /** * Adds a header to the email * @@ -130,6 +139,15 @@ interface IEMailTemplate { */ public function addFooter($text = ''); + /** + * Returns the rendered email subject as string + * + * @return string + * + * @since 13.0.0 + */ + public function renderSubject(); + /** * Returns the rendered HTML email as string * From 8b37fe7f6534ad16bd9a357036e95e748e2068e3 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Sep 2017 10:59:11 +0200 Subject: [PATCH 2/5] Set the subject with the email template to allow theming Signed-off-by: Joas Schilling --- apps/sharebymail/lib/ShareByMailProvider.php | 14 +++++++------- core/Controller/LostController.php | 3 ++- lib/private/Share20/Manager.php | 4 ++-- settings/Controller/MailSettingsController.php | 3 ++- settings/Hooks.php | 8 ++++++-- settings/Mailer/NewUserMailHelper.php | 3 ++- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 516e4e243bf..6e0664a8147 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -380,8 +380,6 @@ class ShareByMailProvider implements IShareProvider { \DateTime $expiration = null) { $initiatorUser = $this->userManager->get($initiator); $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; - $subject = (string)$this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); - $message = $this->mailer->createMessage(); $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [ @@ -392,6 +390,7 @@ class ShareByMailProvider implements IShareProvider { 'shareWith' => $shareWith, ]); + $emailTemplate->setSubject($this->l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename))); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); $text = $this->l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); @@ -428,7 +427,7 @@ class ShareByMailProvider implements IShareProvider { $emailTemplate->addFooter(); } - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setPlainBody($emailTemplate->renderText()); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); @@ -455,7 +454,6 @@ class ShareByMailProvider implements IShareProvider { $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null; - $subject = (string)$this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName]); $plainBodyPart = $this->l->t("%s shared »%s« with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]); $htmlBodyPart = $this->l->t('%s shared »%s« with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]); @@ -468,6 +466,8 @@ class ShareByMailProvider implements IShareProvider { 'initiatorEmail' => $initiatorEmailAddress, 'shareWith' => $shareWith, ]); + + $emailTemplate->setSubject($this->l->t('Password to access »%s« shared to you by %s', [$filename, $initiatorDisplayName])); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false); $emailTemplate->addBodyText($htmlBodyPart, $plainBodyPart); @@ -491,7 +491,7 @@ class ShareByMailProvider implements IShareProvider { } $message->setTo([$shareWith]); - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setBody($emailTemplate->renderText(), 'text/plain'); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); @@ -524,7 +524,6 @@ class ShareByMailProvider implements IShareProvider { ); } - $subject = (string)$this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith]); $bodyPart = $this->l->t("You just shared »%s« with %s. The share was already send to the recipient. Due to the security policies defined by the administrator of %s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.", [$filename, $shareWith, $this->defaults->getName()]); $message = $this->mailer->createMessage(); @@ -536,6 +535,7 @@ class ShareByMailProvider implements IShareProvider { 'shareWith' => $shareWith, ]); + $emailTemplate->setSubject($this->l->t('Password to access »%s« shared with %s', [$filename, $shareWith])); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l->t('Password to access »%s«', [$filename]), false); $emailTemplate->addBodyText($bodyPart); @@ -547,7 +547,7 @@ class ShareByMailProvider implements IShareProvider { $message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]); } $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]); - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setBody($emailTemplate->renderText(), 'text/plain'); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index 4c59b0a1c00..f4400b0f20b 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -309,6 +309,7 @@ class LostController extends Controller { 'link' => $link, ]); + $emailTemplate->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); $emailTemplate->addHeader(); $emailTemplate->addHeading($this->l10n->t('Password reset')); @@ -327,7 +328,7 @@ class LostController extends Controller { try { $message = $this->mailer->createMessage(); $message->setTo([$email => $user->getUID()]); - $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); + $message->setSubject($emailTemplate->renderSubject()); $message->setPlainBody($emailTemplate->renderText()); $message->setHtmlBody($emailTemplate->renderHtml()); $message->setFrom([$this->from => $this->defaults->getName()]); diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index b2e192b8309..dd2a110daeb 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -702,7 +702,6 @@ class Manager implements IManager { \DateTime $expiration = null) { $initiatorUser = $this->userManager->get($initiator); $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; - $subject = $l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); $message = $this->mailer->createMessage(); @@ -714,6 +713,7 @@ class Manager implements IManager { 'shareWith' => $shareWith, ]); + $emailTemplate->setSubject($l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename))); $emailTemplate->addHeader(); $emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); @@ -750,7 +750,7 @@ class Manager implements IManager { $emailTemplate->addFooter(); } - $message->setSubject($subject); + $message->setSubject($emailTemplate->renderSubject()); $message->setPlainBody($emailTemplate->renderText()); $message->setHtmlBody($emailTemplate->renderHtml()); $this->mailer->send($message); diff --git a/settings/Controller/MailSettingsController.php b/settings/Controller/MailSettingsController.php index 974a95618ad..cb622fa9b12 100644 --- a/settings/Controller/MailSettingsController.php +++ b/settings/Controller/MailSettingsController.php @@ -151,6 +151,7 @@ class MailSettingsController extends Controller { 'displayname' => $displayName, ]); + $template->setSubject($this->l10n->t('Email setting test')); $template->addHeader(); $template->addHeading($this->l10n->t('Well done, %s!', [$displayName])); $template->addBodyText($this->l10n->t('If you received this email, the email configuration seems to be correct.')); @@ -158,7 +159,7 @@ class MailSettingsController extends Controller { $message = $this->mailer->createMessage(); $message->setTo([$email => $displayName]); - $message->setSubject($this->l10n->t('Email setting test')); + $message->setSubject($template->renderSubject()); $message->setHtmlBody($template->renderHtml()); $message->setPlainBody($template->renderText()); $errors = $this->mailer->send($message); diff --git a/settings/Hooks.php b/settings/Hooks.php index 6f537796517..3da85b4ae74 100644 --- a/settings/Hooks.php +++ b/settings/Hooks.php @@ -122,6 +122,8 @@ class Hooks { 'emailAddress' => $user->getEMailAddress(), 'instanceUrl' => $instanceUrl, ]); + + $template->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); $template->addHeader(); $template->addHeading($this->l->t('Password changed for %s', [$user->getDisplayName()]), false); $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); @@ -130,7 +132,7 @@ class Hooks { $message = $this->mailer->createMessage(); $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); - $message->setSubject($this->l->t('Password for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); + $message->setSubject($template->renderSubject()); $message->setBody($template->renderText(), 'text/plain'); $message->setHtmlBody($template->renderHtml()); @@ -193,6 +195,8 @@ class Hooks { 'oldEMailAddress' => $oldMailAddress, 'instanceUrl' => $instanceUrl, ]); + + $template->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); $template->addHeader(); $template->addHeading($this->l->t('Email address changed for %s', [$user->getDisplayName()]), false); $template->addBodyText($text . ' ' . $this->l->t('If you did not request this, please contact an administrator.')); @@ -204,7 +208,7 @@ class Hooks { $message = $this->mailer->createMessage(); $message->setTo([$oldMailAddress => $user->getDisplayName()]); - $message->setSubject($this->l->t('Email address for %1$s changed on %2$s', [$user->getDisplayName(), $instanceUrl])); + $message->setSubject($template->renderSubject()); $message->setBody($template->renderText(), 'text/plain'); $message->setHtmlBody($template->renderHtml()); diff --git a/settings/Mailer/NewUserMailHelper.php b/settings/Mailer/NewUserMailHelper.php index c111ffeb4fa..af1627a08e3 100644 --- a/settings/Mailer/NewUserMailHelper.php +++ b/settings/Mailer/NewUserMailHelper.php @@ -124,6 +124,7 @@ class NewUserMailHelper { 'resetTokenGenerated' => $generatePasswordResetToken, ]); + $emailTemplate->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); $emailTemplate->addHeader(); if ($displayName === $userId) { $emailTemplate->addHeading($this->l10n->t('Welcome aboard')); @@ -159,7 +160,7 @@ class NewUserMailHelper { IEMailTemplate $emailTemplate) { $message = $this->mailer->createMessage(); $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); - $message->setSubject($this->l10n->t('Your %s account was created', [$this->themingDefaults->getName()])); + $message->setSubject($emailTemplate->renderSubject()); $message->setHtmlBody($emailTemplate->renderHtml()); $message->setPlainBody($emailTemplate->renderText()); $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); From c9af36a9ab05e808df526a2054c30364ee02241c Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Sep 2017 10:55:27 +0200 Subject: [PATCH 3/5] Introduce a public interface for Message Signed-off-by: Joas Schilling --- 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 message 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 @@ + + * + * @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 . + * + */ + +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); +} From 3119fd41ceebbdab33c4dd86ce0b60b4dc9010e9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 15 Sep 2017 11:01:21 +0200 Subject: [PATCH 4/5] Set the data from the template Signed-off-by: Joas Schilling --- apps/sharebymail/lib/ShareByMailProvider.php | 12 +--- .../tests/ShareByMailProviderTest.php | 40 +++---------- core/Controller/LostController.php | 4 +- lib/private/Share20/Manager.php | 4 +- .../Controller/MailSettingsController.php | 4 +- settings/Hooks.php | 10 +--- settings/Mailer/NewUserMailHelper.php | 4 +- tests/Core/Controller/LostControllerTest.php | 57 +++++++------------ .../Settings/Mailer/NewUserMailHelperTest.php | 24 +++----- 9 files changed, 46 insertions(+), 113 deletions(-) diff --git a/apps/sharebymail/lib/ShareByMailProvider.php b/apps/sharebymail/lib/ShareByMailProvider.php index 6e0664a8147..f610a1a5fa9 100644 --- a/apps/sharebymail/lib/ShareByMailProvider.php +++ b/apps/sharebymail/lib/ShareByMailProvider.php @@ -427,9 +427,7 @@ class ShareByMailProvider implements IShareProvider { $emailTemplate->addFooter(); } - $message->setSubject($emailTemplate->renderSubject()); - $message->setPlainBody($emailTemplate->renderText()); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } @@ -491,9 +489,7 @@ class ShareByMailProvider implements IShareProvider { } $message->setTo([$shareWith]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setBody($emailTemplate->renderText(), 'text/plain'); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); $this->createPasswordSendActivity($share, $shareWith, false); @@ -547,9 +543,7 @@ class ShareByMailProvider implements IShareProvider { $message->setFrom([$initiatorEMailAddress => $initiatorDisplayName]); } $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setBody($emailTemplate->renderText(), 'text/plain'); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); $this->createPasswordSendActivity($share, $shareWith, true); diff --git a/apps/sharebymail/tests/ShareByMailProviderTest.php b/apps/sharebymail/tests/ShareByMailProviderTest.php index 23e61ee58f1..68a645ec0e6 100644 --- a/apps/sharebymail/tests/ShareByMailProviderTest.php +++ b/apps/sharebymail/tests/ShareByMailProviderTest.php @@ -835,26 +835,14 @@ class ShareByMailProviderTest extends TestCase { ->expects($this->once()) ->method('addFooter') ->with('UnitTestCloud - Testing like 1990'); - $message + $template ->expects($this->once()) ->method('setSubject') - ->willReturn('Mrs. Owner User shared »file.txt« with you'); - $template - ->expects($this->once()) - ->method('renderText') - ->willReturn('Text Render'); + ->with('Mrs. Owner User shared »file.txt« with you'); $message ->expects($this->once()) - ->method('setPlainBody') - ->with('Text Render'); - $template - ->expects($this->once()) - ->method('renderHtml') - ->willReturn('HTML Render'); - $message - ->expects($this->once()) - ->method('setHtmlBody') - ->with('HTML Render'); + ->method('useTemplate') + ->with($template); $this->mailer ->expects($this->once()) ->method('send') @@ -936,26 +924,14 @@ class ShareByMailProviderTest extends TestCase { ->expects($this->once()) ->method('addFooter') ->with(''); - $message + $template ->expects($this->once()) ->method('setSubject') - ->willReturn('Mr. Initiator User shared »file.txt« with you'); - $template - ->expects($this->once()) - ->method('renderText') - ->willReturn('Text Render'); + ->with('Mr. Initiator User shared »file.txt« with you'); $message ->expects($this->once()) - ->method('setPlainBody') - ->with('Text Render'); - $template - ->expects($this->once()) - ->method('renderHtml') - ->willReturn('HTML Render'); - $message - ->expects($this->once()) - ->method('setHtmlBody') - ->with('HTML Render'); + ->method('useTemplate') + ->with($template); $this->mailer ->expects($this->once()) ->method('send') diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php index f4400b0f20b..04a3a07bea2 100644 --- a/core/Controller/LostController.php +++ b/core/Controller/LostController.php @@ -328,10 +328,8 @@ class LostController extends Controller { try { $message = $this->mailer->createMessage(); $message->setTo([$email => $user->getUID()]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setPlainBody($emailTemplate->renderText()); - $message->setHtmlBody($emailTemplate->renderHtml()); $message->setFrom([$this->from => $this->defaults->getName()]); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } catch (\Exception $e) { throw new \Exception($this->l10n->t( diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php index dd2a110daeb..42f2170122e 100644 --- a/lib/private/Share20/Manager.php +++ b/lib/private/Share20/Manager.php @@ -750,9 +750,7 @@ class Manager implements IManager { $emailTemplate->addFooter(); } - $message->setSubject($emailTemplate->renderSubject()); - $message->setPlainBody($emailTemplate->renderText()); - $message->setHtmlBody($emailTemplate->renderHtml()); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } diff --git a/settings/Controller/MailSettingsController.php b/settings/Controller/MailSettingsController.php index cb622fa9b12..02bd51e843e 100644 --- a/settings/Controller/MailSettingsController.php +++ b/settings/Controller/MailSettingsController.php @@ -159,9 +159,7 @@ class MailSettingsController extends Controller { $message = $this->mailer->createMessage(); $message->setTo([$email => $displayName]); - $message->setSubject($template->renderSubject()); - $message->setHtmlBody($template->renderHtml()); - $message->setPlainBody($template->renderText()); + $message->useTemplate($template); $errors = $this->mailer->send($message); if (!empty($errors)) { throw new \RuntimeException($this->l10n->t('Email could not be sent. Check your mail server log')); diff --git a/settings/Hooks.php b/settings/Hooks.php index 3da85b4ae74..59bd7bb905d 100644 --- a/settings/Hooks.php +++ b/settings/Hooks.php @@ -132,10 +132,7 @@ class Hooks { $message = $this->mailer->createMessage(); $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); - $message->setSubject($template->renderSubject()); - $message->setBody($template->renderText(), 'text/plain'); - $message->setHtmlBody($template->renderHtml()); - + $message->useTemplate($template); $this->mailer->send($message); } } @@ -208,10 +205,7 @@ class Hooks { $message = $this->mailer->createMessage(); $message->setTo([$oldMailAddress => $user->getDisplayName()]); - $message->setSubject($template->renderSubject()); - $message->setBody($template->renderText(), 'text/plain'); - $message->setHtmlBody($template->renderHtml()); - + $message->useTemplate($template); $this->mailer->send($message); } } diff --git a/settings/Mailer/NewUserMailHelper.php b/settings/Mailer/NewUserMailHelper.php index af1627a08e3..935b3f0e075 100644 --- a/settings/Mailer/NewUserMailHelper.php +++ b/settings/Mailer/NewUserMailHelper.php @@ -160,10 +160,8 @@ class NewUserMailHelper { IEMailTemplate $emailTemplate) { $message = $this->mailer->createMessage(); $message->setTo([$user->getEMailAddress() => $user->getDisplayName()]); - $message->setSubject($emailTemplate->renderSubject()); - $message->setHtmlBody($emailTemplate->renderHtml()); - $message->setPlainBody($emailTemplate->renderText()); $message->setFrom([$this->fromAddress => $this->themingDefaults->getName()]); + $message->useTemplate($emailTemplate); $this->mailer->send($message); } } diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php index 0bdc11f8a2f..196b1da352b 100644 --- a/tests/Core/Controller/LostControllerTest.php +++ b/tests/Core/Controller/LostControllerTest.php @@ -324,20 +324,9 @@ class LostControllerTest extends \Test\TestCase { ->with(['test@example.com' => 'ExistingUser']); $message ->expects($this->at(1)) - ->method('setSubject') - ->with(' password reset'); - $message - ->expects($this->at(2)) - ->method('setPlainBody') - ->with('text body'); - $message - ->expects($this->at(3)) - ->method('setHtmlBody') - ->with('HTML body'); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['lostpassword-noreply@localhost' => null]); + $emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate->expects($this->any()) ->method('renderHtml') @@ -345,6 +334,12 @@ class LostControllerTest extends \Test\TestCase { $emailTemplate->expects($this->any()) ->method('renderText') ->willReturn('text body'); + + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->mailer ->expects($this->at(0)) ->method('createEMailTemplate') @@ -407,20 +402,9 @@ class LostControllerTest extends \Test\TestCase { ->with(['test@example.com' => 'ExistingUser']); $message ->expects($this->at(1)) - ->method('setSubject') - ->with(' password reset'); - $message - ->expects($this->at(2)) - ->method('setPlainBody') - ->with('text body'); - $message - ->expects($this->at(3)) - ->method('setHtmlBody') - ->with('HTML body'); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['lostpassword-noreply@localhost' => null]); + $emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate->expects($this->any()) ->method('renderHtml') @@ -428,6 +412,12 @@ class LostControllerTest extends \Test\TestCase { $emailTemplate->expects($this->any()) ->method('renderText') ->willReturn('text body'); + + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->mailer ->expects($this->at(0)) ->method('createEMailTemplate') @@ -484,20 +474,9 @@ class LostControllerTest extends \Test\TestCase { ->with(['test@example.com' => 'ExistingUser']); $message ->expects($this->at(1)) - ->method('setSubject') - ->with(' password reset'); - $message - ->expects($this->at(2)) - ->method('setPlainBody') - ->with('text body'); - $message - ->expects($this->at(3)) - ->method('setHtmlBody') - ->with('HTML body'); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['lostpassword-noreply@localhost' => null]); + $emailTemplate = $this->createMock(IEMailTemplate::class); $emailTemplate->expects($this->any()) ->method('renderHtml') @@ -505,6 +484,12 @@ class LostControllerTest extends \Test\TestCase { $emailTemplate->expects($this->any()) ->method('renderText') ->willReturn('text body'); + + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->mailer ->expects($this->at(0)) ->method('createEMailTemplate') diff --git a/tests/Settings/Mailer/NewUserMailHelperTest.php b/tests/Settings/Mailer/NewUserMailHelperTest.php index 1af1bed808c..9fd90561127 100644 --- a/tests/Settings/Mailer/NewUserMailHelperTest.php +++ b/tests/Settings/Mailer/NewUserMailHelperTest.php @@ -620,26 +620,18 @@ EOF; ->expects($this->at(0)) ->method('setTo') ->with(['recipient@example.com' => 'John Doe']); - $this->defaults - ->expects($this->exactly(2)) - ->method('getName') - ->willReturn('TestCloud'); $message ->expects($this->at(1)) - ->method('setSubject') - ->with('Your TestCloud account was created'); - $message - ->expects($this->at(2)) - ->method('setHtmlBody') - ->with($emailTemplate->renderHtml()); - $message - ->expects($this->at(3)) - ->method('setPlainBody') - ->with($emailTemplate->renderText()); - $message - ->expects($this->at(4)) ->method('setFrom') ->with(['no-reply@nextcloud.com' => 'TestCloud']); + $message + ->expects($this->at(2)) + ->method('useTemplate') + ->with($emailTemplate); + $this->defaults + ->expects($this->exactly(1)) + ->method('getName') + ->willReturn('TestCloud'); $this->mailer ->expects($this->once()) ->method('createMessage') From cc798fd65f48e4cfc4748f35859c453a6eff4e25 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 18 Oct 2017 15:12:16 +0200 Subject: [PATCH 5/5] Fix autoloading Signed-off-by: Joas Schilling --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 9341bd1a762..489b9b3dc14 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -226,6 +226,7 @@ return array( 'OCP\\Lockdown\\ILockdownManager' => $baseDir . '/lib/public/Lockdown/ILockdownManager.php', 'OCP\\Mail\\IEMailTemplate' => $baseDir . '/lib/public/Mail/IEMailTemplate.php', 'OCP\\Mail\\IMailer' => $baseDir . '/lib/public/Mail/IMailer.php', + 'OCP\\Mail\\IMessage' => $baseDir . '/lib/public/Mail/IMessage.php', 'OCP\\Migration\\BigIntMigration' => $baseDir . '/lib/public/Migration/BigIntMigration.php', 'OCP\\Migration\\IMigrationStep' => $baseDir . '/lib/public/Migration/IMigrationStep.php', 'OCP\\Migration\\IOutput' => $baseDir . '/lib/public/Migration/IOutput.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 5715ff6ed05..1297f4606f6 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -256,6 +256,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Lockdown\\ILockdownManager' => __DIR__ . '/../../..' . '/lib/public/Lockdown/ILockdownManager.php', 'OCP\\Mail\\IEMailTemplate' => __DIR__ . '/../../..' . '/lib/public/Mail/IEMailTemplate.php', 'OCP\\Mail\\IMailer' => __DIR__ . '/../../..' . '/lib/public/Mail/IMailer.php', + 'OCP\\Mail\\IMessage' => __DIR__ . '/../../..' . '/lib/public/Mail/IMessage.php', 'OCP\\Migration\\BigIntMigration' => __DIR__ . '/../../..' . '/lib/public/Migration/BigIntMigration.php', 'OCP\\Migration\\IMigrationStep' => __DIR__ . '/../../..' . '/lib/public/Migration/IMigrationStep.php', 'OCP\\Migration\\IOutput' => __DIR__ . '/../../..' . '/lib/public/Migration/IOutput.php',