Skip to content

Instantly share code, notes, and snippets.

@cjroth
Created September 1, 2014 08:40
Show Gist options
  • Save cjroth/d331c2f4a4aa3a617834 to your computer and use it in GitHub Desktop.
Save cjroth/d331c2f4a4aa3a617834 to your computer and use it in GitHub Desktop.
node/express email service
/**
* inlines all css
* @todo should replace sendgrid with smtp to make more reusable
*/
var _ = require('underscore');
var config = require('../config');
var fs = require('fs');
var jade = require('jade');
var juice = require('juice');
var path = require('path');
var sendgrid = require('../services/sendgrid');
var sendEmail = function(data, done) {
if (!done) {
done = function() {};
}
data = _.extend({
from: config.email.from,
fromname: config.email.fromname,
}, data);
data.params = _.extend({
css: fs.readFileSync(path.join(__dirname, '../public/css/email.css')),
logo: config.domain + '/images/logo-email.png',
basedir: path.join(__dirname, '../views/emails'),
}, data.params);
var template = path.join(__dirname, '../views/emails/' + data.template + '.jade');
jade.renderFile(template, data.params, function(err, html) {
if (err) return done(err);
juice.juiceContent(html, { url: config.domain }, function(err, html) {
if (err) return done(err);
data.html = html;
sendgrid.send(data, done);
});
});
};
module.exports = sendEmail;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment