oskare
oskare

Reputation: 1061

Parsing inbound emails from Sendgrid

I'm trying to parse incoming emails from the Sendgrid Inbound Webhook with Meteor, Picker and Body-Parser. I get the emails but when I log the request body I get an empty object. What am I missing here??

var bodyParser = require('body-parser');;

Picker.middleware( bodyParser.json() );

Picker.route('/incoming/', function(params, req, res, next) {
    console.log("Body: " + JSON.stringify(req.body));
}

Upvotes: 2

Views: 1786

Answers (4)

shammelburg
shammelburg

Reputation: 7308

I know this has already been answered but I've got an alternative solution using express and multer. I've created a repo express-sendgrid-inbound-parse to get you started.

I recommend leaving POST the raw, full MIME message unchecked as you can access to more email data.

console.log('dkim: ', body.dkim)
console.log('to: ', body.to)
console.log('cc: ', body.cc)
console.log('from: ', body.from)
console.log('subject: ', body.subject)
console.log('sender_ip: ', body.sender_ip)
console.log('spam_report: ', body.spam_report)
console.log('envelope: ', body.envelope)
console.log('charsets: ', body.charsets)
console.log('SPF: ', body.SPF)
console.log('spam_score: ', body.spam_score)

if (rawFullMimeMessageChecked) {
    console.log('email: ', body.email)
} else {
    console.log('headers: ', body.headers)
    console.log('html: ', body.html)
    console.log('text: ', body.text)
    console.log('attachments: ', body.attachments)
    console.log('attachment-info: ', body['attachment-info'])
    console.log('content-ids: ', body['content-ids'])
}

Upvotes: 1

user3552762
user3552762

Reputation: 1

You can also do this with multer. Here is the express server version:

const express = require(“express”);
const app = express();
var multer = require(“multer”);
var upload = multer();
app.post(“/”, upload.none(), function (req, res) {
  console.log(req.body);
});

Upvotes: 0

oskare
oskare

Reputation: 1061

The problem was related to the content-type being multipart/form-data. Got it working like this:

var multiparty = require('multiparty');
var bodyParser = Npm.require('body-parser');

Picker.middleware(bodyParser.urlencoded({ extended: true }));
Picker.middleware(bodyParser.json());

Picker.route('/incoming/', function(params, req, res, next) {
  var form = new multiparty.Form();
  form.parse(req, function(err, fields, files) {
    console.log("Heureka: " + JSON.stringify(fields) + JSON.stringify(files));
    res.writeHead(200, {'content-type': 'text/plain'});
    res.write('received upload:\n\n');
    res.end("thanks");
  });
});

Upvotes: 4

hwillson
hwillson

Reputation: 1399

It sounds like the incoming content from SendGrid doesn't have a application/json Content-Type, so bodyParser.json() can't parse it properly. Try adding a bodyParser.urlencoded() call as well, to try to parse a application/x-www-form-urlencoded Content-Type, to see if that helps. So something like:

var bodyParser = require('body-parser');

Picker.middleware(bodyParser.json());
Picker.middleware(bodyParser.urlencoded({ extended: false }));

Picker.route('/incoming/', function(params, req, res, next) {
  console.log("Body: " + JSON.stringify(req.body));
}

Upvotes: 0

Related Questions