zbateson/mail-mime-parser
PSR-compliant, testable MIME email parser for PHP (RFC 822/2822/5322). A standards-based but forgiving alternative to imap* and Pear for reading and inspecting messages, headers, parts, and attachments. Requires PHP 8.1+.
Strengths:
MailMimeParser, Message, Header classes) enable selective adoption (e.g., parsing headers vs. attachments).Gaps:
Illuminate\Mail or Illuminate\Events).Psr7\StreamInterface support) ensures compatibility with Laravel’s HTTP layer (e.g., Symfony\Component\HttpFoundation streams).fopen/fclose), which may conflict with Laravel’s resource management (e.g., Storage facade).mail.parsed) for reacting to parsed emails (would need custom middleware/services).ErrorBag but not exposed as Laravel validation exceptions (would need wrapper logic).php-di, guzzlehttp/psr7), reducing risk of breaking changes.mmp-crypt-*) add S/MIME/PGP support but require OpenSSL/PECL extensions, which may not be enabled in all Laravel deployments.Mailable classes may suffice)?openssl for encryption) available in the deployment environment?mail.parsed) or integrate with existing services (e.g., Notifiable)?Symfony\Component\HttpFoundation\FileBag or Psr7 streams from symfony/http-foundation).MailMimeParser singleton) for dependency injection.throw ValidationException).Mailable: Better suited for sending emails; this package is optimized for parsing/receiving.Mime Component: More heavyweight; this package is lighter and email-focused.imap Functions: Less standards-compliant and harder to test.Phase 1: Proof of Concept
composer require zbateson/mail-mime-parser.use ZBateson\MailMimeParser\MailMimeParser;
use Illuminate\Support\Facades\Storage;
$parser = app(MailMimeParser::class);
$stream = Storage::disk('emails')->readStream('inbox/example.eml');
$message = $parser->parse($stream);
Phase 2: Laravel Integration
MailMimeParser to Laravel’s container:
// app/Providers/MailParserServiceProvider.php
public function register()
{
$this->app->singleton(MailMimeParser::class, fn() => new MailMimeParser());
}
HandleIncomingEmail middleware) to parse emails from HTTP requests or queues.Storage facade:
$attachment = $message->getAttachmentPart(0);
$attachment->saveContent(storage_path('app/attachments/' . $attachment->getFileName()));
Phase 3: Advanced Features
zbateson/mmp-crypt-smime or zbateson/mmp-crypt-gpg for S/MIME/PGP support (requires OpenSSL/PECL).ParseEmailJob::dispatch($emailStream)->onQueue('emails');
EmailParsed) to decouple parsing from business logic:
event(new EmailParsed($message));
php-di and guzzlehttp/psr7, which are Laravel-compatible).mbstring recommended for multibyte email handling.Storage facade for attachments.php-di/guzzlehttp/psr7 for breaking changes.fopen/fclose mismanagement (e.g., resource leaks).mbstring functions.getErrors() on Message/Header objects to diagnose parsing issues.ErrorBag errors to Laravel’s log channel.MailMimeParserFacade).How can I help you explore Laravel packages today?