Receiving Email with Node.js

This example may be outdated. You can now find examples for newer POST formats within the HTTP POST Formats documentation.

Express

Node.js will automatically parse JSON and provide you with an object to work with. If you need to access 'multipart/form-data' content (for the original format) in Node.js you can use the node-formidable module.

The following is a really simple express app that parses the multipart content using node-formidable and outputs the to, from and subject to the console.

var express = require('express');

var app = module.exports = express.createServer()
  , formidable = require('formidable')

app.post('/incoming_mail', function(req, res){
  var form = new formidable.IncomingForm()
  form.parse(req, function(err, fields, files) {
    console.log(fields.to)
    console.log(fields.from)
    console.log(fields.subject)
    res.writeHead(200, {'content-type': 'text/plain'})
    res.end('Message Received. Thanks!\r\n')
  })
})

app.listen(8080);

The following gist (by jafstar) may also be of help when dealing with errors from Node.js applications and presenting the correct parameters.