Fake SMTP Server

0.8.0 · active · verified Tue Apr 21

Fake SMTP Server is a utility designed for email testing within QA and development workflows. It provides a local, in-memory SMTP server that captures all incoming emails, preventing them from being sent to real recipients. The captured emails can then be inspected manually via a web interface or programmatically through a RESTful API. The package is currently at version 0.8.0. While a strict release cadence isn't published, it appears actively maintained through its GitHub repository. Key differentiators include its ease of installation and use as a standalone server, making it ideal for local development and Continuous Integration (CI) environments where actual email sending is undesirable or complex to mock.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically start the `fake-smtp-server`, send an email to it using Nodemailer, and then retrieve and verify the captured email via its HTTP API. It also shows how to clear the mailbox.

import { createServer } from 'fake-smtp-server';
import { createTransport } from 'nodemailer';
import fetch from 'node-fetch'; // For HTTP API interaction

async function runEmailTest() {
  const smtpPort = 2525;
  const httpPort = 1081;

  console.log(`Starting Fake SMTP Server on SMTP port ${smtpPort}, HTTP port ${httpPort}...`);
  const server = createServer({
    smtpPort,
    httpPort,
    headers: true // Enable headers for richer email data
  });

  await server.start();
  console.log('Fake SMTP Server started.');

  // Configure Nodemailer to send to our fake SMTP server
  const transporter = createTransport({
    host: 'localhost',
    port: smtpPort,
    secure: false, // Use plain SMTP for testing
    tls: {
      rejectUnauthorized: false
    }
  });

  const testEmail = {
    from: 'sender@example.com',
    to: 'recipient@example.com',
    subject: 'Test Email from Fake SMTP Server',
    text: 'This is a test email sent to the fake SMTP server.',
    html: '<b>This is a test email</b> sent to the fake SMTP server.'
  };

  console.log('Sending test email...');
  await transporter.sendMail(testEmail);
  console.log('Test email sent.');

  console.log('Fetching received emails from API...');
  const response = await fetch(`http://localhost:${httpPort}/api/emails?to=${testEmail.to}`);
  const emails = await response.json();

  if (emails.length > 0) {
    console.log(`Received ${emails.length} email(s):`);
    console.log(JSON.stringify(emails[0], null, 2));
    if (emails[0].subject !== testEmail.subject) {
      console.error('ERROR: Email subject mismatch!');
    }
  } else {
    console.error('ERROR: No emails received!');
  }

  console.log('Clearing all emails via DELETE API...');
  await fetch(`http://localhost:${httpPort}/api/emails`, { method: 'DELETE' });
  const afterDelete = await (await fetch(`http://localhost:${httpPort}/api/emails`)).json();
  console.log(`Emails after clear: ${afterDelete.length}`);

  console.log('Stopping Fake SMTP Server...');
  await server.stop();
  console.log('Fake SMTP Server stopped.');
}

runEmailTest().catch(console.error);

view raw JSON →