Mail MIME Parser

Testable, PSR-compliant PHP library for parsing RFC 822/2822/5322 email messages, MIME parts, headers, and attachments.

Library
Composer
v4.0.3
550stars
BSD-2-Clause

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
71/100Good
Development Activity60
Maintenance68
Community76
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture85
Code Quality86
Innovation70
Learning Curve74

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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search