53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
const Config = require('./config.js');
|
|
const nodemailer = require('nodemailer');
|
|
|
|
class Mailer {
|
|
sender;
|
|
started = false;
|
|
mailer;
|
|
constructor(host, port, email, password) {
|
|
this.mailer = nodemailer.createTransport({
|
|
host: host,
|
|
port: port,
|
|
secure: true,
|
|
auth: {
|
|
user: email,
|
|
pass: password
|
|
}
|
|
});
|
|
this.sender = email;
|
|
this.started = true;
|
|
}
|
|
async sendMail(recipients, subject, content, contentStripped) {
|
|
console.log(`sending mail to ${recipients}`);
|
|
let info = await this.mailer.sendMail({
|
|
from: `"Todo App" <${this.sender}>`,
|
|
to: Array.isArray(recipients) ? recipients.join(', ') : recipients,
|
|
subject: subject,
|
|
text: contentStripped,
|
|
html: content
|
|
});
|
|
}
|
|
}
|
|
|
|
if (!global.mailer || !global.mailer.started) {
|
|
if (
|
|
!Config.config['mail_host'] ||
|
|
!Config.config['mail_port'] ||
|
|
!Config.config['mail_username'] ||
|
|
!Config.config['mail_password']
|
|
) {
|
|
console.error(`could not create email account as
|
|
mail_host, mail_port, mail_username or mail_password is not set.`);
|
|
process.exit();
|
|
}
|
|
global.mailer = new Mailer(
|
|
Config.config['mail_host'],
|
|
Config.config['mail_port'],
|
|
Config.config['mail_username'],
|
|
Config.config['mail_password']
|
|
);
|
|
}
|
|
|
|
module.exports = global.mailer;
|