will-lib
Version:
Library classes
85 lines (84 loc) • 3.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MailLibrary = void 0;
const will_sql_1 = require("will-sql");
const EnvironmentVariable_1 = require("./EnvironmentVariable");
var mailer = require("nodemailer");
class MailLibrary {
static async getMailConfig(conn, category = "CONFIGMAIL") {
let config = { host: EnvironmentVariable_1.MAIL_HOST, port: EnvironmentVariable_1.MAIL_PORT, user: EnvironmentVariable_1.MAIL_USER, pass: EnvironmentVariable_1.MAIL_PASSWORD, from: EnvironmentVariable_1.MAIL_FROM };
if (conn) {
let sql = new will_sql_1.KnSQL("select colname,colvalue from tconfig where category = ?category ");
sql.set("category", category);
console.info(sql);
let rs = await sql.executeQuery(conn);
if (rs.rows && rs.rows.length > 0) {
let title = undefined;
let from = undefined;
for (let i = 0, isz = rs.rows.length; i < isz; i++) {
let row = rs.rows[i];
if (row.colname == "MAIL_SERVER") {
config.host = row.colvalue;
}
else if (row.colname == "MAIL_PORT") {
config.port = parseInt(row.colvalue);
}
else if (row.colname == "MAIL_USER") {
config.user = row.colvalue;
}
else if (row.colname == "MAIL_PASSWORD") {
config.pass = row.colvalue;
}
else if (row.colname == "MAIL_FROM") {
config.from = row.colvalue;
from = row.colvalue;
}
else if (row.colname == "MAIL_TITLE") {
title = row.colvalue;
}
}
if (title && from) {
config.from = '"' + title + '" <' + from + '>';
}
}
}
return Promise.resolve(config);
}
static async sendMail(info, conn, config, category = "CONFIGMAIL") {
console.log("send mail to : ", info.email);
if (!config)
config = await this.getMailConfig(conn, category);
let smtp = {
host: config.host,
port: config.port,
secure: true,
auth: {
user: config.user,
pass: config.pass
}
};
let transport = mailer.createTransport(smtp);
let mail = {
from: config.from,
to: info.email,
subject: info.subject,
html: info.message,
cc: info.cc,
bcc: info.bcc,
attachments: info.attachments
};
return new Promise((resolve, reject) => {
transport.sendMail(mail, function (err, res) {
if (err) {
console.error("send mail error", err);
reject(err);
}
else {
console.log("send mail success", JSON.stringify(res));
resolve(res);
}
});
});
}
}
exports.MailLibrary = MailLibrary;