Skip to content

Instantly share code, notes, and snippets.

@baudehlo
Created February 27, 2012 16:02
Show Gist options
  • Save baudehlo/1924944 to your computer and use it in GitHub Desktop.
Save baudehlo/1924944 to your computer and use it in GitHub Desktop.
Subject: Thanks for signing up!
Date: {{date}}
To: {{recipient}}
From: {{sender}}
MIME-Version: 1.0
Content-Type: text/plain
Thanks for signing up for the Foobar service!
Your email address is: {{recipient}}
var outbound = require('./outbound');
var utils = require('./utils');
var express = require('express');
var expapp = express.createServer();
exports.hook_init_master = function (next) {
expapp.use(express.bodyParser());
expapp.get('/', function (req, res) {
res.send("Please use a valid URL");
});
var plugin = this;
expapp.post('/SendMail/:template', function (req, res) {
plugin.send_email(req, res);
});
expapp.listen(this.config.get('hdout.port') || 5000);
next();
}
exports.send_email = function (req, res) {
var self = this;
this.loginfo("Sending email");
if (!req.body.recipient) {
res.send("No recipient", 400)
}
if (!req.body.sender) {
res.send("No sender", 400)
}
// TODO: check format of recip/sender
var template = this.config.get('mail_template/' + req.params.template, 'data');
if (!template.length) {
return res.send("Can't find that template", 404);
}
req.body.date = utils.ISODate(new Date());
// we may want to replace this with handlebars.js in the future
var output = template.join('\n').replace(/\{\{(\w+)\}\}/g, function (str, key) {
// todo: will this break if the value is zero?
return req.body[key] || '';
});
this.loginfo("Output: " + output);
var outnext = function (code, msg) {
switch (code) {
case DENY: self.logerror("Sending mail failed: " + msg);
res.send("Error sending email: " + msg, 500);
break;
case OK: self.loginfo("mail sent");
res.send({"result": "Success"});
break;
default: self.logerror("Unrecognised return code from sending email: " + msg);
res.send("Internal confusion error", 500);
}
};
outbound.send_email(req.body.sender, req.body.recipient, output, outnext);
}
@turbobuilt
Copy link

This is awesome! Thanks for posting, saved me a lot of time.

@netcookies
Copy link

Awesome! Thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment