$ show php-mime-mail-parser/php-mime-mail-parser
This is a PHP library for parsing MIME email messages conforming to RFC 822/2822/5322. It wraps the `mailparse` PECL extension to provide a clean, object-oriented API for extracting headers, plain text and HTML bodies, attachments, and embedded content from raw email data (file, string, or stream). The library features a middleware system for custom MIME part processing, configurable attachment filename conflict strategies (suffix, random, throw), charset decoding with an extensive alias map, and type-safe enums for body type and filename strategy. It is designed for use in webmail clients, email processing pipelines, and Postfix integration, with support for complex MIME structures including multipart/alternative, inline images, and nested RFC822 messages. The test suite includes over 30 real-world email fixtures covering edge cases, making the library robust enough for production use.
The LLM identified 3 files worth highlighting for reuse:
Implements a generic middleware pipeline for processing MimePart objects. The stack can be configured with middleware instances or class names (via factory), and processes parts in order. This pattern is decoupled from the rest of the parserβit only depends on the `Middleware` interface and `MimePart`. It can be extracted and reused in any PHP project needing a middleware pipeline for structured data transformation, such as token filtering or HTTP middleware.
Decodes email body and header text from various character encodings to UTF-8. Contains a comprehensive static alias map (over 300 entries) that normalizes common encoding names (e.g., 'iso-8859-1', 'latin1') to canonical forms and delegates to PHP's `mb_convert_encoding`. The class is self-contained and can be reused in any email or text processing project requiring robust charset detection and conversion.
An enum that defines three strategies for handling duplicate attachment filenames: `DuplicateThrow` (throws exception), `DuplicateSuffix` (appends a number), and `RandomFilename` (generates UUID-based name). `DuplicateSuffix` includes an internal counter to produce unique names like 'file-2.txt'. This is a clean, reusable pattern for any system that must manage file naming conflicts, not just email attachments.