address-rfc2821 is a JavaScript/TypeScript library designed for parsing email addresses conforming to RFC-5321 (formerly RFC-821/2821) specifically for the SMTP envelope, found in `MAIL FROM:` and `RCPT TO:` commands. This module focuses on the nuances of envelope addressing, including handling null senders (`<>`) and correctly managing quoted local-parts. Since version 2.0.0, it transitioned from a regex-based parser to one built with Nearley, providing more robust and accurate parsing. It also supports UTF-8 email addresses according to RFCs 5890-5892, providing the domain in punycode when necessary. The current stable version is 2.1.5, with recent updates including the addition of TypeScript type definitions in v2.1.4 and a fix to `isNull()`'s return type in v2.1.5. The library maintains an active release cadence, reflecting ongoing development and maintenance for Haraka mail server components.
npm install address-rfc2821Verified import paths — ran on the pinned version, not inferred.
Demonstrates parsing various RFC-5321 compliant email addresses, including null senders and quoted local-parts. It shows how to access user and host components, format the address, and handle internationalized domains with punycode.
Review parsing logic, especially for local-parts with double quotes or other non-standard characters. Ensure existing tests cover these cases.
Ensure your Node.js environment is version 11 or higher. The current recommended `engines` field specifies `>= 20.20.0`.
Remove manual `@types/address-rfc2821` or local `.d.ts` files, and use the types shipped with the package: `import type { Address } from 'address-rfc2821';`Ensure any code checking `address.isNull()` expects a boolean result. Most JavaScript truthy/falsy checks will continue to work, but strict type comparisons (`===`) might fail if they expected a non-boolean value.
Ensure the email string strictly follows the RFC-5321 envelope address format. For header addresses (To, From, CC), use a parser like `address-rfc2822` or `email-addresses` instead.
If using CommonJS, ensure you're accessing the `Address` class correctly: `const { Address } = require('address-rfc2821');` or `const Address = require('address-rfc2821').Address;`. If using ESM, make sure to use named import: `import { Address } from 'address-rfc2821';`.