← All packages

@lacspace/imap

v1.0.0Mail Kit0 deps

Zero-dependency IMAP4rev1 client for Node (RFC 3501 + IDLE, MOVE, UIDPLUS, CONDSTORE, SPECIAL-USE, LIST-EXTENDED, LITERAL+, SASL-IR, QUOTA, ID). Implicit TLS and STARTTLS with verification on, LOGIN / PLAIN / XOAUTH2 / OAUTHBEARER, streaming byte-accurate parser, async-iterable FETCH, envelopes with RFC 2047 decoding, BODYSTRUCTURE trees shared with @lacspace/mime, modified UTF-7 mailbox names, special-use detection (incl. Gmail XLIST and localised names), IDLE with NOOP fallback. Works with Hostinger (Dovecot), Gmail, Outlook/Exchange, GoDaddy.

npm i @lacspace/imap

Usage

imap.ts
import { createImapClient } from "@lacspace/imap";

const imap = createImapClient({
  host: "imap.hostinger.com",                       // port 993, TLS by default
  auth: { user: "chandan@lacspace.com", pass: process.env.IMAP_PASS! },
});
await imap.connect();

const boxes = await imap.listMailboxes();
// [{ path: "INBOX", specialUse: "\\Inbox", … }, { path: "Sent", specialUse: "\\Sent", subscribed: true, … }, …]
const sent = boxes.find((b) => b.specialUse === "\\Sent");

const inbox = await imap.select("INBOX");          // { exists, uidValidity, uidNext, highestModseq?, … }

const uids = imap.sortUidsDesc(await imap.search({ unseen: true, since: new Date("2026-10-01") }));

for await (const msg of imap.fetch(uids.slice(0, 50), { envelope: true, flags: true, bodyStructure: true })) {
  console.log(msg.uid, msg.envelope?.from[0]?.address, msg.envelope?.subject); // RFC 2047 already decoded
}

// fetch one text part by partId (from bodyStructure, see @lacspace/mime findTextParts)
const [m] = await imap.fetchAll([uids[0]!], { bodyParts: ["1.1"] });
const raw = m!.parts["1.1"];                          // Uint8Array, still transfer-encoded

await imap.store([uids[0]!], { add: ["\\Seen"] });   // mark seen

// live updates until aborted
const ac = new AbortController();
await imap.idle({ signal: ac.signal, onEvent: (e) => console.log(e.type, e) }); // exists | expunge | fetch | recent | flags

await imap.logout();

Exports 19

ImapAuthErrorImapClientImapCommandErrorImapErrorImapNetworkErrorImapProtocolErrorResponseFramerSPECIAL_USE_NAMESbuildSearchcreateImapClientdecodeModifiedUtf7decodeWordsencodeModifiedUtf7expandSequenceSetparseBodyStructureparseEnvelopeparseImapResponsesortUidsDescuidRange

Keywords

imapimap-clientemailmailwebmailidle

More in Mail Kit

@lacspace/email-templates

Compose bulletproof, responsive, dark-mode HTML emails from simple blocks, plus 13 ready-made transactional templates (OTP, verify, password-reset, magic-link, receipt, order, shipping, invitation, digest, announcement). Ships plaintext generation, preheaders and {{var}} i18n interpolation. Zero-dependency, isomorphic.

@lacspace/email-validate

Smart, network-free email validation — RFC-5322 syntax (incl. quoted local parts & IP-literal domains), disposable/temp-mail & role-account detection, free-provider flags, Gmail normalization and 'did you mean?' typo suggestions. Zero-dependency, isomorphic.

@lacspace/email-verify

Best-effort email deliverability for Node — syntax + disposable/role, MX lookup with priority ranking, an optional SMTP RCPT probe (no mail sent), catch-all detection, a 0-100 confidence score, and de-duped batch verification. All DNS/SMTP injectable; zero npm dependencies.

@lacspace/mailer

A tiny zero-dependency SMTP client for Node — send email over raw net/tls with STARTTLS & AUTH, plus a fluent MIME builder (inline images, attachments, alternatives), RFC 5322 address + RFC 2047 helpers, batch send with retry, and no-network test transports. Provider presets (Hostinger, Gmail, Outlook, Zoho…) make setup one line.

@lacspace/mime

Isomorphic RFC 5322 / RFC 2045-2049 MIME parser and builder for webmail. parseMime() turns raw mail (string or bytes) into from/to/cc/subject/date/text/html/attachments/inline cid images/priority/List-Unsubscribe one-click and the part tree; handles nested multipart (mixed, alternative, related, report, signed), message/rfc822 forwards, base64 and quoted-printable (tolerant), RFC 2047 encoded words (split multibyte, adjacent whitespace), RFC 2231 parameters, 30+ charsets and malformed input without ever throwing. parseBodyStructure() parses IMAP BODYSTRUCTURE (literals, extension data) into the same tree with IMAP partIds, plus findTextParts / listAttachments / decodePart for lazy fetching. buildMime() writes CRLF messages with encoded-word headers, QP/base64 bodies, mixed/alternative/related nesting, Message-ID generation and header-injection guards; replyHeaders() / forwardSubject() for threading. Pairs with @lacspace/imap and @lacspace/mailer. Zero dependencies; Node 18+, edge runtimes and browsers.

@lacspace/mail-sanitize

Isomorphic email-HTML sanitizer for webmail. Allowlist-based tags and attributes, URL scheme filtering (entity/tab/case tricks decoded), remote images blocked behind placeholders with data-lac-src for a "Load images" button, tracking pixels removed and listed, cid: images mapped to your attachment URLs, optional image proxy, <style> rules scoped under your container, class/id prefixing against DOM clobbering, linear-time hand-rolled tokenizer. Plus htmlToText, textToHtml (linkify + quote blocks) and snippet. Zero dependencies, no DOM; Node 18+, edge and browser.