Skip to content

Instantly share code, notes, and snippets.

@arlolra
Created May 19, 2011 22:26
Show Gist options
  • Save arlolra/981943 to your computer and use it in GitHub Desktop.
Save arlolra/981943 to your computer and use it in GitHub Desktop.
// A queue to deliver to couch
var cradle = require('cradle'),
logger = require('nebulog'),
fs = require('fs'),
config = JSON.parse(fs.readFileSync('config.json', 'utf8')),
db = new (cradle.Connection)(config.couch).database(config.couch.database);
exports.hook_queue = function(next, connection) {
var ct = connection.transaction;
if (ct.data_lines.length === 0) { return next(DENY); }
var rcpt_to = [];
ct.rcpt_to.forEach(function (rcpt) {
rcpt_to.push(rcpt.address());
});
db.save({
docType: 'email',
dateReceived: (new Date()).toISOString().split('T')[0],
body: ct.data_lines.join(''),
to: rcpt_to,
from: connection.transaction.mail_from.address()
}, function (err, res) {
if (err) {
logger.warn("Saving the email to db failed");
return next(DENY, "Saving to db failed");
}
return next(OK);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment