envelopwd
envelopwd is a robust, lightweight Rust library for parsing, validating, and manipulating email addresses and mailboxes.
It was created to address limitations encountered while using the email_address crate - particularly the lack of sqlx integration and the fact that both plain email addresses and name-addr formats were parsed into the same type, which can be awkward in certain contexts.
This crate provides a cleaner separation between address types, adds sqlx support, and introduces additional functionality aimed at improving ergonomics and practical usability.
A significant portion of the parsing logic and test coverage was inspired by and adapted from email_address. Many thanks to its authors for building such a solid and well-tested foundation.
Features
- Strict Validation: Checks local-part and domain lengths, character sets, and subdomain formatting.
- Mailbox Support: Easily parse "Name email@example.com" formats.
- Serialization: Optional Serde support for easy integration with JSON and other formats.
- Internationalization: Supports non-ASCII characters in local parts and domains (UTF-8).
- Zero Dependencies: Uses only
thiserrorfor clean error reporting. - URI Encoding: Built-in support for converting email addresses to
mailto:links. - Case Sensitivity Awareness: Properly handles case-insensitive domains while preserving the (rarely used but valid) case sensitivity of local parts.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
To enable optional features like Serde or sqlx, use:
[]
= { = "0.1", = ["serde", "sqlx"] }
Quick Start
Validating and Parsing an Email
use EmailAddress;
use FromStr;
Working with Mailboxes (Names + Emails)
use Mailbox;
use FromStr;
Optional Features
Serialization with Serde
When the serde feature is enabled, both EmailAddress and Mailbox implement Serialize and Deserialize. They are treated as transparent strings, ensuring they integrate seamlessly with web APIs and configuration files.
use EmailAddress;
use ;
Database Integration with sqlx
Enable the sqlx feature to use EmailAddress and Mailbox directly in your database models.
Postgres arrays
Enable the sqlx_postgres feature to use EmailAddress and Mailbox in array queries.
Lettre Integration
Enable the lettre feature to allow converting types to their lettre counterparts:
let addr = from_str?;
let lettre_addr: Address = addr.try_into?;
let mb = from_str?;
let lettre_mb: Mailbox = mb.try_into?;
Validation Logic
envelopwd ensures:
- Lengths: Local part ≤ 64 octets, Domain ≤ 254 octets.
- Subdomains: Each label is ≤ 63 characters and follows DNS-style alphanumeric rules.
- Quoted Parts: Supports quoted local parts (e.g.,
"john..doe"@example.org). - Literals: Supports domain literals (e.g.,
jsmith@[192.168.2.1]).
License
This project is licensed under Apache-2.0