Friday, 23 August 2019

Handling emails with node.js and Mailparser

After sorting out mail forwarding and piping emails with postfix, I then needed to understand how to handle emails being POSTed to an API endpoint.

To parse an email with node.js, I recommend using Mailparser's simpleParser. I'm using express with bodyParser configured as follows:
 limit : config.bodyLimit
In your handler:
const express = require('express');
const router = express.Router();
const simpleParser = require('mailparser').simpleParser;
import { Router } from 'express';
import { simpleParser } from 'mailparser';
and then'/', (req, res) => {
        .then(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!

1 comment:

  1. Thank you for taking the time to publish this information very useful! web design agency


ISBN codes for Dummies

Step 1: Acquire ISBN codes. For South African residents this is a free service (thank you, NLSA!), and all you have to do is request them an...