Quick and dirty smtp server, that accepts handlers to process messages


smtp-tester is a simple smtp server that accepts connections, receives mail, and then calls callbacks that are bound to a particular address.

Installation is fairly straightforward, just install the npm module:

npm install smtp-tester

First, require smtp-tester:

var ms = require('smtp-tester');

Next, initialize a server with a port it should listen on (yes, I know, never end a sentence in a preposition).

var mailServer = ms.init(port);

Done. Your SMTP server is now listening on port 'port'.

Send mail using any SMTP client you want. For node work, I personally use nodemailer

npm install nodemailer

To receive mail, bind a handler to the mailServer you created earlier.

var ms, mailServer, handler, port = 4000;
ms = require('smtp-tester');
mailServer = ms.init(port);
handler = function(addr,id,email) {
    // do something interesting

Done. Every mail sent to (and every one sent before binding) will call the handler exactly once.

You can have as many handlers as you want, they are all executed, even for the same address. However, execution order, while usually in the order in which they were added, is not guaranteed.

Catch-All Handlers

If you want a handler to catch every email that is sent through the system, just bind with no address at all.

handler = function(addr,id,email) {
    // do something interesting
    // because this is a catch-all, the addr will be null

Catch-All handlers are always run before specific handlers.

To stop receiving mail at a particular handler, just unbind.


Catch-All Handlers

If you want to remove a catch-all handler that catches every email that is sent through the system, just unbind with no address at all.

handler = function(addr,id,email) {
    // do something interesting
    // because this is a catch-all, the addr will be null
mailServer.bind(handler); // this adds it
mailServer.unbind(handler); // this removes it

To remove messages from the mail server, you can remove an individual message or all of them:


Surprisingly, the method is just called "stop".


Handlers that receive mail are passed three parameters.

  • addr: Address to which the email was addressed, and for which the handler was bound. If this is a catch-all handler, then this is null.
  • id: Internal ID of the email in this mail server process. Useful for removing messages or checking against something in our cache.
  • email: JavaScript object of the email, containing "sender", "receivers", "data" (raw text), "headers" and "body".

Sample email object is as follows, taken from the test.js included with the package.

{ sender: { address: '<>', valid: true },
  receivers: { '': true },
  data: 'X-Mailer: Nodemailer (0.2.3; +\r\nDate: Thu, 01 Dec 2011 10:24:01 GMT\r\nFrom:\r\nTo:\r\nSubject: email test\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\nThis is a test mail\r\n',
   { 'X-Mailer': 'Nodemailer (0.2.3; +',
     Date: 'Thu, 01 Dec 2011 10:24:01 GMT',
     From: '',
     To: '',
     Subject: 'email test',
     'MIME-Version': '1.0',
     'Content-Type': 'text/plain; charset=UTF-8',
     'Content-Transfer-Encoding': 'quoted-printable' },
  body: 'This is a test mail\r\n' }

smtp-tester supports pre-shipped modules. They are named and can be run by calling

var success;
// to load a module
success = mailServer.module(name);
// to unload a module

If the module successfully loads, it will return success, else it will return false.

The following modules are currently available.

  • logAll: logs every message received to the console in a text format close to raw text.

More are expected to follow.