解析来自Sendgrid的入站电子邮件

时间:2016-08-18 23:05:39

标签: meteor sendgrid picker body-parser

我正在尝试使用Meteor,Picker和Body-Parser解析来自Sendgrid Inbound Webhook的传入电子邮件。我收到了电子邮件,但是当我记录请求正文时,我得到一个空对象。我在这里想念的是什么?

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

Picker.middleware( bodyParser.json() );

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

4 个答案:

答案 0 :(得分:4)

问题与内容类型是multipart / form-data有关。得到这样的工作:

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");
  });
});

答案 1 :(得分:0)

听起来,来自SendGrid的传入内容没有application/json内容类型,因此bodyParser.json()无法正确解析它。尝试添加bodyParser.urlencoded()调用,尝试解析application/x-www-form-urlencoded内容类型,看看是否有帮助。如下所示:

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));
}

答案 2 :(得分:0)

您也可以使用multer进行此操作。这是快递服务器版本:

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);
});

答案 3 :(得分:0)

我知道已经回答了这个问题,但是我有一个使用expressmulter的替代解决方案。我已经创建了一个仓库express-sendgrid-inbound-parse来帮助您入门。

我建议不要选中POST原始的完整MIME消息,因为您可以访问更多电子邮件数据。

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'])
}