From 4994cb0489b168ecc9f18103fdea5e578bb9ef74 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 3 Mar 2021 17:54:12 +0100 Subject: [PATCH] Added all missing config vars/interpolations ref #2 --- src/Mailer.ts | 59 +++++++++++++++++++++++++++---- src/config.ts | 3 +- src/templates/welcome_runner.html | 4 +-- src/templates/welcome_runner.txt | 2 +- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/Mailer.ts b/src/Mailer.ts index 93597d6..d2752f8 100644 --- a/src/Mailer.ts +++ b/src/Mailer.ts @@ -16,7 +16,7 @@ import { MailServerConfigError } from './errors/MailErrors'; */ export class Mailer { private transport: Mail; - private static interpolations = { copyright_owner: config.copyright_owner } + private static interpolations = { copyright_owner: config.copyright_owner, event_name: config.event_name, contact_mail: config.contact_mail } /** * Main constructor. @@ -69,11 +69,18 @@ export class Mailer { public async sendResetMail(to_address: string, token: string, locale: string = "en") { await i18next.changeLanguage(locale); - const reset_link = `${config.app_url}/reset/${(Buffer.from(token)).toString("base64")}` + const replacements = { + recipient_mail: to_address, + copyright_owner: config.copyright_owner, + link_imprint: `${config.app_url}/imprint`, + link_privacy: `${config.app_url}/privacy`, + reset_link: `${config.app_url}/reset/${(Buffer.from(token)).toString("base64")}` + } + const template_html = Handlebars.compile(fs.readFileSync(__dirname + '/templates/pw-reset.html', { encoding: 'utf8' })); const template_txt = Handlebars.compile(fs.readFileSync(__dirname + '/templates/pw-reset.txt', { encoding: 'utf8' })); - const body_html = template_html({ recipient_mail: to_address, copyright_owner: config.copyright_owner, link_imprint: `${config.app_url}/imprint`, link_privacy: `${config.app_url}/privacy`, reset_link }); - const body_txt = template_txt({ recipient_mail: to_address, copyright_owner: config.copyright_owner, link_imprint: `${config.app_url}/imprint`, link_privacy: `${config.app_url}/privacy`, reset_link }); + const body_html = template_html(replacements); + const body_txt = template_txt(replacements); const mail: MailOptions = { to: to_address, @@ -91,13 +98,18 @@ export class Mailer { public async sendTestMail(locale: string = "en") { await i18next.changeLanguage(locale); const to_address: string = config.mail_from; + const replacements = { + recipient_mail: to_address, + copyright_owner: config.copyright_owner, + link_imprint: `${config.app_url}/imprint`, + link_privacy: `${config.app_url}/privacy` + } const template_html = Handlebars.compile(fs.readFileSync(__dirname + '/templates/test.html', { encoding: 'utf8' })); const template_txt = Handlebars.compile(fs.readFileSync(__dirname + '/templates/test.txt', { encoding: 'utf8' })); - const body_html = template_html({ recipient_mail: to_address, copyright_owner: config.copyright_owner, link_imprint: `${config.app_url}/imprint`, link_privacy: `${config.app_url}/privacy` }); - const body_txt = template_txt({ recipient_mail: to_address, copyright_owner: config.copyright_owner, link_imprint: `${config.app_url}/imprint`, link_privacy: `${config.app_url}/privacy` }); + const body_html = template_html(replacements); + const body_txt = template_txt(replacements); - fs.writeFileSync("./test.tmp", body_txt); const mail: MailOptions = { to: to_address, subject: i18next.t("test-mail", Mailer.interpolations).toString(), @@ -107,6 +119,39 @@ export class Mailer { await this.sendMail(mail); } + /** + * Function for sending a reset mail from the reset mail template. + * @param to_address The address the mail will be sent to. Should always get pulled from a user object. + * @param token The requested password reset token - will be combined with the app_url to generate a password reset link. + */ + public async sendWelcomeMail(to_address: string, token: string, locale: string = "en") { + await i18next.changeLanguage(locale); + + const replacements = { + recipient_mail: to_address, + copyright_owner: config.copyright_owner, + link_imprint: `${config.app_url}/imprint`, + link_privacy: `${config.app_url}/privacy`, + selfservice_link: `${config.app_url}/selfservice/profile/${token}`, + forgot_link: `${config.app_url}/selfservice`, + contact_mail: config.contact_mail, + event_name: config.event_name + } + + const template_html = Handlebars.compile(fs.readFileSync(__dirname + '/templates/welcome_runner.html', { encoding: 'utf8' })); + const template_txt = Handlebars.compile(fs.readFileSync(__dirname + '/templates/welcome_runner.txt', { encoding: 'utf8' })); + const body_html = template_html(replacements); + const body_txt = template_txt(replacements); + + const mail: MailOptions = { + to: to_address, + subject: i18next.t("event_name-registration", Mailer.interpolations).toString(), + text: body_txt, + html: body_html + }; + await this.sendMail(mail); + } + /** * Wrapper function for sending a mail via this object's transporter. * @param mail MailOptions object containing the diff --git a/src/config.ts b/src/config.ts index e1eaca6..2464181 100644 --- a/src/config.ts +++ b/src/config.ts @@ -16,7 +16,8 @@ export const config = { privacy_url: process.env.PRIVACY_URL || "/privacy", imprint_url: process.env.IMPRINT_URL || "/imprint", copyright_owner: process.env.COPYRIGHT_OWNER || "LfK!", - event_name: process.env.EVENT_NAME || "Testing 4 Kaya!" + event_name: process.env.EVENT_NAME || "Testing 4 Kaya!", + contact_mail: process.env.CONTACT_MAIL || process.env.MAIL_FROM, } let errors = 0 if (typeof config.internal_port !== "number") { diff --git a/src/templates/welcome_runner.html b/src/templates/welcome_runner.html index c0acbdf..cf35fa8 100644 --- a/src/templates/welcome_runner.html +++ b/src/templates/welcome_runner.html @@ -322,7 +322,7 @@
- {{__ "view-my-data"}} + {{__ "view-my-data"}}
@@ -331,7 +331,7 @@ -

Link: {{selfservice_url}}
+

Link: {{selfservice_link}}
{{__ "if-you-ever-loose-the-link-you-can-request-a-new-one-by-visiting-our-website"}} {{forgot_link}}

diff --git a/src/templates/welcome_runner.txt b/src/templates/welcome_runner.txt index 2d78482..a77b00b 100644 --- a/src/templates/welcome_runner.txt +++ b/src/templates/welcome_runner.txt @@ -7,7 +7,7 @@ The only thing you have to do now is to bring your registration code with you. You can view your registration code, lap times and much more by visiting our selfservice. This is your private selfservice link: -{{selfservice_url}} +{{selfservice_link}} If you ever loose the link you can request a new one by visiting our website: {{forgot_link}}