EmailJS IMAP Client

3.1.0 · maintenance · verified Wed Apr 22

emailjs-imap-client is a low-level JavaScript IMAP client designed to interact with IMAP servers for email management. It provides a programmatic interface for tasks like connecting, authenticating, listing mailboxes, fetching messages, and other standard IMAP operations. The current stable version is 3.1.0, released approximately six years ago. The library is currently in a maintenance state, as the original author is no longer actively maintaining it, and there's an open call for community contributions. This means new feature development and proactive bug fixes are sporadic or non-existent without community involvement. Its primary differentiator is being one of the few dedicated IMAP client implementations available for JavaScript environments, making it a critical component for applications requiring direct IMAP protocol interaction. It handles both plain and TLS-secured connections, with configurable STARTTLS behavior.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the ImapClient, connect to a server with authentication, list mailboxes, select a mailbox, list messages, and handle basic errors, emphasizing security for passwords and instance reusability.

import ImapClient from 'emailjs-imap-client';

const host = 'localhost'; // Replace with your IMAP server host, e.g., 'imap.mail.com'
const port = 143; // Standard IMAP port (993 for IMAPS)
const username = 'testuser'; // Replace with your IMAP username
const password = process.env.IMAP_PASS ?? 'testpass'; // Use environment variable for real passwords in production

async function runImapClient() {
  // Create a new client instance for each connection session due to reusability limitations.
  const client = new ImapClient(host, port, {
    auth: {
      user: username,
      pass: password
    },
    // Recommended for secure connections: useSecureTransport for IMAPS, requireTLS for STARTTLS.
    // useSecureTransport: true, // For IMAPS (SSL/TLS on connection, typically port 993)
    // requireTLS: true,       // Force STARTTLS if server supports it, fail if not.
    // ignoreTLS: true,        // Use with extreme caution: allows plain text password over unencrypted connection.
    // For self-signed certificates in dev/test, add:
    // tls: { rejectUnauthorized: false }
  });

  client.logLevel = 'info'; // Set verbosity to 'error', 'warn', 'info', 'debug'

  client.on('error', (err) => {
    console.error('IMAP Client Error:', err);
  });

  try {
    await client.connect();
    console.log('Successfully connected to IMAP server.');

    // Example: List all mailboxes
    const mailboxes = await client.listMailboxes();
    console.log('Available Mailboxes:');
    mailboxes.forEach(mb => console.log(`  - ${mb.name} (Delimiter: '${mb.delimiter}', Flags: ${mb.flags.join(', ')})`));

    // Example: Select 'INBOX' and list messages (first 10)
    console.log('\nSelecting INBOX...');
    const mailboxStatus = await client.selectMailbox('INBOX');
    console.log(`INBOX has ${mailboxStatus.exists} messages, ${mailboxStatus.unseen} unseen.`);

    const messages = await client.listMessages('INBOX', '1:10', ['uid', 'flags', 'envelope']);
    console.log('First 10 messages in INBOX:');
    messages.forEach(msg => {
      console.log(`  UID: ${msg.uid}, Subject: ${msg.envelope.subject ?? '[No Subject]'}, From: ${msg.envelope.from?.[0]?.address ?? 'N/A'}`);
    });

    // Disconnect from the server
    await client.logout();
    console.log('Disconnected from IMAP server.');
  } catch (error) {
    console.error('Failed to connect or perform IMAP operation:', error);
    // It's crucial to create a new client instance after an error if you want to retry.
  }
}

runImapClient();

view raw JSON →