parent
c418603423
commit
637975305f
|
@ -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: `<b>${reset_link}</b>`
|
||||
};
|
||||
await this.sendMail(mail);
|
||||
}
|
||||
|
||||
public async sendMail(mail: MailOptions) {
|
||||
mail.from = config.mail_from;
|
||||
await this.transport.sendMail(mail);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue