summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Reschke <lukas@statuscode.ch>2017-04-13 20:02:15 +0200
committerGitHub <noreply@github.com>2017-04-13 20:02:15 +0200
commit81d3732bf51590ca3e3b8abc794d9562346862f5 (patch)
tree0c31d0bd84d07461cb3b439ed256be7f6dc3596c
parentd36751ee38ee3c9994ac6f2e44313b29d893d2f7 (diff)
parent1f962f91154b04c5ec49df14a3e0aa2f4905e1c6 (diff)
downloadnextcloud-server-81d3732bf51590ca3e3b8abc794d9562346862f5.tar.gz
nextcloud-server-81d3732bf51590ca3e3b8abc794d9562346862f5.zip
Merge pull request #4308 from nextcloud/lost-password-email
Update email template for lost password email
-rw-r--r--core/Controller/LostController.php22
-rw-r--r--core/templates/lostpassword/email.php21
-rw-r--r--lib/private/Mail/EMailTemplate.php15
-rw-r--r--lib/public/Mail/IEMailTemplate.php4
-rw-r--r--tests/Core/Controller/LostControllerTest.php58
-rw-r--r--tests/data/emails/new-account-email-single-button.txt2
-rw-r--r--tests/lib/Mail/EMailTemplateTest.php3
7 files changed, 89 insertions, 36 deletions
diff --git a/core/Controller/LostController.php b/core/Controller/LostController.php
index a4768cbeafc..4597124897b 100644
--- a/core/Controller/LostController.php
+++ b/core/Controller/LostController.php
@@ -284,15 +284,29 @@ class LostController extends Controller {
$link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token));
- $tmpl = new \OC_Template('core', 'lostpassword/email');
- $tmpl->assign('link', $link);
- $msg = $tmpl->fetchPage();
+ $emailTemplate = $this->mailer->createEMailTemplate();
+
+ $emailTemplate->addHeader();
+ $emailTemplate->addHeading($this->l10n->t('Password reset'));
+
+ $emailTemplate->addBodyText(
+ $this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.'),
+ $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.')
+ );
+
+ $emailTemplate->addBodyButton(
+ $this->l10n->t('Reset your password'),
+ $link,
+ false
+ );
+ $emailTemplate->addFooter();
try {
$message = $this->mailer->createMessage();
$message->setTo([$email => $user->getUID()]);
$message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()]));
- $message->setPlainBody($msg);
+ $message->setPlainBody($emailTemplate->renderText());
+ $message->setHtmlBody($emailTemplate->renderHTML());
$message->setFrom([$this->from => $this->defaults->getName()]);
$this->mailer->send($message);
} catch (\Exception $e) {
diff --git a/core/templates/lostpassword/email.php b/core/templates/lostpassword/email.php
deleted file mode 100644
index 3ca424d5294..00000000000
--- a/core/templates/lostpassword/email.php
+++ /dev/null
@@ -1,21 +0,0 @@
-<?php
-/**
- * @author Christopher Schäpers <kondou@ts.unde.re>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * 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, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
-echo str_replace('{link}', $_['link'], $l->t('Use the following link to reset your password: {link}'));
diff --git a/lib/private/Mail/EMailTemplate.php b/lib/private/Mail/EMailTemplate.php
index 805126d2ad8..c0949b91c42 100644
--- a/lib/private/Mail/EMailTemplate.php
+++ b/lib/private/Mail/EMailTemplate.php
@@ -423,10 +423,12 @@ EOF;
*
* @param string $text Text of button
* @param string $url URL of button
+ * @param string $plainText Text of button in plain text version
+ * if empty the $text is used, if false none will be used
*
* @since 12.0.0
*/
- public function addBodyButton($text, $url) {
+ public function addBodyButton($text, $url, $plainText = '') {
if ($this->footerAdded) {
return;
}
@@ -436,9 +438,18 @@ EOF;
$this->bodyOpened = true;
}
+ if ($plainText === '') {
+ $plainText = $text;
+ }
+
$color = $this->themingDefaults->getColorPrimary();
$this->htmlBody .= vsprintf($this->button, [$color, $color, $url, $color, htmlspecialchars($text)]);
- $this->plainBody .= $text . ': ' . $url . PHP_EOL;
+
+ if ($plainText !== false) {
+ $this->plainBody .= $plainText . ': ';
+ }
+
+ $this->plainBody .= $url . PHP_EOL;
}
diff --git a/lib/public/Mail/IEMailTemplate.php b/lib/public/Mail/IEMailTemplate.php
index 4e308509c42..41daacdb49f 100644
--- a/lib/public/Mail/IEMailTemplate.php
+++ b/lib/public/Mail/IEMailTemplate.php
@@ -99,10 +99,12 @@ interface IEMailTemplate {
*
* @param string $text Text of button
* @param string $url URL of button
+ * @param string $plainText Text of button in plain text version
+ * if empty the $text is used, if false none will be used
*
* @since 12.0.0
*/
- public function addBodyButton($text, $url);
+ public function addBodyButton($text, $url, $plainText = '');
/**
* Adds a logo and a text to the footer. <br> in the text will be replaced by new lines in the plain text email
diff --git a/tests/Core/Controller/LostControllerTest.php b/tests/Core/Controller/LostControllerTest.php
index c8b8f87e73b..539fe016c8b 100644
--- a/tests/Core/Controller/LostControllerTest.php
+++ b/tests/Core/Controller/LostControllerTest.php
@@ -33,6 +33,7 @@ use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
+use OCP\Mail\IEMailTemplate;
use OCP\Mail\IMailer;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;
@@ -314,17 +315,32 @@ class LostControllerTest extends \Test\TestCase {
$message
->expects($this->at(2))
->method('setPlainBody')
- ->with('Use the following link to reset your password: https://example.tld/index.php/lostpassword/');
+ ->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')
+ ->willReturn('HTML body');
+ $emailTemplate->expects($this->any())
+ ->method('renderText')
+ ->willReturn('text body');
$this->mailer
->expects($this->at(0))
+ ->method('createEMailTemplate')
+ ->willReturn($emailTemplate);
+ $this->mailer
+ ->expects($this->at(1))
->method('createMessage')
->will($this->returnValue($message));
$this->mailer
- ->expects($this->at(1))
+ ->expects($this->at(2))
->method('send')
->with($message);
@@ -385,17 +401,32 @@ class LostControllerTest extends \Test\TestCase {
$message
->expects($this->at(2))
->method('setPlainBody')
- ->with('Use the following link to reset your password: https://example.tld/index.php/lostpassword/');
+ ->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')
+ ->willReturn('HTML body');
+ $emailTemplate->expects($this->any())
+ ->method('renderText')
+ ->willReturn('text body');
$this->mailer
->expects($this->at(0))
+ ->method('createEMailTemplate')
+ ->willReturn($emailTemplate);
+ $this->mailer
+ ->expects($this->at(1))
->method('createMessage')
->will($this->returnValue($message));
$this->mailer
- ->expects($this->at(1))
+ ->expects($this->at(2))
->method('send')
->with($message);
@@ -450,17 +481,32 @@ class LostControllerTest extends \Test\TestCase {
$message
->expects($this->at(2))
->method('setPlainBody')
- ->with('Use the following link to reset your password: https://example.tld/index.php/lostpassword/');
+ ->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')
+ ->willReturn('HTML body');
+ $emailTemplate->expects($this->any())
+ ->method('renderText')
+ ->willReturn('text body');
$this->mailer
->expects($this->at(0))
+ ->method('createEMailTemplate')
+ ->willReturn($emailTemplate);
+ $this->mailer
+ ->expects($this->at(1))
->method('createMessage')
->will($this->returnValue($message));
$this->mailer
- ->expects($this->at(1))
+ ->expects($this->at(2))
->method('send')
->with($message)
->will($this->throwException(new \Exception()));
diff --git a/tests/data/emails/new-account-email-single-button.txt b/tests/data/emails/new-account-email-single-button.txt
index 55954961400..90ae35b0695 100644
--- a/tests/data/emails/new-account-email-single-button.txt
+++ b/tests/data/emails/new-account-email-single-button.txt
@@ -4,7 +4,7 @@ You have now an Nextcloud account, you can add, protect, and share your data.
Your username is: abc
-Set your password: https://example.org/resetPassword/123
+https://example.org/resetPassword/123
--
TestCloud - A safe home for your data
diff --git a/tests/lib/Mail/EMailTemplateTest.php b/tests/lib/Mail/EMailTemplateTest.php
index f9e1ecf29ca..2ba68dbbabb 100644
--- a/tests/lib/Mail/EMailTemplateTest.php
+++ b/tests/lib/Mail/EMailTemplateTest.php
@@ -157,7 +157,8 @@ class EMailTemplateTest extends TestCase {
$this->emailTemplate->addBodyText('You have now an Nextcloud account, you can add, protect, and share your data.');
$this->emailTemplate->addBodyText('Your username is: abc');
$this->emailTemplate->addBodyButton(
- 'Set your password', 'https://example.org/resetPassword/123'
+ 'Set your password', 'https://example.org/resetPassword/123',
+ false
);
$this->emailTemplate->addFooter();