PHPackages                             jungleran/mail-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. jungleran/mail-parser

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

jungleran/mail-parser
=====================

0358PHP

Since Feb 21Pushed 4y ago1 watchersCompare

[ Source](https://github.com/jungleran/mail-parser)[ Packagist](https://packagist.org/packages/jungleran/mail-parser)[ RSS](/packages/jungleran-mail-parser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

php-mime-mail-parser
====================

[](#php-mime-mail-parser)

A fully tested email parser for PHP 7.2+ ([mailparse extension](https://www.php.net/manual/book.mailparse.php) wrapper).

It's the most effective php email parser around in terms of performance, foreign character encoding, attachment handling, and ease of use. Internet Message Format RFC [822](https://tools.ietf.org/html/rfc822), [2822](https://tools.ietf.org/html/rfc2822), [5322](https://tools.ietf.org/html/rfc5322).

[![Latest Version](https://camo.githubusercontent.com/68ca4ca09d9813fcf8013e38a8085879c250ae50acc00c75d6fad8786f665173/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068702d6d696d652d6d61696c2d7061727365722f7068702d6d696d652d6d61696c2d7061727365722e7376673f7374796c653d666c61742d737175617265)](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases)[![Total Downloads](https://camo.githubusercontent.com/18f5e5881142545fc96af8c4e00b714ad8c8097b7acbc43dacfb99f7b18ca2fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7068702d6d696d652d6d61696c2d7061727365722f7068702d6d696d652d6d61696c2d7061727365722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/php-mime-mail-parser/php-mime-mail-parser)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Why?
----

[](#why)

This extension can be used to...

- Parse and read email from Postfix
- For reading messages (Filename extension: eml)
- Create webmail
- Store email information such a subject, HTML body, attachments, and etc. into a database

Is it reliable?
---------------

[](#is-it-reliable)

Yes. All known issues have been reproduced, fixed and tested.

We use GitHub Actions, Codecov, Codacy to help ensure code quality. You can see real-time statistics below:

[![Build Status](https://github.com/php-mime-mail-parser/php-mime-mail-parser/workflows/CI/badge.svg?branch=release-6.0.0&event=push)](https://github.com/php-mime-mail-parser/php-mime-mail-parser/workflows/CI/badge.svg?branch=release-6.0.0&event=push)[![Coverage](https://camo.githubusercontent.com/1240782cd280ac97be72187b461b2898a172c3b2e5365a6429173e1a56e72202/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f7068702d6d696d652d6d61696c2d7061727365722f7068702d6d696d652d6d61696c2d7061727365723f7374796c653d666c61742d737175617265)](https://codecov.io/gh/php-mime-mail-parser/php-mime-mail-parser)[![Code Quality](https://camo.githubusercontent.com/cfd2ccd4d3142be5e5bc0a0c97e8582998975fb4f44ec8ac470d9973e709c056/68747470733a2f2f696d672e736869656c64732e696f2f636f646163792f67726164652f34653065343466656532313134376464626464313866663937363235313837353f7374796c653d666c61742d737175617265)](https://app.codacy.com/app/php-mime-mail-parser/php-mime-mail-parser)

How do I install it?
--------------------

[](#how-do-i-install-it)

The easiest way is via [Composer](https://getcomposer.org/).

To install the latest version of PHP MIME Mail Parser, run the command below:

```
composer require php-mime-mail-parser/php-mime-mail-parser

```

Requirements
------------

[](#requirements)

The following versions of PHP are supported:

- PHP 7.2
- PHP 7.3
- PHP 7.4
- PHP 8.0

Previous Versions:

PHP CompatibilityVersionHHVMphp-mime-mail-parser 2.11.1PHP 5.4php-mime-mail-parser 2.11.1PHP 5.5php-mime-mail-parser 2.11.1PHP 5.6php-mime-mail-parser 3.0.4PHP 7.0php-mime-mail-parser 3.0.4PHP 7.1php-mime-mail-parser 5.0.5Make sure you have the mailparse extension () properly installed. The command line `php -m | grep mailparse` needs to return "mailparse".

### Install mailparse extension

[](#install-mailparse-extension)

#### Ubuntu, Debian &amp; derivatives

[](#ubuntu-debian--derivatives)

```
sudo apt install php-cli php-mailparse

```

#### Php docker image

[](#php-docker-image)

```
# Example with an alpine distribution
RUN apk update && apk add g++ autoconf make re2c && \
    pecl install mailparse && \
    docker-php-ext-enable mailparse && \
    apk del g++ autoconf make re2c

```

#### Others platforms

[](#others-platforms)

```
sudo apt install php-cli php-pear php-dev php-mbstring
pecl install mailparse

```

#### From source

[](#from-source)

```
git clone https://github.com/php/pecl-mail-mailparse.git
cd pecl-mail-mailparse
phpize
./configure
sed -i 's/#if\s!HAVE_MBSTRING/#ifndef MBFL_MBFILTER_H/' ./mailparse.c
make
sudo mv modules/mailparse.so $(php-config --extension-dir)
echo "extension=mailparse.so" | sudo tee /etc/php/7.1/mods-available/mailparse.ini
sudo phpenmod mailparse

```

#### Windows

[](#windows)

You need to download mailparse DLL from  and add the line "extension=php\_mailparse.dll" to php.ini accordingly.

How do I use it?
----------------

[](#how-do-i-use-it)

### Loading an email

[](#loading-an-email)

You can load an email in 4 different ways – choose one:

```
require_once __DIR__.'/vendor/autoload.php';

$path = 'path/to/email.eml';
$parser = new PhpMimeMailParser\Parser();

// 1. Specify a file path (string)
$parser->setPath($path);

// 2. Specify the raw mime mail text (string)
$parser->setText(file_get_contents($path));

// 3. Specify a php file resource (stream)
$parser->setStream(fopen($path, "r"));

// 4.  Specify a stream to work with mail server (stream)
$parser->setStream(fopen("php://stdin", "r"));
```

### Get the metadata of the message

[](#get-the-metadata-of-the-message)

Get the sender and the receiver:

```
$rawHeaderTo = $parser->getHeader('to');
// return "test" , "test2"

$arrayHeaderTo = $parser->getAddresses('to');
// return [["display"=>"test", "address"=>"test@example.com", "is_group"=>false]]

$rawHeaderFrom = $parser->getHeader('from');
// return "test"

$arrayHeaderFrom = $parser->getAddresses('from');
// return [["display"=>"test", "address"=>"test@example.com", "is_group"=>false]]
```

Get the subject:

```
$subject = $parser->getHeader('subject');
```

Get other headers:

```
$stringHeaders = $parser->getHeadersRaw();
// return all headers as a string, no charset conversion

$arrayHeaders = $parser->getHeaders();
// return all headers as an array, with charset conversion
```

### Get the body of the message

[](#get-the-body-of-the-message)

```
$text = $parser->getMessageBody('text');
// return the text version

$html = $parser->getMessageBody('html');
// return the html version

$htmlEmbedded = $parser->getMessageBody('htmlEmbedded');
// return the html version with the embedded contents like images
```

### Get attachments

[](#get-attachments)

Save all attachments in a directory

```
$parser->saveAttachments('/path/to/save/attachments/');
// return all attachments saved in the directory (include inline attachments)

$parser->saveAttachments('/path/to/save/attachments/', false);
// return all attachments saved in the directory (exclude inline attachments)

// Save all attachments with the strategy ATTACHMENT_DUPLICATE_SUFFIX (default)
$parser->saveAttachments('/path/to/save/attachments/', false, Parser::ATTACHMENT_DUPLICATE_SUFFIX);
// return all attachments saved in the directory: logo.jpg, logo_1.jpg, ..., logo_100.jpg, YY34UFHBJ.jpg

// Save all attachments with the strategy ATTACHMENT_RANDOM_FILENAME
$parser->saveAttachments('/path/to/save/attachments/', false, Parser::ATTACHMENT_RANDOM_FILENAME);
// return all attachments saved in the directory: YY34UFHBJ.jpg and F98DBZ9FZF.jpg

// Save all attachments with the strategy ATTACHMENT_DUPLICATE_THROW
$parser->saveAttachments('/path/to/save/attachments/', false, Parser::ATTACHMENT_DUPLICATE_THROW);
// return an exception when there is attachments duplicate.
```

Get all attachments

```
$attachments = $parser->getAttachments();
// return an array of all attachments (include inline attachments)

$attachments = $parser->getAttachments(false);
// return an array of all attachments (exclude inline attachments)
```

Loop through all the Attachments

```
foreach ($attachments as $attachment) {
    echo 'Filename : '.$attachment->getFilename().'';
    // return logo.jpg

    echo 'Filesize : '.filesize($attach_dir.$attachment->getFilename()).'';
    // return 1000

    echo 'Filetype : '.$attachment->getContentType().'';
    // return image/jpeg

    echo 'MIME part string : '.$attachment->getMimePartStr().'';
    // return the whole MIME part of the attachment

    $attachment->save('/path/to/save/myattachment/', Parser::ATTACHMENT_DUPLICATE_SUFFIX);
    // return the path and the filename saved (same strategy available than saveAttachments)
}
```

Postfix configuration to manage email from a mail server
--------------------------------------------------------

[](#postfix-configuration-to-manage-email-from-a-mail-server)

Next you need to forward emails to this script above. For that I'm using [Postfix](http://www.postfix.org/) like a mail server, you need to configure /etc/postfix/master.cf

Add this line at the end of the file (specify myhook to send all emails to the script test.php)

```
myhook unix - n n - - pipe
  				flags=F user=www-data argv=php -c /etc/php5/apache2/php.ini -f /var/www/test.php ${sender} ${size} ${recipient}

```

Edit this line (register myhook)

```
smtp      inet  n       -       -       -       -       smtpd
        			-o content_filter=myhook:dummy

```

The php script must use the fourth method to work with this configuration.

And finally the easiest way is to use my SaaS

My sponsors
-----------

[](#my-sponsors)

Thanks to [Bertel Torp](https://github.com/berteltorp) who is my first sponsor. Thanks to [Will Browning](https://github.com/willbrowningme) who is my second sponsor.

Can I contribute?
-----------------

[](#can-i-contribute)

Feel free to contribute!

```
git clone https://github.com/php-mime-mail-parser/php-mime-mail-parser
cd php-mime-mail-parser
composer install
./vendor/bin/phpunit

```

If you report an issue, please provide the raw email that triggered it. This helps us reproduce the issue and fix it more quickly.

License
-------

[](#license)

The php-mime-mail-parser/php-mime-mail-parser is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity27

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/3cf4ebd9bedf6084a094cf15d5ca31a08683c6e7f15f0cc7acffc89e8fbea27e?d=identicon)[jungleran](/maintainers/jungleran)

---

Top Contributors

[![ranqiangjun](https://avatars.githubusercontent.com/u/5101533?v=4)](https://github.com/ranqiangjun "ranqiangjun (3 commits)")

### Embed Badge

![Health badge](/badges/jungleran-mail-parser/health.svg)

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

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[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)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

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

PHPackages © 2026

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