silvie
Version:
Typescript Back-end Framework
67 lines (66 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = sendMail;
exports.instanceCallback = void 0;
var _nodemailer = _interopRequireDefault(require("nodemailer"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const config = process.configs.mail;
const transporters = {};
const instanceCallback = exports.instanceCallback = {
callback: null
};
async function sendMail(mail, aliasName, to, subject, textBody, htmlBody, attachments) {
if (config) {
if (!config.host) {
throw new Error('Mail host is not defined');
}
if (!config.port) {
throw new Error('Mail port is not defined');
}
if (!config.accounts) {
throw new Error('Mail accounts not found in mail config');
}
} else {
throw new Error('Mail config not found');
}
if (config.enabled) {
const account = config.accounts[mail];
let fromMail = mail;
if (aliasName) fromMail = `"${aliasName}" <${mail}>`;
if (account) {
if (!transporters[mail]) {
let trans = _nodemailer.default.createTransport({
host: config.host,
port: config.port,
secure: config.secure,
tls: {
rejectUnauthorized: config.rejectUnauthorized
},
auth: {
user: account.username,
pass: account.password
}
});
if (instanceCallback.callback instanceof Function) {
trans = instanceCallback.callback(mail, trans);
}
transporters[mail] = trans;
}
const transporter = transporters[mail];
await transporter.sendMail({
from: fromMail,
to,
subject,
text: textBody,
html: htmlBody,
attachments
});
} else {
throw new Error(`Could not find mail '${mail}'`);
}
} else {
throw new Error('Mail is not enabled, you can enabled it in the project mail config file.');
}
}