From 637975305f1adf9bf505507790638cf1e229cfb1 Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Tue, 26 Jan 2021 17:21:18 +0100 Subject: [PATCH] Implemented a basic mailer with reset link sending ref #118 --- src/mailer.ts | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/mailer.ts b/src/mailer.ts index 069e496..f834697 100644 --- a/src/mailer.ts +++ b/src/mailer.ts @@ -1,7 +1,44 @@ - +import nodemailer from 'nodemailer'; +import { MailOptions } from 'nodemailer/lib/json-transport'; +import Mail from 'nodemailer/lib/mailer'; +import { config } from './config'; +import { MailServerConfigError } from './errors/MailErrors'; +import { User } from './models/entities/User'; /** * This class is responsible for all things mail sending. */ export class Mailer { + private transport: Mail; + constructor() { + this.transport = nodemailer.createTransport({ + host: config.mail_server, + port: config.mail_port, + auth: { + user: config.mail_user, + pass: config.mail_password + } + }); + + this.transport.verify(function (error, success) { + if (error) { + throw new MailServerConfigError(); + } + }); + } + + public async sendResetMail(user: User, token: String) { + const reset_link = `${config.app_url}/reset/${token}` + const mail: MailOptions = { + to: user.email, + subject: "LfK! Password Reset", + html: `${reset_link}` + }; + await this.sendMail(mail); + } + + public async sendMail(mail: MailOptions) { + mail.from = config.mail_from; + await this.transport.sendMail(mail); + } }