Skip to content

Commit

Permalink
Update email template for lost password email
Browse files Browse the repository at this point in the history
Signed-off-by: Morris Jobke <[email protected]>
  • Loading branch information
MorrisJobke committed Apr 12, 2017
1 parent b3b2417 commit 1f962f9
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 36 deletions.
22 changes: 18 additions & 4 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,29 @@ protected function sendEmail($input) {

$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) {
Expand Down
21 changes: 0 additions & 21 deletions core/templates/lostpassword/email.php

This file was deleted.

15 changes: 13 additions & 2 deletions lib/private/Mail/EMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,12 @@ public function addBodyButtonGroup($textLeft,
*
* @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;
}
Expand All @@ -436,9 +438,18 @@ public function addBodyButton($text, $url) {
$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;

}

Expand Down
4 changes: 3 additions & 1 deletion lib/public/Mail/IEMailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ public function addBodyButtonGroup($textLeft, $urlLeft, $textRight, $urlRight, $
*
* @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
Expand Down
58 changes: 52 additions & 6 deletions tests/Core/Controller/LostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -314,17 +315,32 @@ public function testEmailSuccessful() {
$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);

Expand Down Expand Up @@ -385,17 +401,32 @@ public function testEmailWithMailSuccessful() {
$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);

Expand Down Expand Up @@ -450,17 +481,32 @@ public function testEmailCantSendException() {
$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()));
Expand Down
2 changes: 1 addition & 1 deletion tests/data/emails/new-account-email-single-button.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion tests/lib/Mail/EMailTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ public function testEMailTemplateSingleButton() {
$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();

Expand Down

0 comments on commit 1f962f9

Please sign in to comment.