daaki-message
An RFC 5322 email message parser and builder.
Highlights
- Parse and build — turn raw bytes into structured data, or structured data into raw bytes.
- Full MIME support — multipart messages, attachments, Content-Transfer-Encoding, boundary handling.
- Encoded words — RFC 2047 decoding and encoding for non-ASCII headers.
- Internationalized headers — RFC 6532 UTF-8 support.
- Validated types —
HeaderName,MessageId,Addressenforce RFC syntax at construction time. - 7-bit safe output — always produces
7bitorquoted-printableencoding, compatible with any SMTP server. - Zero unsafe code — enforced by
#![deny(unsafe_code)]crate-wide. - No runtime dependency — works with any async runtime or without one.
- Optional serde — enable the
serdefeature forSerialize/Deserializeon all public types.
Quick Start
[]
= "0.2"
Parsing
Parse a raw email into its structured parts:
use parse_email;
let raw = b"From: alice@example.com\r\n\
To: bob@example.com\r\n\
Subject: Hi\r\n\
\r\n\
Hello!";
let email = parse_email.unwrap;
assert_eq!;
assert_eq!;
Building
Construct a well-formed RFC 5322 message from typed inputs:
use ;
let mut email = default;
email.from = vec!;
email.to = vec!;
email.subject = "Hello from daaki".into;
email.body_text = Some;
let built = build_message.unwrap;
assert!;
assert_eq!;
Headers-only parsing
Parse just headers for fast metadata extraction (skips body and MIME processing):
use parse_headers_only;
let raw = b"From: alice@example.com\r\nSubject: Hi\r\n\r\nBody here";
let email = parse_headers_only.unwrap;
assert_eq!;
// body_text, body_html, attachments are not populated
Parsing address lists
Parse a comma-separated address list — for example, a user-typed recipient
field in a compose form or a decoded To/Cc header value — using the liberal
RFC 5322 Section 3.4 parser that daaki-message uses internally for inbound
headers. It handles quoted display names, parenthesized comments, group
syntax, domain literals, and RFC 2047 encoded-word display names:
use ;
let raw = r#""Doe, Jane" <jane@example.com>, alice@example.com"#;
let addrs = parse_address_list;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// `parse_address_list` is liberal (Postel's law) and never errors —
// the returned `Address` records are not validated for outgoing mail.
// For outgoing mail, re-validate each result through `Address::new` /
// `Address::with_name`, which enforce the same strict rules the
// message builder uses:
let validated: = addrs
.into_iter
.map
.collect;
assert!;
Note: this function expects already-decoded text. It does not perform
header unfolding, charset detection, or transfer-encoding decoding — use
parse_email for raw message bytes.
Encoding
build_message always produces 7-bit safe output. Pure ASCII with conforming
lines uses Content-Transfer-Encoding: 7bit; anything else uses
quoted-printable. This guarantees the built bytes can be sent to any SMTP
server — with or without 8BITMIME — and stored via IMAP APPEND without
re-encoding.
use ;
let mut email = default;
email.from = vec!;
email.to = vec!;
email.subject = "Héllo".into;
email.body_text = Some;
let built = build_message.unwrap;
// Non-ASCII body is automatically quoted-printable encoded
assert!;
Standards
| Standard | Coverage |
|---|---|
| RFC 5322 | Internet Message Format — headers, date-time, addresses, message identification |
| RFC 2045 | MIME Part One — Content-Transfer-Encoding (base64, quoted-printable, 7bit, 8bit) |
| RFC 2046 | MIME Part Two — multipart structure, boundary handling, media types |
| RFC 2047 | MIME Part Three — encoded words for non-ASCII in headers (Q and B encoding) |
| RFC 2183 | Content-Disposition — inline and attachment handling |
| RFC 2387 | multipart/related — HTML with inline images via Content-ID |
| RFC 2231 | MIME parameter encoding — charset, language, and continuations |
| RFC 2392 | Content-ID — cid: URL references for inline attachments |
| RFC 6531 | Internationalized email — non-ASCII local-parts and domains |
| RFC 6532 | Internationalized headers — UTF-8 throughout header fields |
License
The contents of this package are licensed under the terms of the MIT license.