Move emails to IEmailTemplate

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-07-10 15:06:50 +02:00
parent 7fb215c8e6
commit 2d71c46cad
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
7 changed files with 55 additions and 59 deletions

View File

@ -26,7 +26,6 @@
namespace OCA\Registration\Service; namespace OCA\Registration\Service;
use OCA\Registration\Db\Registration; use OCA\Registration\Db\Registration;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Defaults; use OCP\Defaults;
use OCP\IConfig; use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
@ -75,29 +74,39 @@ class MailService {
/** /**
* @param Registration $registration * @param Registration $registration
* @return bool
* @throws RegistrationException * @throws RegistrationException
*/ */
public function sendTokenByMail(Registration $registration) { public function sendTokenByMail(Registration $registration) {
$link = $this->urlGenerator->linkToRoute('registration.register.verifyToken', ['token' => $registration->getToken()]); $link = $this->urlGenerator->linkToRouteAbsolute('registration.register.verifyToken', ['token' => $registration->getToken()]);
$link = $this->urlGenerator->getAbsoluteURL($link);
$template_var = [
'link' => $link,
'sitename' => $this->defaults->getName()
];
$html_template = new TemplateResponse('registration', 'email.validate_html', $template_var, 'blank');
$html_part = $html_template->render();
$plaintext_template = new TemplateResponse('registration', 'email.validate_plaintext', $template_var, 'blank');
$plaintext_part = $plaintext_template->render();
$subject = $this->l10n->t('Verify your %s registration request', [$this->defaults->getName()]); $subject = $this->l10n->t('Verify your %s registration request', [$this->defaults->getName()]);
$template = $this->mailer->createEMailTemplate('registration_verify', [
'link' => $link,
'token' => $registration->getToken(),
'sitename' => $this->defaults->getName(),
]);
$template->setSubject($subject);
$template->addHeader();
$template->addHeading($this->l10n->t('Registration'));
$body = $this->l10n->t('Email address verified, you can now complete your registration.');
$template->addBodyText(
htmlspecialchars($body . ' ' . $this->l10n->t('Click the button below to continue.')),
$body
);
$template->addBodyButton(
$this->l10n->t('Continue registration'),
$link
);
$template->addFooter();
$from = Util::getDefaultEmailAddress('register'); $from = Util::getDefaultEmailAddress('register');
$message = $this->mailer->createMessage(); $message = $this->mailer->createMessage();
$message->setFrom([$from => $this->defaults->getName()]); $message->setFrom([$from => $this->defaults->getName()]);
$message->setTo([$registration->getEmail()]); $message->setTo([$registration->getEmail()]);
$message->setSubject($subject); $message->useTemplate($template);
$message->setPlainBody($plaintext_part);
$message->setHtmlBody($html_part);
$failed_recipients = $this->mailer->send($message); $failed_recipients = $this->mailer->send($message);
if (!empty($failed_recipients)) { if (!empty($failed_recipients)) {
throw new RegistrationException($this->l10n->t('A problem occurred sending email, please contact your administrator.')); throw new RegistrationException($this->l10n->t('A problem occurred sending email, please contact your administrator.'));
@ -147,40 +156,49 @@ class MailService {
* @throws \Exception * @throws \Exception
*/ */
private function sendNewUserNotifEmail(array $to, $username, $userIsEnabled) { private function sendNewUserNotifEmail(array $to, $username, $userIsEnabled) {
if ($this->config->getAppValue('core', 'vendor', '') === 'nextcloud') { $link = $this->urlGenerator->linkToRouteAbsolute('settings.Users.usersListByGroup', [
$link = $this->urlGenerator->linkToRouteAbsolute('settings.Users.usersList'); 'group' => 'disabled',
} else { ]);
$link = $this->urlGenerator->linkToRouteAbsolute('user_management.users'); $template = $this->mailer->createEMailTemplate('registration_admin', [
} 'link' => $link,
$template_var = [
'user' => $username, 'user' => $username,
'sitename' => $this->defaults->getName(), 'sitename' => $this->defaults->getName(),
'link' => $link, ]);
];
$subject = $this->l10n->t('New user "%s" has created an account on %s', [$username, $this->defaults->getName()]);
$template->setSubject($subject);
$template->addHeader();
$template->addHeading($this->l10n->t('New user registered'));
// handle user enableness
if ($userIsEnabled) { if ($userIsEnabled) {
$html_template_file = 'email.newuser_html'; $template->addBodyText(
$plaintext_template_file = 'email.newuser_plaintext'; $this->l10n->t('"%1$s" registered a new account on %2$s.', [
$username,
$this->defaults->getName(),
])
);
} else { } else {
$html_template_file = 'email.newuser.disabled_html'; $template->addBodyText(
$plaintext_template_file = 'email.newuser.disabled_plaintext'; $this->l10n->t('"%1$s" registered a new account on %2$s and needs to be enabled.', [
} $username,
$this->defaults->getName(),
])
);
$html_template = new TemplateResponse('registration', $html_template_file, $template_var, 'blank'); $template->addBodyButton(
$html_part = $html_template->render(); $this->l10n->t('Enable now'),
$plaintext_template = new TemplateResponse('registration', $plaintext_template_file, $template_var, 'blank'); $link
$plaintext_part = $plaintext_template->render(); );
$subject = $this->l10n->t('A new user "%s" has created an account on %s', [$username, $this->defaults->getName()]); }
$template->addFooter();
$from = Util::getDefaultEmailAddress('register'); $from = Util::getDefaultEmailAddress('register');
$message = $this->mailer->createMessage(); $message = $this->mailer->createMessage();
$message->setFrom([$from => $this->defaults->getName()]); $message->setFrom([$from => $this->defaults->getName()]);
$message->setTo([]); $message->setTo([]);
$message->setBcc($to); $message->setBcc($to);
$message->setSubject($subject); $message->useTemplate($template);
$message->setPlainBody($plaintext_part);
$message->setHtmlBody($html_part);
$failed_recipients = $this->mailer->send($message); $failed_recipients = $this->mailer->send($message);
if (!empty($failed_recipients)) { if (!empty($failed_recipients)) {
throw new RegistrationException('Failed recipients: '.print_r($failed_recipients, true)); throw new RegistrationException('Failed recipients: '.print_r($failed_recipients, true));

View File

@ -1,4 +0,0 @@
<?php
echo $l->t('A new user "%s" has created an account on %s and awaits admin approbation', [$_['user'], $_['sitename']]);
echo str_replace('{link}', $_['link'], '<br/><br/><a href="{link}">{link}</a>');

View File

@ -1,4 +0,0 @@
<?php
echo $l->t('A new user "%s" has created an account on %s and awaits admin approbation', [$_['user'], $_['sitename']]);
echo "\n\n".$_['link'];

View File

@ -1,3 +0,0 @@
<?php
echo $l->t('A new user "%s" has created an account on %s', [$_['user'], $_['sitename']]);

View File

@ -1,3 +0,0 @@
<?php
echo $l->t('A new user "%s" has created an account on %s', [$_['user'], $_['sitename']]);

View File

@ -1,4 +0,0 @@
<?php
echo $l->t('To create a new account on %s, just click the following link:', [$_['sitename']]);
echo str_replace('{link}', $_['link'], '<br/><br/><a href="{link}">{link}</a>');

View File

@ -1,4 +0,0 @@
<?php
echo $l->t("To create a new account on %s, just click the following link:", [$_['sitename']]);
echo "\n\n".$_['link'];