From 602600db895a01ca72182e3e2d6094a5e20d4b85 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 2 Mar 2021 16:19:43 +0100 Subject: [PATCH] Added function for sending test mail --- src/Mailer.ts | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Mailer.ts b/src/Mailer.ts index 4133247..2fb7adf 100644 --- a/src/Mailer.ts +++ b/src/Mailer.ts @@ -63,16 +63,16 @@ export class Mailer { } /** - * Function for sending a test mail from the test 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. - */ + * Function for sending a test mail from the test 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 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 template_html = Handlebars.compile(fs.readFileSync(__dirname + '/static/mail_templates/pw-reset.html', { encoding: 'utf8' })); - const template_txt = Handlebars.compile(fs.readFileSync(__dirname + '/static/mail_templates/pw-reset.html', { encoding: 'utf8' })); + 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 }); @@ -85,6 +85,29 @@ export class Mailer { await this.sendMail(mail); } + /** + * Function for sending a test mail from the test 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 sendTestMail(to_address: string = config.mail_from, locale: string = "en") { + await i18next.changeLanguage(locale); + + 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` }); + + fs.writeFileSync("./test.tmp", body_txt); + const mail: MailOptions = { + to: to_address, + subject: i18next.t("subject_reset", 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