notification-services
Version:
use email, sms and custom notification services for node.js application easily
68 lines (67 loc) • 2.06 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SMTPSendWithoutTemplate = exports.SMTPSendWithTemplate = exports.SMTPSend = exports.SMTPConfig = void 0;
const nodemailer_1 = __importDefault(require("nodemailer"));
const Email = require('email-templates');
const SMTPConfig = (host, user, pass, callback) => {
let transporter = nodemailer_1.default.createTransport({
host,
port: 587,
secure: false,
auth: {
user,
pass, // generated ethereal password
},
tls: {
rejectUnauthorized: false,
},
pool: true,
});
callback({
transporter,
message: 'Connection Setup!'
}, null);
};
exports.SMTPConfig = SMTPConfig;
const SMTPSend = (transporter, mailOptions, template, callback) => {
if (transporter) {
if (template) {
(0, exports.SMTPSendWithTemplate)(transporter, mailOptions, callback);
}
else {
(0, exports.SMTPSendWithoutTemplate)(transporter, mailOptions, callback);
}
}
else {
callback(null, 'Please setup the connection first!');
}
};
exports.SMTPSend = SMTPSend;
const SMTPSendWithTemplate = (transporter, mailOptions, callback) => {
const email = new Email({
transport: transporter,
message: mailOptions,
send: true,
preview: false,
});
callback({
email,
}, null);
};
exports.SMTPSendWithTemplate = SMTPSendWithTemplate;
const SMTPSendWithoutTemplate = (transporter, mailOptions, callback) => {
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
callback(null, err);
return;
}
callback({
envelope: info.envelope,
messageId: info.messageId,
}, null);
});
};
exports.SMTPSendWithoutTemplate = SMTPSendWithoutTemplate;