initial commit
This commit is contained in:
commit
1cda61f376
4 changed files with 93 additions and 0 deletions
13
errorSender.js
Normal file
13
errorSender.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
module.exports = function(req, res, next) {
|
||||
res.sendError = function(code, text) {
|
||||
try {
|
||||
res.status(code).type('json').send(JSON.stringify({
|
||||
error: text
|
||||
}));
|
||||
} catch(err) {
|
||||
console.error("Error formatting error response: ", err);
|
||||
res.sendStatus(500);
|
||||
}
|
||||
}
|
||||
next();
|
||||
}
|
58
index.js
Normal file
58
index.js
Normal file
|
@ -0,0 +1,58 @@
|
|||
let fs = require('fs');
|
||||
|
||||
let bodyParser = require('body-parser');
|
||||
let outbound = require('./outbound');
|
||||
|
||||
let getTemplates = require('./templates');
|
||||
let errorSender = require('./errorSender');
|
||||
|
||||
exports.register = function() {
|
||||
this.register_hook('hook_init_http', function(next, server) {
|
||||
server.http.app.use('/webSend', setupRoutes(server.http.express.Router()));
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
function setupRoutes(app) {
|
||||
app.use(bodyParser.json());
|
||||
app.use(errorSender);
|
||||
|
||||
app.post('/template', function(req, res) {
|
||||
if(!req.body) {
|
||||
return res.sendError(400, "No body provided.");
|
||||
}
|
||||
|
||||
let {to, from, template, headers} = req.body;
|
||||
if(!template) {
|
||||
return res.sendError(400, "No template name provided.");
|
||||
}
|
||||
return getTemplates.then(function(templates) {
|
||||
let emailText = `From: ${from}\nTo: ${to}`;
|
||||
if(headers) {
|
||||
headers.forEach(function(header) {
|
||||
emailText += `\n${header}`;
|
||||
});
|
||||
}
|
||||
try {
|
||||
let [subject, body] = `\n\n${templates[template](req.body)}\n`;
|
||||
emailText += `\nSubject: ${subject}\n\n${body}`;
|
||||
} catch (err) {
|
||||
console.error("Error building email from template: ", err);
|
||||
res.sendError(err.httpStatus || 400, err.userMessage || "Error parsing template");
|
||||
}
|
||||
|
||||
outbound.send_email(from, to, emailText, function(code, message) {
|
||||
console.log(`Queue mail result code: ${code} msg: ${msg}`);
|
||||
res.type("json").send(JSON.stringigy({
|
||||
code,
|
||||
message
|
||||
}));
|
||||
});
|
||||
}).catch(function(err) {
|
||||
console.error("Error loading templates:", err);
|
||||
res.sendError(500, "Server Error");
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
11
package.json
Normal file
11
package.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "haraka-plugin-webmailer",
|
||||
"version": "1.0.0",
|
||||
"description": "Plugin to add our webhooks to haraka",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "john@rootprojects.com",
|
||||
"license": "UNLICENSED"
|
||||
}
|
11
templater.js
Normal file
11
templater.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
let escape = require("lodash/escape");
|
||||
|
||||
module.exports = function(strings) {
|
||||
//first element is the strings, don't need it
|
||||
let args = arguments.slice(1);
|
||||
|
||||
//first element will be the initial value
|
||||
return strings.slice(1).reduce(function(result, string, i) {
|
||||
return result + escape(args[i]) + string;
|
||||
}, strings[0]);
|
||||
};
|
Loading…
Reference in a new issue