Last active
August 29, 2015 14:06
-
-
Save baudehlo/9682f762d400fb094cb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//var databaseUrl = "localhost"; | |
//var collections = ["email"]; | |
//var db = require("mongojs").connect(databaseUrl, collections); | |
var MongoClient = require('mongodb').MongoClient; | |
exports.hook_data = function (next, connection) { | |
// enable mail body parsing | |
connection.transaction.parse_body = 1; | |
next(); | |
} | |
function create_body_struct (body) { | |
var struct = {}; | |
struct.bodytext = body.bodytext; | |
struct.content_type = body.header.get_decoded('content-type'); | |
if (body.children.length > 0) { | |
struct.children = []; | |
for (var i=0; i<body.children.length; i++) { | |
struct.children.push(create_body_struct(body.children[i])) | |
} | |
} | |
return struct; | |
} | |
exports.hook_queue = function (next, connection) { | |
// basic logging so we can see if we have an email hitting the stack | |
this.loginfo("New inbound email detected, inserting into mongodb"); | |
var that = this; | |
MongoClient.connect('mongodb://127.0.0.1:27017/emails', function(err, db) { | |
if(err) throw err; | |
var transaction = connection.transaction; | |
var receivedDate = transaction.header.get_decoded('date'); | |
var subjectLine = transaction.header.get_decoded('subject'); | |
var email = transaction.mail_from; | |
var message = create_body_struct(transaction.body); | |
var collection = db.collection('test_insert'); | |
collection.insert({ | |
email : email, | |
receivedDate : receivedDate, | |
subjectLine: subjectLine, | |
message : message | |
}, function(err, docs) { | |
if(err) throw err; | |
collection.count(function(err, count) { | |
that.logdebug("count = " + count); | |
db.close(); | |
next(OK); | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment