@inv2/common
Version:
A common module for v2
68 lines (67 loc) • 3.21 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendEmail = void 0;
const nodemailer_1 = __importDefault(require("nodemailer"));
const nodemailer_zeptomail_transport_1 = require("nodemailer-zeptomail-transport");
const fs_1 = __importDefault(require("fs"));
const handlebars_1 = __importDefault(require("handlebars"));
handlebars_1.default.registerHelper('breaklines', (text) => {
text = handlebars_1.default.Utils.escapeExpression(text);
text = text.replace(/(\r\n|\n|\r)/gm, '<br>');
return new handlebars_1.default.SafeString(text);
});
const zeptomail = new nodemailer_zeptomail_transport_1.ZeptomailTransport({
apiKey: process.env.MAIL_ZEPTO_KEY
});
// const mandrill = new MandrillTransport({
// apiKey: process.env.MANDRILL_KEY!
// });
let transport = nodemailer_1.default.createTransport(zeptomail);
const sendEmail = (options) => __awaiter(void 0, void 0, void 0, function* () {
const replacements = options;
let url = new URL('/emailTemplates/', process.env.BACKEND_BASE).href;
replacements.image =
url +
'assets/' +
(options.images
? options.images[Math.floor(Math.random() * options.images.length)]
: Math.floor(Math.random() * 10) + 1 + '.jpg');
replacements.url = url;
let templatePath = __path.join(__dirname, '../', `/public/templates/${options.template ? options.template : 'default.html'}`);
templatePath = templatePath.replace('config', '');
let html = fs_1.default.readFileSync(templatePath, 'utf-8');
let template = handlebars_1.default.compile(html);
let htmlToSend = template(replacements);
let mailOptions = {
// from: 'InvestNaija <info@investnaija.com>',
from: options.sender,
to: options.email,
subject: options.subject,
replyTo: options.replyTo,
html: htmlToSend,
attachments: options.attachments,
};
// transport.sendMail(mailOptions)
transport.sendMail(mailOptions, function (error, response) {
if (error) {
console.log('Error Sending Email with Template: ', error);
console.log(options.template ? options.template : 'default.html');
// throw error;
return;
}
console.log('Message sent: ' + JSON.stringify(response));
});
});
exports.sendEmail = sendEmail;