mathsgod / eml2html
Convert EML files to self-contained HTML with base64-embedded inline images
v1.0.0
2026-04-15 04:05 UTC
Requires
- php: >=8.0
- php-mime-mail-parser/php-mime-mail-parser: ^9.0
Requires (Dev)
- phpunit/phpunit: ^9.0 || ^10.0
README
A PHP library to convert EML files into self-contained HTML, with all inline images embedded as base64 data URIs.
Requirements
- PHP 8.0+
ext-mailparse(pecl install mailparse)
Installation
composer require mathsgod/eml2html
Usage
Convert from a file
use Eml2Html\Converter; $converter = new Converter(); $html = $converter->convertFile('/path/to/email.eml'); file_put_contents('output.html', $html);
Convert from a string
use Eml2Html\Converter; $converter = new Converter(); $eml = file_get_contents('/path/to/email.eml'); $html = $converter->convertString($eml);
How it works
- Parses the EML file using
php-mime-mail-parser. - Extracts the HTML body (Quoted-Printable and charset decoding handled automatically).
- Collects all inline attachments and maps each
Content-IDto a base64 data URI. - Replaces every
cid:reference in the HTML (including thecid:<id>bracketed variant emitted by Microsoft Word/Outlook) with the corresponding data URI, producing a fully self-contained HTML file.
API
Converter::convertFile(string $path): string
Reads the EML file at $path and returns self-contained HTML.
Throws \InvalidArgumentException if the file cannot be read.
Converter::convertString(string $eml): string
Accepts raw EML content as a string and returns self-contained HTML.
License
MIT