Mail MIME Parser
Testable, PSR-compliant PHP library for parsing RFC 822/2822/5322 email messages, MIME parts, headers, and attachments.
Repository Health
Technical Analysis
Mail MIME Parser is a standards-compliant but forgiving alternative to PHP’s imap_* functions and legacy Pear libraries for reading Internet Message Format email. It parses messages from strings, resources, or PSR-7 streams into a rich object model, giving you typed access to headers, addresses, text and HTML bodies, and attachments. The parser is stream-backed for memory efficiency and thoroughly unit tested.
What You Get
- A MailMimeParser::parse() entry point accepting strings, resources, or PSR-7 streams
- A structured IMessage object model with typed header and address access
- Convenience accessors for text content, HTML content, and attachment parts
- Forgiving parsing of real-world, non-conformant email while staying standards-based
Common Use Cases
- Extracting sender, subject, and body from inbound email in a mail-processing pipeline
- Reading attachments out of stored .eml or MIME files
- Migrating away from PHP’s imap_* functions to a testable, modern parser
Under The Hood
Architecture
Parsing is orchestrated by MailMimeParser, which builds an IMessage from a byte stream via a set of parser services and part factories. The message is modeled as a tree of MIME parts, each carrying its own headers; header values are lazily parsed into typed objects (address headers, parameter headers) through dedicated consumer classes.
Tech Stack
PHP 8.1+ library depending on zbateson/mb-wrapper, zbateson/stream-decorators, PSR-7 and PSR-3 interfaces, and Guzzle streams. Uses PHP-DI for internal dependency wiring. Distributed via Composer as zbateson/mail-mime-parser.
Code Quality
The project emphasizes testability: an extensive PHPUnit suite lives under tests/, PHPStan static analysis is configured via phpstan.neon, and CI runs across PHP 8.1 through 8.5. Interfaces are used throughout so parsing components can be mocked and swapped.
API Design
The everyday API is a single parse() call plus fluent accessors like getHeaderValue(), getTextContent(), and getAttachmentPart(). Header constants (HeaderConsts) avoid magic strings, and comprehensive documentation on mail-mime-parser.org lowers the barrier for common tasks.