bulk-mail-cli
Version:
Do quick, hassle-free email marketing with this small but very powerful tool! 🔥
177 lines • 10.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var fs = require("fs");
var chalk = require("chalk");
var cron = require("node-cron");
var csvToJson = require("csvtojson");
var createTransport_1 = require("../../utils/createTransport");
var stringProcessor_1 = require("../../utils/stringProcessor");
var isForLoop_1 = require("../../utils/isForLoop");
function massMail(configData, shouldRestart) {
var _a, _b;
return tslib_1.__awaiter(this, void 0, void 0, function () {
function sendMail(row, task) {
var _a, _b, _c, _d;
return tslib_1.__awaiter(this, void 0, void 0, function () {
var processString, error_1;
return tslib_1.__generator(this, function (_e) {
switch (_e.label) {
case 0:
processString = function (string) {
return stringProcessor_1.default(string, tslib_1.__assign(tslib_1.__assign({}, row), process.env));
};
_e.label = 1;
case 1:
_e.trys.push([1, 4, , 5]);
return [4, transporter.sendMail({
from: processString((_a = configData === null || configData === void 0 ? void 0 : configData.mail) === null || _a === void 0 ? void 0 : _a.from),
subject: processString((_b = configData === null || configData === void 0 ? void 0 : configData.mail) === null || _b === void 0 ? void 0 : _b.subject),
html: processString(htmlData),
to: processString(row.email),
attachments: configData.mail.attachments
? (_c = configData === null || configData === void 0 ? void 0 : configData.mail) === null || _c === void 0 ? void 0 : _c.attachments.map(function (attachment) { return (tslib_1.__assign(tslib_1.__assign({}, attachment), { filename: processString(attachment.filename), path: (attachment === null || attachment === void 0 ? void 0 : attachment.path.startsWith('http')) ? processString(attachment.path)
: processString(configurationDirPath.join('/') + "/" + (attachment === null || attachment === void 0 ? void 0 : attachment.path)) })); }) : [],
})];
case 2:
_e.sent();
if (!sentTo)
sentTo = [];
sentTo.push(row.email);
return [4, fs.writeFileSync(configData.jsonConfPath, JSON.stringify(tslib_1.__assign(tslib_1.__assign({}, configDataToWriteFile), { nonUserData: {
sentTo: sentTo,
} }), null, 2))];
case 3:
_e.sent();
((_d = configData === null || configData === void 0 ? void 0 : configData.configuration) === null || _d === void 0 ? void 0 : _d.verbose) !== false &&
console.log("" + chalk.yellow("Mail sent to " + row.email + "."));
return [3, 5];
case 4:
error_1 = _e.sent();
isError = true;
logResult();
if (typeof task === 'object')
task.destroy();
else
return [2, 'break'];
return [3, 5];
case 5:
dataIndex++;
if (dataIndex > csvDataWithoutSentTo.length - 1) {
logResult();
if (typeof task === 'object')
task.destroy();
else
return [2, 'break'];
}
return [2];
}
});
});
}
var transporter, configurationDirPath, csvPath, csvData, htmlPath, htmlData, isError, logResult, sentTo, csvDataWithoutSentTo, configDataToWriteFile, mailInterval, dataIndex, csvDataWithoutSentTo_1, csvDataWithoutSentTo_1_1, row, sendingProcess, e_1_1, task_1;
var e_1, _c;
var _this = this;
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
transporter = createTransport_1.default(configData);
configurationDirPath = configData.jsonConfPath.split('/');
configurationDirPath.pop();
csvPath = configurationDirPath.join('/') + "/" + configData.mail.to;
return [4, csvToJson().fromFile(csvPath)];
case 1:
csvData = _d.sent();
htmlPath = configurationDirPath.join('/') + "/" + configData.mail.theme;
return [4, fs.readFileSync(htmlPath, 'utf8')];
case 2:
htmlData = _d.sent();
isError = false;
logResult = function () {
if (isError) {
console.log(chalk.red.bold("\nProcess Exited. Some grave error occured. Check your configuration file.") + "\n " + chalk.cyan("\nDo not worry, your campaign will be resumed from where you stopped.") + "\n " + chalk.yellow("\nMake sure that your internet connection is alright.") + "\n ");
}
else
console.log("" + chalk.green.bold("\nProcess exited."));
};
sentTo = (_a = configData === null || configData === void 0 ? void 0 : configData.nonUserData) === null || _a === void 0 ? void 0 : _a.sentTo;
if (shouldRestart) {
csvDataWithoutSentTo = csvData;
}
else {
csvDataWithoutSentTo = tslib_1.__spread(csvData).filter(function (row) {
if (sentTo)
return !sentTo.includes(row.email);
return true;
});
}
configDataToWriteFile = Object.assign({}, configData);
delete configDataToWriteFile.jsonConfPath;
mailInterval = ((_b = configData === null || configData === void 0 ? void 0 : configData.configuration) === null || _b === void 0 ? void 0 : _b.mailInterval) || '*/10 * * * * *';
dataIndex = 0;
if (!(csvDataWithoutSentTo.length > 0)) return [3, 13];
if (!isForLoop_1.default(mailInterval)) return [3, 11];
_d.label = 3;
case 3:
_d.trys.push([3, 8, 9, 10]);
csvDataWithoutSentTo_1 = tslib_1.__values(csvDataWithoutSentTo), csvDataWithoutSentTo_1_1 = csvDataWithoutSentTo_1.next();
_d.label = 4;
case 4:
if (!!csvDataWithoutSentTo_1_1.done) return [3, 7];
row = csvDataWithoutSentTo_1_1.value;
return [4, sendMail(row, 'forloop')];
case 5:
sendingProcess = _d.sent();
if (typeof sendingProcess === 'function' &&
sendingProcess() === 'break') {
return [3, 7];
}
_d.label = 6;
case 6:
csvDataWithoutSentTo_1_1 = csvDataWithoutSentTo_1.next();
return [3, 4];
case 7: return [3, 10];
case 8:
e_1_1 = _d.sent();
e_1 = { error: e_1_1 };
return [3, 10];
case 9:
try {
if (csvDataWithoutSentTo_1_1 && !csvDataWithoutSentTo_1_1.done && (_c = csvDataWithoutSentTo_1.return)) _c.call(csvDataWithoutSentTo_1);
}
finally { if (e_1) throw e_1.error; }
return [7];
case 10: return [3, 12];
case 11:
task_1 = cron.schedule(mailInterval, function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var row;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
row = csvDataWithoutSentTo[dataIndex];
return [4, sendMail(row, task_1)];
case 1:
_a.sent();
return [2];
}
});
}); });
task_1.start();
_d.label = 12;
case 12: return [3, 14];
case 13:
if (csvData.length > 0 && sentTo.length > 0) {
console.log("" + chalk.cyan("Email campaign is already complete. Please check your updated configuration file."));
}
else {
console.log("" + chalk.cyan("The provided CSV file is probably empty."));
}
logResult();
_d.label = 14;
case 14: return [2];
}
});
});
}
exports.default = massMail;
//# sourceMappingURL=massMail.js.map