Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add emailing lib

  • Loading branch information...
commit 379e965a6b902b184d7bda23e3918b426b64bcda 1 parent 458ba32
Jim Snodgrass authored
View
2  app.js
@@ -17,6 +17,7 @@ var flash = require('./lib/flash');
var mongoose = require('./lib/mongoose');
var redis = require('./lib/redis');
var reload = require('./lib/reload')();
+var emailer = require('./lib/emailer');
// Require our components
@@ -43,6 +44,7 @@ function createApp(config) {
middleware(app);
locals(app);
flash(app);
+ emailer(app);
reload();
// components
View
10 config.default.js
@@ -1,6 +1,8 @@
+var port = 3000;
+
module.exports = {
- port: 3000,
+ port: port,
request_timeout: 100000,
session_secret: "base12secret",
log_requests: false,
@@ -14,6 +16,8 @@ module.exports = {
auth: "",
debug: false
},
- mongoose_url: "mongodb://localhost/base12"
-
+ mongoose_url: "mongodb://localhost/base12",
+ send_mail: true,
+ contact_email: "jim@skookum.com",
+ host: "http://localhost:"+port
};
View
3  config.test.js
@@ -3,5 +3,6 @@ module.exports = {
stylus_debug: false,
test: true,
mongoose_url: "mongodb://localhost/base12_test",
- session_secret: "base12secret_test"
+ session_secret: "base12secret_test",
+ send_mail: false
};
View
63 lib/emailer/index.js
@@ -0,0 +1,63 @@
+var sendmail = require('email'),
+ fs = require('fs'),
+ cachedTemplates = {};
+
+
+_.templateSettings = {
+ interpolate : /\{\{(.+?)\}\}/g
+};
+
+
+
+module.exports = function(app){
+
+ var config = app.config;
+
+ var default_props = {
+ contact_email: config.contact_email,
+ host: config.host
+ };
+
+ app.emailer = {
+
+ send: function(message, props, callback) {
+ if (config.test || !config.send_mail) return false;
+
+ callback = callback || function() {};
+
+ _load_template(__dirname+'/templates/' + message + '.html', _.extend(props, default_props), function(err, options) {
+ // console.log("sending email: ", err, props);
+ if (!err) new sendmail.Email(options).send(callback);
+ else callback(err);
+ });
+ }
+ };
+
+};
+
+function _load_template(file, props, callback) {
+
+ // check cache for template
+ if (cachedTemplates[file]) {
+ buildEmail(cachedTemplates[file]);
+ }
+
+ // load template from file
+ else {
+ fs.readFile(file, function (err, data) {
+ if (!err && data) {
+ cachedTemplates[file] = data;
+ buildEmail(data);
+ }
+ else callback(err);
+ });
+ }
+
+ function buildEmail(template) {
+ var temp_file = _.template(template.toString(), props),
+ split_i = temp_file.indexOf('\n\n'),
+ options = JSON.parse('{' + temp_file.slice(0, split_i) + '}');
+ options.body = temp_file.slice(split_i + 1);
+ callback(null, options);
+ }
+}
View
1  package.json
@@ -18,6 +18,7 @@
"mongoose-utils": "0.0.1",
"redis": "0.8.2",
"bcrypt": "0.7.5",
+ "email": "0.2.4",
"nib": "0.9.1",
"async": "0.2.6",
"underscore": "1.4.4"
Please sign in to comment.
Something went wrong with that request. Please try again.