Permalink
Browse files

Added template email

  • Loading branch information...
davidmerfield committed Jan 10, 2019
1 parent 568771f commit 1b65e7218ea207fde10429c44e9a52c4b63e8b4f
Showing with 66 additions and 0 deletions.
  1. +3 −0 scripts/email/_template/email.txt
  2. +19 −0 scripts/email/_template/index.js
  3. +44 −0 scripts/email/send.js
@@ -0,0 +1,3 @@
Subject line here.

Email body here.
@@ -0,0 +1,19 @@
var send = require("../send");
var each = require("../../each/blog");

var emailFile = __dirname + "/email.txt";
var users = [];

each(
function(user, blog, next) {

users.push(user);
next();
},
function() {
send(emailFile, users, function(err) {
if (err) throw err;
process.exit();
});
}
);
@@ -0,0 +1,44 @@
var helper = require("../../app/helper");
var send = helper.email.send;
var yesno = require("yesno");
var async = require("async");
var fs = require("fs-extra");
var colors = require('colors/safe');

module.exports = function(emailFile, users, callback) {
if (!users.length)
return callback(new Error("No users in list of users to send email to."));

var unique = {};

users = users.filter(function(user){
if (unique[user.email]) return false;
unique[user.email] = true;
return true;
});

console.log(colors.dim("-------------------\n") + fs.readFileSync(emailFile, "utf-8") + colors.dim('\n-------------------\n'));

yesno.ask("Send email to " + colors.green(users.length + " users") + "? (y/n)", false, function(ok) {
if (!ok) {
console.log("Emails were not sent.");
return process.exit();
}

async.eachSeries(
users,
function(user, next) {
send(user, emailFile, user.email, function(err) {
if (err) console.log("Error sending message to", user.email);
next();
});
},
function(err) {
if (err) return callback(err);

console.log("Emails sent");
callback();
}
);
});
};

0 comments on commit 1b65e72

Please sign in to comment.