parent
b94179e3ca
commit
9bd7636a23
@ -4,12 +4,18 @@ import { MailOptions } from 'nodemailer/lib/json-transport';
|
|||||||
import Mail from 'nodemailer/lib/mailer';
|
import Mail from 'nodemailer/lib/mailer';
|
||||||
import { config } from './config';
|
import { config } from './config';
|
||||||
import { MailServerConfigError } from './errors/MailErrors';
|
import { MailServerConfigError } from './errors/MailErrors';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class is responsible for all things mail sending.
|
* This class is responsible for all things mail sending.
|
||||||
|
* This uses the mail emplates from src/static/mail_templates
|
||||||
*/
|
*/
|
||||||
export class Mailer {
|
export class Mailer {
|
||||||
private transport: Mail;
|
private transport: Mail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The class's default constructor.
|
||||||
|
* Creates the transporter and tests the connection.
|
||||||
|
*/
|
||||||
constructor() {
|
constructor() {
|
||||||
this.transport = nodemailer.createTransport({
|
this.transport = nodemailer.createTransport({
|
||||||
host: config.mail_server,
|
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) {
|
public async sendResetMail(to_address: string, token: String) {
|
||||||
const reset_link = `${config.app_url}/reset/${token}`
|
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`);
|
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);
|
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_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 = {
|
const mail: MailOptions = {
|
||||||
to: to_address,
|
to: to_address,
|
||||||
subject: "LfK! Test Mail",
|
subject: "LfK! Test Mail",
|
||||||
@ -53,6 +68,10 @@ export class Mailer {
|
|||||||
await this.sendMail(mail);
|
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) {
|
public async sendMail(mail: MailOptions) {
|
||||||
mail.from = config.mail_from;
|
mail.from = config.mail_from;
|
||||||
await this.transport.sendMail(mail);
|
await this.transport.sendMail(mail);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user