npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smtp-tester

v2.1.0

Published

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

Downloads

56,378

Readme

smtp-tester

Overview

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

Installation

Installation is fairly straightforward, just install the npm module:

npm install smtp-tester

Starting an SMTP server

First, require smtp-tester:

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

Next, initialize a server with a port on which it should listen.

var mailServer = ms.init(port);

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

Sending Mail

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

npm install nodemailer

Receiving Mail

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
};

mailServer.bind("[email protected]",handler);

Done. Every mail sent to [email protected] (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.

If you intend to capture a single message using promises, you can do:

mailServer.captureOne('[email protected]')
  .then(function({ address, id, email }) {
    // Do something interesting
  });

Most likely you'll want to use the wait option as well, so if no message is received in the given time frame, captureOne() rejects the promise:

mailServer.captureOne('[email protected]', { wait: 1000 })
  .then(function({ address, id, email }) {
    // Do something interesting
  })
  .catch(function(error) {
    // No message delivered to [email protected] in 1 second.
  });

Now using async/await:

try {
  const { email } = await mailServer.captureOne('[email protected]', { wait: 1000 });
} catch (error) {
  // No message delivered to [email protected] in 1 second.
}

This is useful for testing that no message was delivered, too.

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
};
mailServer.bind(handler);

Catch-All handlers are always run before specific handlers.

Stopping Receipt

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

mailServer.unbind("[email protected]",handler);

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

Removing Messages

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

mailServer.remove(id);
mailServer.removeAll();

Stopping the Server

Surprisingly, the method is just called "stop".

mailServer.stop();

Handlers

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", "body" (plain text) and "html".

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

{
  sender:    '[email protected]',
  receivers: {
    '[email protected]': true
  },
  data:      'X-Mailer: Nodemailer (0.2.3; +http://www.nodemailer.org)\r\nDate: Thu, 01 Dec 2011 10:24:01 GMT\r\nFrom: [email protected]\r\nTo: [email protected]\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',
  body:      'This is a test mail',
  headers: {
    'x-mailer':                  'Nodemailer (0.2.3; +http://www.nodemailer.org)',
    date:                        'Thu, 01 Dec 2011 10:24:01 GMT',
    from:                        '[email protected]',
    to:                          '[email protected]',
    subject:                     'email test',
    'mime-version':              '1.0',
    'content-type':              { value: 'text/plain' },
    'content-transfer-encoding': 'quoted-printable'
  }
}

Modules

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
mailServer.unmodule(name);

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.

Testing

Just run

npm test

Note that each build triggers a Travis CI build