44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
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';
|
|
/**
|
|
* 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(to_address: string, token: String) {
|
|
const reset_link = `${config.app_url}/reset/${token}`
|
|
const mail: MailOptions = {
|
|
to: to_address,
|
|
subject: "LfK! Password Reset",
|
|
html: `<b>${reset_link}</b>`
|
|
};
|
|
await this.sendMail(mail);
|
|
}
|
|
|
|
public async sendMail(mail: MailOptions) {
|
|
mail.from = config.mail_from;
|
|
await this.transport.sendMail(mail);
|
|
}
|
|
}
|