From 9bd7636a23b5a9662ea2b965d2a2407727a188fb Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Wed, 27 Jan 2021 11:37:46 +0100 Subject: [PATCH] Added comments ref #124 --- src/mailer.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/mailer.ts b/src/mailer.ts index 13475f0..d52e540 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -4,12 +4,18 @@ import { MailOptions } from 'nodemailer/lib/json-transport'; import Mail from 'nodemailer/lib/mailer'; import { config } from './config'; import { MailServerConfigError } from './errors/MailErrors'; + /** * This class is responsible for all things mail sending. + * This uses the mail emplates from src/static/mail_templates */ export class Mailer { private transport: Mail; + /** + * The class's default constructor. + * Creates the transporter and tests the connection. + */ constructor() { this.transport = nodemailer.createTransport({ host: config.mail_server, @@ -27,6 +33,11 @@ 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. + */ public async sendResetMail(to_address: string, token: String) { const reset_link = `${config.app_url}/reset/${token}` const body_html = fs.readFileSync(__dirname + '/static/mail_templates/pw-reset.html', { encoding: 'utf8' }).replace("{{reset_link}}", reset_link).replace("{{recipient_mail}}", to_address).replace("{{copyright_owner}}", "LfK!").replace("{{link_imprint}}", `${config.app_url}/imprint`).replace("{{link_privacy}}", `${config.app_url}/privacy`); @@ -41,9 +52,13 @@ export class Mailer { await this.sendMail(mail); } - public async sendTestMail(to_address: string) { + /** + * Function for sending a test mail from the test mail template. + * @param to_address The address the test mail will be sent to - this is the configured from-address by default. + */ + public async sendTestMail(to_address: string = config.mail_from) { const body_html = fs.readFileSync(__dirname + '/static/mail_templates/test.html', { encoding: 'utf8' }).replace("{{recipient_mail}}", to_address).replace("{{copyright_owner}}", "LfK!").replace("{{link_imprint}}", `${config.app_url}/imprint`).replace("{{link_privacy}}", `${config.app_url}/privacy`); - const body_txt = fs.readFileSync(__dirname + '/static/mail_templates/test.html', { encoding: 'utf8' }).replace("{{recipient_mail}}", to_address).replace("{{copyright_owner}}", "LfK!").replace("{{link_imprint}}", `${config.app_url}/imprint`).replace("{{link_privacy}}", `${config.app_url}/privacy`); + const body_txt = fs.readFileSync(__dirname + '/static/mail_templates/test.txt', { encoding: 'utf8' }).replace("{{recipient_mail}}", to_address).replace("{{copyright_owner}}", "LfK!").replace("{{link_imprint}}", `${config.app_url}/imprint`).replace("{{link_privacy}}", `${config.app_url}/privacy`); const mail: MailOptions = { to: to_address, subject: "LfK! Test Mail", @@ -53,6 +68,10 @@ export class Mailer { await this.sendMail(mail); } + /** + * Wrapper function for sending a mail via this object's transporter. + * @param mail MailOptions object containing the + */ public async sendMail(mail: MailOptions) { mail.from = config.mail_from; await this.transport.sendMail(mail);