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 11, 2017
1 parent d2de4d5 commit 64873bd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 31 deletions.
18 changes: 14 additions & 4 deletions core/Controller/LostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,25 @@ 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.'));

$emailTemplate->addBodyButton(
$this->l10n->t('Reset your password'),
$link
);
$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.

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

0 comments on commit 64873bd

Please sign in to comment.