Skip to content

Instantly share code, notes, and snippets.

@baudehlo
Created March 28, 2011 15:50
Show Gist options
  • Save baudehlo/890703 to your computer and use it in GitHub Desktop.
Save baudehlo/890703 to your computer and use it in GitHub Desktop.
// Store mail in Mongo
var Db = require('mongodb').Db,
Connection = require('mongodb').Connection,
Server = require('mongodb').Server,
BSON = require('mongodb').BSONNative;
var MailParser = require("mailparser").MailParser;
exports.hook_queue = function (callback, connection) {
// Parse the mail first...
var mp = new MailParser({fix_smtp_escapes: 0});
var headers;
var body;
mp.on('header', function (h) {
headers = h;
});
mp.on('body', function (b) {
body = b;
});
connection.transaction.data_lines.forEach(mp.feed);
mp.end();
var host = this.config.get('mongodb.host') || 'localhost';
var port = this.config.get('mongodb.port') || Connection.DEFAULT_PORT;
var dbname = this.config.get('mongodb.name') || 'default-db';
var db = new Db(dbname, new Server(host, port, {}), {native_parser:true});
db.open(function(err, db) {
if (err) {
return callback(CONT);
}
db.collection('test', function(err, collection) {
if (err) {
return callback(CONT);
}
// do collection.insert here with whatever is in body.bodyText or body.bodyHTML
// don't forget to run the callback if it all worked
return callback(OK);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment