Skip to content

Instantly share code, notes, and snippets.

@srfrnk
Created August 16, 2014 12:23
Show Gist options
  • Save srfrnk/fe03abd1eb62dfeea6ed to your computer and use it in GitHub Desktop.
Save srfrnk/fe03abd1eb62dfeea6ed to your computer and use it in GitHub Desktop.
require("requirejs").define("models/emailHelper", [], function () {
var Q = require("q");
var emailjs = require("emailjs");
var ejs = require('ejs');
var fs = require('fs');
var path = require('path');
return {
send: function (templatePath,data,to,subject) {
var q= Q.defer();
var server = emailjs.server.connect(JSON.parse(process.env.EMAILJS_CONNECTION_OPTIONS));
var str = fs.readFileSync(path.join(process.env.rootPath, templatePath), 'utf8');
var html = ejs.render(str, data);
server.send({
text: "",
from: "[email protected] <[email protected]>",
to: to,
subject: subject,
attachment: [
{data: html, alternative: true}
]
}, function (err, message) {
if (!err) {
q.resolve(message);
}
else {
console.log(err);
q.reject(err);
}
});
return q.promise;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment