-
-
Save ralyodio/becd16efaf2787593b9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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