Skip to content

Instantly share code, notes, and snippets.

@ralyodio
Created October 30, 2012 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralyodio/becd16efaf2787593b9d to your computer and use it in GitHub Desktop.
Save ralyodio/becd16efaf2787593b9d to your computer and use it in GitHub Desktop.
exports.test = function(req, res){
var users = [
{
email: 'anthony@chovy.com',
name: {
first: 'Pappa',
last: 'Pizza'
}
},
{
email: 'ettinger@ettinger.name',
name: {
first: 'Mister',
last: 'Geppetto'
}
}
];
//test welcome email
emails.email_batch(req, res, users, function(err, res){
req.flash('info', 'Welcome to '+cfg.appname+', a welcome email has been sent to you.');
res.redirect('/');
});
});
};
/*
* Send batch email.
*/
exports.email_batch = function(res, req, users){
emailTemplates(templatesDir, function(err, template) {
if (err) {
console.log(err);
throw err;
} else {
// ## Send a batch of emails and only load the template once
// Prepare nodemailer transport object
var transportBatch = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
user: cfg.email.smtp_username,
pass: cfg.email.smtp_password
}
});
// Custom function for sending emails outside the loop
//
// NOTE:
// We need to patch postmark.js module to support the API call
// that will let us send a batch of up to 500 messages at once.
// (e.g. <https://github.com/diy/trebuchet/blob/master/lib/index.js#L160>)
var Render = function(locals) {
this.locals = locals;
this.send = function(err, html, text) {
if (err) {
console.log(err);
} else {
transportBatch.sendMail({
from: 'Spicy Meatball <spicy.meatball@spaghetti.com>',
to: locals.email,
subject: 'Mangia gli spaghetti con polpette!',
html: html,
// generateTextFromHTML: true,
text: text
}, function(err, responseStatus) {
if (err) {
console.log(err);
} else {
console.log(responseStatus.message);
}
});
}
};
this.batch = function(batch) {
batch(this.locals, this.send);
};
};
// Load the template and send the emails
template('test', true, function(err, batch) {
for(var user in users) {
var render = new Render(users[user]);
render.batch(batch);
}
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment