Mailin

Artisanal inbound emails for every web app

Mailin is an smtp server that listens for emails, parses them and posts them as json to the url of your choice.
It checks the incoming emails dkim, spf, spam score (using spamassassin) and tells you in which language the email is written.

Mailin can be used as a standalone application directly from the command line, or embedded inside a node application.

Mailin relies heavily on the excellent work of @andris9 for the smtp and mail parsing services.

In order to receive emails, your smtp server address should be made available somewhere. Two records should be added to your DNS records. Let us pretend that we want to receive emails at *@subdomain.domain.com:

First an MX record: subdomain.domain.com MX 10 mxsubdomain.domain.com. This means that the mail server for addresses like *@subdomain.domain.com will be mxsubdomain.domain.com.

Then an A record: mxsubdomain.domain.com A the.ip.address.of.your.mailin.server. This tells at which ip address the mail server can be found.

You can fire up Mailin (see next section) and use an smtp server tester to verify that everything is correct.

Run it, specifying your webhook url (addtionnal help can be found using mailin --help). By default, Mailin will listen on port 25, the standard smtp port. you can change this port for testing purpose using the --port option. However, do not change this port if you want to receive emails from the real world.

Ports number under 1000 are reserved to root user. So two options here. Either run Mailin as root:

sudo mailin --webhook http://mydomain.com/incoming_emails

Or, prefered choice, use something like authbind to run Mailin with a standard user while still using port 25.
Here comes a tutorial on how to setup authbind. In this case, do something like:

authbind --deep mailin --webhook http://mydomain.com/incoming_emails

and make sure that your user can write to the log file.

At this point, Mailin will listen for incoming emails, parse them and post an urlencoded form multipart/form-data to your webhook url.

The webhook payload is a multipart form with a mailinMsg fields always present and some optional additional fields containing the content of the attachments. How to handle this? We have got you covered, there is a working example using node and express in mailin/samples/server.js. Anyway, once parsed, you should end up with something like:

error: listen EACCES: your user do not have sufficients privileges to run on the given port. Ports under 1000 are restricted to root user. Try with sudo.

error: listen EADDRINUSE: the current port is already used by something. Most likely, you are trying to use port 25 and your machine's mail transport agent is already running. Stop it with something like sudo service exim4 stop or sudo service postfix stop before using Mailin.

error: Unable to compute spam score ECONNREFUSED: it is likely that spamassassin is not enabled on your machine, check the /etc/default/spamassassin file.

node: command not found: most likely, your system does not have node installed or it is installed with a different name. For instance on Debian/Ubuntu, the node interpreter is called nodejs. The quick fix is making a symlink: ln -s $(which nodejs) /usr/bin/node to make the node command available.

Uncaught SenderError: Mail from command failed - 450 4.1.8 <an@email.address>: Sender address rejected: Domain not found: The smtpOption disableDNSValidation is set to false and an email was sent from an invalid domain.

authorizeUser(connection, username, password, callback) - Emitted if requireAuthentication option is set to true. callback has two parameters (err, success) where success is a Boolean and should be true, if user is authenticated successfully.