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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@civitas-cerebrum/email-client

v0.0.4

Published

A generic SMTP/IMAP email client for test automation. Send, receive, search, and clean emails with composable filters.

Downloads

356

Readme

@civitas-cerebrum/email-client

NPM Version

A highly robust, zero-dependency SMTP/IMAP email client built specifically for E2E test automation. Send, receive, search, manage, and clean emails using composable, deterministic filters.

Why this client?

  • Zero Playwright runtime dependency — Works seamlessly with Playwright, Cypress, Vitest, Jest, or any Node.js test runner.
  • Smart Polling Engine — Built-in retry logic prevents flaky tests caused by slow email delivery.
  • Memory Protected — Automatically caps raw MIME fetches to prevent Node.js memory crashes when hitting large, unmanaged test inboxes.
  • Two-Phase Matching — Evaluates exact IMAP criteria first, then falls back to a smart, case-insensitive client-side match to catch poorly formatted automated emails.
  • Full Mailbox Management — Send, receive, flag, archive, and aggressively clean your test environment.

Installation

npm install @civitas-cerebrum/email-client

Quick Start

import { EmailClient, EmailFilterType, EmailMarkAction } from '@civitas-cerebrum/email-client';

const client = new EmailClient({
    senderEmail: '[email protected]',
    senderPassword: 'app-password',
    senderSmtpHost: 'smtp.example.com',
    receiverEmail: '[email protected]',
    receiverPassword: 'app-password',
});

// 1. Send an email
await client.send({
    to: '[email protected]',
    subject: 'Your OTP Code',
    text: 'Your code is 123456',
});

// 2. Poll the inbox until the email arrives
const email = await client.receive({
    filters: [{ type: EmailFilterType.SUBJECT, value: 'Your OTP Code' }],
    waitTimeout: 30000 // Waits up to 30 seconds
});
console.log(email.text); // "Your code is 123456"

// 3. Mark as Read, or clean up the test
await client.mark({
    action: EmailMarkAction.READ,
    filters: [{ type: EmailFilterType.SUBJECT, value: 'Your OTP Code' }]
});

await client.clean({
    filters: [{ type: EmailFilterType.SUBJECT, value: 'Your OTP Code' }]
});

API Reference

Initialization

const client = new EmailClient(credentials: EmailCredentials);

| Field | Type | Default | Description | |---|---|---|---| | senderEmail | string | — | SMTP sender email address | | senderPassword | string | — | SMTP sender password or app password | | senderSmtpHost | string | — | SMTP host (e.g., 'smtp.gmail.com') | | senderSmtpPort | number | 587 | SMTP port | | receiverEmail | string | — | IMAP receiver email address | | receiverPassword | string | — | IMAP receiver password or app password | | receiverImapHost | string | 'imap.gmail.com' | IMAP host | | receiverImapPort | number | 993 | IMAP port |


Sending Emails (send)

Supports plain text, inline HTML, and loading HTML files directly from disk.

// Plain text
await client.send({ to: '[email protected]', subject: 'Test', text: 'Hello' });

// Inline HTML
await client.send({ to: '[email protected]', subject: 'Report', html: '<h1>Results</h1>' });

// HTML template from disk
await client.send({ to: '[email protected]', subject: 'Report', htmlFile: 'emails/template.html' });

Receiving Emails (receive / receiveAll)

The client uses a robust polling mechanism. It will continuously query the IMAP server until the waitTimeout is reached or the filters are satisfied. Combine multiple filters to create strict AND logic constraints.

// Get the single most recent matching email
const email = await client.receive({
    filters: [{ type: EmailFilterType.SUBJECT, value: 'Your OTP' }],
    waitTimeout: 15000 // Optional: fail if not found in 15s
});

// Get ALL matching emails in the inbox (useful for batch processing)
const allEmails = await client.receiveAll({
    filters: [
        { type: EmailFilterType.FROM, value: '[email protected]' },
        { type: EmailFilterType.SINCE, value: new Date('2025-01-01') },
    ],
});

Receive Options

| Option | Type | Default | Description | |---|---|---|---| | filters | EmailFilter[] | — | Required. Array of filters (AND logic) | | folder | string | 'INBOX' | IMAP folder to search | | waitTimeout | number | 30000 | Max milliseconds to poll before throwing an error | | pollInterval | number | 3000 | Milliseconds to wait between IMAP fetch attempts | | downloadDir | string | os.tmpdir() | Directory to save downloaded .html copies |

Available Filters (EmailFilterType)

| Type | Value Type | Description | |---|---|---| | SUBJECT | string | Exact or partial match of the email subject | | FROM | string | Sender email address | | TO | string | Recipient email address | | CONTENT | string | Matches anywhere in the HTML body or plain text fallback | | SINCE | Date | Only fetch emails received after this timestamp |


Managing the Inbox (mark / clean)

Keep your automated test inboxes clean and organized to prevent IMAP throttling and memory issues.

Mark (Flagging and Moving)

Modify the state of emails matching specific criteria. Returns the number of emails affected.

// Apply standard flags
await client.mark({
    action: EmailMarkAction.READ, // READ, UNREAD, FLAGGED, UNFLAGGED
    filters: [{ type: EmailFilterType.SUBJECT, value: 'Welcome' }]
});

// Move emails to an archive folder
await client.mark({
    action: EmailMarkAction.ARCHIVED,
    filters: [{ type: EmailFilterType.FROM, value: '[email protected]' }],
    archiveFolder: 'Archive' // Note: This must match the server's localized folder name
});

// Apply custom IMAP flags
await client.mark({
    action: ['\\Draft', '\\Answered'],
    filters: [{ type: EmailFilterType.SUBJECT, value: 'Custom State' }]
});

Clean (Deleting)

Permanently delete emails from the server.

// Delete specific emails
await client.clean({
    filters: [{ type: EmailFilterType.FROM, value: '[email protected]' }],
});

// Nuke the entire inbox (Use with caution!)
await client.clean();

The ReceivedEmail Object

When you receive an email, the client parses the raw MIME source and returns a clean, strongly-typed object:

| Property | Type | Description | |---|---|---| | subject | string | Email subject line | | from | string | Sender address | | to | string | Recipient address | | date | Date | Date the email was sent | | html | string | Parsed HTML body (empty string if plain-text only) | | text | string | Parsed plain-text content | | filePath | string | Local path to the downloaded .html copy |


Contributing

git clone https://github.com/Umutayb/email-client.git
cd email-client
npm install
npm run test

License

MIT