PHPackages                             yarri/email-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. yarri/email-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

yarri/email-parser
==================

Parses emails, parses them well :)

v0.2.1(9mo ago)014MITPHPPHP &gt;=7.0.0

Since May 26Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/yarri/EmailParser)[ Packagist](https://packagist.org/packages/yarri/email-parser)[ Docs](https://github.com/yarri/EmailParser)[ RSS](/packages/yarri-email-parser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (6)Used By (0)

EmailParser
===========

[](#emailparser)

[![Build Status](https://camo.githubusercontent.com/081bdb311d9194c58007af5f115193257a17b293232cf77d4f44fd93d26b111b/68747470733a2f2f6170702e7472617669732d63692e636f6d2f79617272692f456d61696c5061727365722e7376673f746f6b656e3d4b63375578674b356f71464738735a4168437a67266272616e63683d6d6173746572)](https://app.travis-ci.com/yarri/EmailParser)

Parses emails, parses them well :)

EmailParser tries to simplify some of the pains in the email parsing process:

- All headers, text/plain and text/html parts (which are not attachments) and filenames of attachments are converted into UTF-8 encoding.
- In these components, illegal UTF-8 characters are replaced.
- Attachment filenames are properly sanitized.
- Email can be parsed by its source or by its filename.
- The email source file can be gzipped.
- EmailParser itself determines mime types of all attachments found in the message.
- An email sent as an attachment in another email can be easily accessed by calling $part-&gt;getAttachedEmail().
- A caching mechanism is built-in in EmailParser.
- Contents of attachments can be accessd via [StringBuffer](https://packagist.org/packages/atk14/string-buffer) which has a positive impact on memory consumption.

Usage
-----

[](#usage)

```
$parser = new \Yarri\EmailParser();

// Parsing email
$email = $parser->parse($email_content);
// or
$email = $parser->parseFile("/path/to/email.eml");
// or
$email = $parser->parseFile("/path/to/email.eml.gz");

// Getting headers
$email->getSubject();
$email->getFrom();
$email->getTo();
$email->getDate(); // returns date in the ISO format (YYYY-mm-dd H:i:s) in the current timezone (set via date_default_timezone_set()); e.g. "2025-05-25 12:40:22"
$email->getHeader("Date"); // e.g. "Sun, 25 May 2025 06:37:33 +0200 (CEST)"
$email->getHeader("Return-Path");
$email->getHeader("Subject"); // same as $email->getSubject()
$email->getHeader("Received"); // returns string
$email->getHeader("Received",["as_array" => true]); // returns array of strings
$email->hasAttachment(); // true or false
$email->getSmtpRelayIps(); // e.g ["78.45.42.91","209.85.128.53"]

// Displaying the message
$part = $email->getFirstReadablePart();
// or
$part = $email->getFirstReadablePart(["prefer_html" => true]);
//
header(sprintf(
  "Content-Type: %s; charset=%s",
  $part->getMimeType(), // "text/plain" or "text/html"
  $part->getCharset() // always "UTF-8"
));
echo $part->getContent();

// Traversing email structure
$parts = $email->getParts();
foreach($parts as $part){
  $id = $part->getId(); // 1,2,3...
  $level = $part->getLevel(); // 1,2,3..
  $padding = str_repeat(" ",$level); // " ","  ","   "...
  $mime_type = $part->getMimeType();
  if($part->hasContent()){
    $content_info = $part->getSize()." bytes";
    if($part->getFilename()){
      $content_info .= ", ".$part->getFilename();
    }
  }else{
    $content_info = "no content";
  }
  echo "$id.$padding$mime_type ($content_info)\n";
}

// Something like this can be printed:
/*
1. multipart/related (no content)
2.  multipart/alternative (no content)
3.   text/plain (55 bytes)
4.   text/html (107 bytes)
5.  image/png (11462 bytes, dungeon-master.png)
6.  image/jpeg (9123 bytes, pigeon.jpg)
// */

// Getting parts
$part = $email->getPartById(5);
$part->isAttachment(); // true
$part->getMimeType(); // "image/png"
$part->getContent(); // binary content

// Email sent as an attachment
$email = $parser->parseFile("/path/to/email_with_message_rfc822_part.eml");

$parts = $email->getParts();
$part_message_rfc822 = $parts[2]; // for instance the 3rd part is message/rfc822, i.e. an attached email
$part_message_rfc822->isAttachedEmail(); // true
$attached_email = $part_message_rfc822->getAttachedEmail();

$attached_email->getSubject();
$attached_email->getFrom();
$attached_email->getTo();
$attached_email_parts = $attached_email->getParts();
// etc.

// Caching mechanism
// (you are responsible for providing specific cache path for every email you want to parse)
$email = $parser->parse($email_1_content,"/path/to/cache/for_email_1/");
// or
$email = $parser->parseFile("/path/to/email_2.eml","/path/to/cache/for_email_2/");
// or
$email = $parser->parseFile("/path/to/email_3.eml.gz","/path/to/cache/for_email_3/");

// Displaying attachment via StringBuffer which is memory more efficient
// (only takes effect when caching is active)
header(sprintf('Content-Type: %s',$part->getMimeType());
header(sprintf('Content-Disposition: attachment; filename="%s"',$part->getFilename()));
$buffer = $part->getContentBuffer();
$buffer->printOut();

```

Installation
------------

[](#installation)

Just use the Composer:

```
composer require yarri/email-parser

```

Testing
-------

[](#testing)

The EmailParser is tested automatically using Travis CI in PHP 7.0 to PHP 8.4.

For the tests execution, the package [atk14/tester](https://packagist.org/packages/atk14/tester) is used. It is just a wrapping script for [phpunit/phpunit](https://packagist.org/packages/phpunit/phpunit).

Install required dependencies for development:

```
composer update --dev

```

Run tests:

```
cd test
../vendor/bin/run_unit_tests

```

License
-------

[](#license)

EmailParser is free software distributed [under the terms of the MIT license](http://www.opensource.org/licenses/mit-license)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance59

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity25

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~16 days

Total

5

Last Release

292d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6304dffbd91d7a978f98632b0e4e30d662dcdb691daadb1388a58984e98faf5c?d=identicon)[yarri](/maintainers/yarri)

---

Top Contributors

[![yarri](https://avatars.githubusercontent.com/u/974278?v=4)](https://github.com/yarri "yarri (53 commits)")

### Embed Badge

![Health badge](/badges/yarri-email-parser/health.svg)

```
[![Health](https://phpackages.com/badges/yarri-email-parser/health.svg)](https://phpackages.com/packages/yarri-email-parser)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
