MIME email message parser

1.0.1 2019-12-30 16:31 UTC

This package is not auto-updated.

Last update: 2022-06-22 21:56:35 UTC


Testable and PSR-compliant mail mime parser alternative to PHP's imap* functions and Pear libraries for reading messages in Internet Message Format RFC 822 (and later revisions RFC 2822, RFC 5322).

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version

The goals of this project are to be:

  • Well written
  • Standards-compliant but forgiving
  • Tested where possible

To include it for use in your project, please install via composer:

composer require zbateson/mail-mime-parser


MailMimeParser requires PHP 5.4 or newer. Tested on PHP 5.4, 5.5, 5.6, 7, 7.1, 7.2 and 7.3 on travis.

Please note: hhvm support has been dropped as it no longer supports 'php' as of version 4. Previous versions of hhvm may still work, but are no longer supported.


// use an instance of MailMimeParser as a class dependency
$mailParser = new \ZBateson\MailMimeParser\MailMimeParser();

$handle = fopen('file.mime', 'r');
// parse() accepts a string, resource or Psr7 StreamInterface
$message = $mailParser->parse($handle);         // returns a \ZBateson\MailMimeParser\Message

// OR: use this procedurally (Message::from also accepts a string,
// resource or Psr7 StreamInterface
$message = \ZBateson\MailMimeParser\Message::from($string);

echo $message->getHeaderValue('from');          //
echo $message
    ->getHeader('from')                         // AddressHeader
    ->getPersonName();                          // Person Name
echo $message->getHeaderValue('subject');       // The email's subject
echo $message
    ->getHeader('to')                           // also AddressHeader
    ->getAddresses()[0]                         // AddressPart
    ->getName();                                // Person Name
echo $message
    ->getHeader('cc')                           // also AddressHeader
    ->getAddress()[0]                           // AddressPart
    ->getEmail();                               //

echo $message->getTextContent();                // or getHtmlContent()

$att = $message->getAttachmentPart(0);          // first attachment
echo $att->getHeaderValue('Content-Type');      // e.g. "text/plain"
echo $att->getHeaderParameter(                  // value of "charset" part
echo $att->getContent();                        // get the attached file's contents
$stream = $att->getContentStream();             // the file is decoded automatically
$dest = \GuzzleHttp\Psr7\stream_for(
    $stream, $dest
// OR: more simply if saving or copying to another stream
$att->saveContent('my-file.ext');               // writes to my-file.ext
$att->saveContent($stream);                     // copies to the stream


Upgrading to 1.x


BSD licensed - please see license agreement.