From fb678f0afeda986442d92805d17c17c3ca0aeb0e Mon Sep 17 00:00:00 2001 From: Richard Remer Date: Tue, 15 Sep 2015 23:53:54 -0700 Subject: [PATCH] parse incoming messages and pass them along to the endpoint --- bin/smtp2http.js | 16 +++++++++++++++- package.json | 4 ++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/bin/smtp2http.js b/bin/smtp2http.js index d8fce33..3fb21ea 100644 --- a/bin/smtp2http.js +++ b/bin/smtp2http.js @@ -2,6 +2,7 @@ var squabble = require("squabble").createParser(), smtp = require("smtp-protocol"), http = require("request"), readFile = require("fs").readFileSync, + MailParser = require("mailparser").MailParser, args, tlsTokens, serverOpts = {}; @@ -13,6 +14,7 @@ squabble.shortOpts().longOpts().stopper() // parse arguments args = squabble.parse(); +serverOpts.endpoint = args.named.ENDPOINT; if (args.named["--tls"]) { tlsTokens = args.named["--tls"].split(":"); switch (tlsTokens.length) { @@ -37,12 +39,24 @@ if (args.named["--tls"]) { smtp.createServer(serverOpts, function(req) { // accept all incoming messages req.on("to", function(to, ack) { + console.log(to); ack.accept(); }); // send message to web endpoint req.on("message", function(stream, ack) { - stream.pipe(process.stdout); + stream.pipe(new MailParser().on("end", function(email) { + http.post({ + url: serverOpts.endpoint, + json: email + }, function(err, res, body) { + if (err) console.error(err); + }) + }).on("error", function(err) { + console.error(err); + })); + ack.accept(); }); }).listen(process.env.NODE_PORT || 25); + diff --git a/package.json b/package.json index 076c6bf..fe1c21d 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "smtp2http", "version": "0.0.1", "description": "SMTP to HTTP gateway", - "main": "index.js", "bin": { "smtp2http": "./bin/smtp2http.js" }, "dependencies": { "smtp-protocol": "^2", "squabble": "^1", - "request": "^2" + "request": "^2", + "mailparser": "^0.5" }, "repository": { "type": "git",