To parse an email with node.js, I recommend using Mailparser's simpleParser. I'm using express with bodyParser configured as follows:
app.use(bodyParser.json({ limit : config.bodyLimit }));In your handler:
const express = require('express'); const router = express.Router(); const simpleParser = require('mailparser').simpleParser;or
import { Router } from 'express'; import { simpleParser } from 'mailparser';and then
api.post('/', (req, res) => { simpleParser(req) .then(parsed => { res.json(parsed); }) .catch(err => { res.json(500, err); }); });Mailparser is excellent, and documented, but the documentation assumes that we're familiar with the email format. Fortunately, oblac's example email exists for those of us who aren't!
To test, send the example email to the endpoint via curl:
curl --data-binary "@./example.eml" http://your-domain-name/api/email
or Postman (attach file to "binary").
And we're good to go!
No comments:
Post a Comment