Symfony Mime

Build and parse MIME messages, emails, and file type detection in PHP

Library
Composer
vv8.1.4
2,827stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
88/100Excellent
Development Activity96
Maintenance100
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
83/100Excellent
Architecture88
Code Quality90
Innovation75
Learning Curve78

Symfony Mime is the standalone PHP component behind email and MIME-message construction across the Symfony ecosystem, most visibly powering symfony/mailer. It models a MIME message as a composable tree of typed parts — TextPart, DataPart (attachments), Multipart\Alternative/Mixed/Related — with an Email builder class on top that handles the common case of subject, from/to addresses, HTML/plain-text bodies, attachments, and embedded images without requiring callers to hand-construct headers or multipart boundaries.

Beyond message assembly, the component ships a MimeTypes registry that guesses a file’s MIME type either from its extension or, via FileinfoMimeTypeGuesser/FileBinaryMimeTypeGuesser, by inspecting file contents directly (using PHP’s fileinfo extension or a file binary fallback). It also includes an HtmlToTextConverter for auto-generating plain-text alternatives from HTML bodies, S/MIME signing and encryption support in Crypto/, and header classes for every RFC-defined MIME header type (mailbox lists, structured/unstructured headers, parameterized headers), making it usable independently as a general MIME-handling toolkit, not just as an email builder.

What You Get

  • Email — a high-level builder for subject, addresses, HTML/plain-text bodies, attachments, and embedded inline images, without manually assembling multipart structures
  • A composable Part hierarchy (TextPart, DataPart, Multipart\Alternative/Mixed/Related) for constructing arbitrary MIME structures beyond simple emails
  • MimeTypes with pluggable guessers (FileinfoMimeTypeGuesser, FileBinaryMimeTypeGuesser) for detecting a file’s MIME type from content or extension
  • HtmlToTextConverter for generating a plain-text alternative body automatically from an HTML email
  • S/MIME signing and encryption support (Crypto/SMimeSigner, SMimeEncrypter) plus a full set of RFC-compliant header classes (mailbox, date, identification, parameterized headers)

Common Use Cases

  • Composing transactional or marketing emails with HTML bodies, plain-text fallbacks, attachments, and embedded images via symfony/mailer
  • Detecting a file’s MIME type reliably from its actual content (not just extension) for upload validation or content-disposition headers
  • Signing or encrypting outgoing email content with S/MIME for compliance-sensitive applications
  • Parsing or generating multipart MIME payloads outside of email — any application needing RFC 2045-compliant message structures

Under The Hood

Architecture — The component models a MIME message as a tree of Part objects (AbstractPart.php as the base, TextPart.php and DataPart.php as leaves, Multipart/ subclasses as composite nodes implementing alternative/mixed/related semantics), with Email.php acting as a builder facade that assembles the correct part tree from a simpler subject/body/attachment API and Message.php/RawMessage.php handling serialization to the raw RFC 2045 wire format. Tech Stack — Pure PHP 8.4+ with only two runtime dependencies (symfony/polyfill-intl-idn, symfony/polyfill-mbstring), keeping the component usable outside the full Symfony framework; dev dependencies (egulias/email-validator, league/html-to-markdown) are wired in only for optional integrations like address validation and HTML-to-text conversion. Code Quality — 45 PHPUnit test files exercise the part hierarchy, header parsing/encoding, MIME-type guessers, and S/MIME crypto; the codebase follows Symfony’s long-standing component conventions (PSR-4 autoloading under Symfony\Component\Mime\, strict interface segregation via MimeTypesInterface/HeaderInterface/BodyRendererInterface) and is one of the most heavily used components in the ecosystem (15.7M monthly installs), which keeps its API surface unusually stable. API Design — The Email builder class collapses what would otherwise be manual multipart/header construction into a fluent, chainable API (->subject()->from()->to()->html()->attach()), while the lower-level Part/Header classes remain available for callers who need to construct non-email MIME structures directly — giving both a low-friction common path and full control when needed.

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