PHPackages                             php-mime-mail-parser/php-mime-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. [Mail &amp; Notifications](/categories/mail)
4. /
5. php-mime-mail-parser/php-mime-mail-parser

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

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

A fully tested email parser for PHP 8.0+ (mailparse extension wrapper).

9.0.1(1y ago)9979.6M—9.6%199[11 PRs](https://github.com/php-mime-mail-parser/php-mime-mail-parser/pulls)20MITPHPPHP ^8.0CI passing

Since Mar 17Pushed 6mo ago35 watchersCompare

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

READMEChangelog (10)Dependencies (3)Versions (95)Used By (20)

[![Logo php-mime-mail-parser](/art/php-mime-mail-parser.png)](/art/php-mime-mail-parser.png)

[![Build Status](https://github.com/php-mime-mail-parser/php-mime-mail-parser/actions/workflows/main.yml/badge.svg?style=flat-square)](https://github.com/php-mime-mail-parser/php-mime-mail-parser/actions)[![Total Downloads](https://camo.githubusercontent.com/18f5e5881142545fc96af8c4e00b714ad8c8097b7acbc43dacfb99f7b18ca2fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7068702d6d696d652d6d61696c2d7061727365722f7068702d6d696d652d6d61696c2d7061727365722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/php-mime-mail-parser/php-mime-mail-parser)[![Latest Stable Version](https://camo.githubusercontent.com/68ca4ca09d9813fcf8013e38a8085879c250ae50acc00c75d6fad8786f665173/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068702d6d696d652d6d61696c2d7061727365722f7068702d6d696d652d6d61696c2d7061727365722e7376673f7374796c653d666c61742d737175617265)](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases)[![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)

Introduction
------------

[](#introduction)

A fully tested email parser for PHP 8.0+ (mailparse extension 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).

Why?
----

[](#why)

This extension can be used to...

- Parse and read email from Postfix
- Read messages (Filename extension: `.eml`)
- Create webmail
- Store email information such a subject, HTML body, attachments, 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:

[![CI](https://github.com/php-mime-mail-parser/php-mime-mail-parser/actions/workflows/main.yml/badge.svg?style=flat-square)](https://github.com/php-mime-mail-parser/php-mime-mail-parser/actions/workflows/main.yml)[![Coverage](https://camo.githubusercontent.com/58ff09ed06d04db14b8fae8da597df647078b2e33248c1f8b9815c83232ed3c0/68747470733a2f2f636f6465636f762e696f2f67682f7068702d6d696d652d6d61696c2d7061727365722f7068702d6d696d652d6d61696c2d7061727365722f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d7754534962584a444c30)](https://codecov.io/gh/php-mime-mail-parser/php-mime-mail-parser)[![Code Quality](https://camo.githubusercontent.com/beb686dae8048e9c0c2df65062e196680b3b6d19b9ff0053cfde684fc2d0439e/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3863626665306663643834633462326239323832623961306234343637363037)](https://app.codacy.com/gh/php-mime-mail-parser/php-mime-mail-parser/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)

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 8.0
- PHP 8.1
- PHP 8.2
- PHP 8.3
- PHP 8.4

Previous Versions:

PHP CompatibilityVersionHHVM[php-mime-mail-parser 2.11.1](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/2.11.1)PHP 5.4[php-mime-mail-parser 2.11.1](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/2.11.1)PHP 5.5[php-mime-mail-parser 2.11.1](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/2.11.1)PHP 5.6[php-mime-mail-parser 3.0.4](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/3.0.4)PHP 7.0[php-mime-mail-parser 3.0.4](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/3.0.4)PHP 7.1[php-mime-mail-parser 5.0.5](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/5.0.5)PHP 7.2[php-mime-mail-parser 7.1.2](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/7.1.2)PHP 7.3[php-mime-mail-parser 7.1.2](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/7.1.2)PHP 7.4[php-mime-mail-parser 7.1.2](https://github.com/php-mime-mail-parser/php-mime-mail-parser/releases/tag/7.1.2)Make 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)

#### Debian, Ubuntu &amp; derivatives

[](#debian-ubuntu--derivatives)

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

```

#### MacOS

[](#macos)

```
brew install php
pecl install mailparse

```

#### Other platforms

[](#other-platforms)

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

```

#### From source

[](#from-source)

AAAAMMDD should be `php-config --extension-dir`

```
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 /usr/lib/php/AAAAMMDD/
echo "extension=mailparse.so" | sudo tee /etc/php/8.4/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 differents ways:

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

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

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

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

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

// 4. or specify a stream to work with a 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", 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, PhpMimeMailParser\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, PhpMimeMailParser\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, PhpMimeMailParser\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 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

    $stream = $attachment->getStream();
    // get the stream of the attachment file

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

Alternatives
------------

[](#alternatives)

If you're looking for alternatives to this library, here are a few options you might consider:

- [zbateson/mail-mime-parser](https://github.com/zbateson/mail-mime-parser): If you prefer not to use the mailparse extension, this library is a good alternative. However, parsing will likely be slower.
- [DirectoryTree/ImapEngine](https://github.com/DirectoryTree/ImapEngine): If you need to parse emails available on an IMAP server, this library can meet your needs.
- [Mailcare](https://mailcare.io): A service that acts as both an email receiving server and a parser, and can be connected with any code. It's a SaaS solution that can simplify incoming email management.

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

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

To forward mails from [Postfix](http://www.postfix.org/) to the PHP script above, add this line at the end of your `/etc/postfix/master.cf`(to 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/php8/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 (see above) to work with this configuration.

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

67

—

FairBetter than 100% of packages

Maintenance55

Moderate activity, may be stable

Popularity71

Solid adoption and visibility

Community47

Growing community involvement

Maturity84

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 81% 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 ~47 days

Recently: every ~118 days

Total

83

Last Release

527d ago

Major Versions

4.0.3 → 5.0.02019-04-28

5.0.5 → 6.0.02020-01-14

6.0.0 → 7.0.02021-02-25

7.1.2 → 8.0.02022-11-06

8.0.4 → 9.0.02024-12-07

PHP version history (9 changes)v1.0.0PHP &gt;=5.3.0

2.0.0PHP &gt;=5.4.0

2.2.3PHP ^5.4.0 || ^7.0

3.0.0PHP ^5.6.0 || ^7.0

4.0.0PHP ^7.1

6.0.0PHP ^7.2

7.0.0PHP ^7.2|^8.0

7.1.1PHP ^7.3|^8.0

8.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/e04c7236e22a6b102a2e8e611fbd4c151d5bde7b12e8a19b36ab34e398ff017d?d=identicon)[message](/maintainers/message)

![](https://avatars.githubusercontent.com/u/1255561?v=4)[Vincent Dauce](/maintainers/eXorus)[@eXorus](https://github.com/eXorus)

---

Top Contributors

[![eXorus](https://avatars.githubusercontent.com/u/1255561?v=4)](https://github.com/eXorus "eXorus (491 commits)")[![fkoyer](https://avatars.githubusercontent.com/u/8293789?v=4)](https://github.com/fkoyer "fkoyer (21 commits)")[![iateadonut](https://avatars.githubusercontent.com/u/1443928?v=4)](https://github.com/iateadonut "iateadonut (11 commits)")[![hadyhallak](https://avatars.githubusercontent.com/u/5484006?v=4)](https://github.com/hadyhallak "hadyhallak (9 commits)")[![VincentDauce](https://avatars.githubusercontent.com/u/18286949?v=4)](https://github.com/VincentDauce "VincentDauce (7 commits)")[![fijiwebdesign](https://avatars.githubusercontent.com/u/809011?v=4)](https://github.com/fijiwebdesign "fijiwebdesign (7 commits)")[![xqus](https://avatars.githubusercontent.com/u/203013?v=4)](https://github.com/xqus "xqus (6 commits)")[![DigiLive](https://avatars.githubusercontent.com/u/7613487?v=4)](https://github.com/DigiLive "DigiLive (5 commits)")[![gmta](https://avatars.githubusercontent.com/u/3210731?v=4)](https://github.com/gmta "gmta (5 commits)")[![lucasvdh](https://avatars.githubusercontent.com/u/4311594?v=4)](https://github.com/lucasvdh "lucasvdh (4 commits)")[![ThibaudDauce](https://avatars.githubusercontent.com/u/1770543?v=4)](https://github.com/ThibaudDauce "ThibaudDauce (4 commits)")[![ThomasLandauer](https://avatars.githubusercontent.com/u/1054469?v=4)](https://github.com/ThomasLandauer "ThomasLandauer (4 commits)")[![myselfhimself](https://avatars.githubusercontent.com/u/1265346?v=4)](https://github.com/myselfhimself "myselfhimself (3 commits)")[![uda](https://avatars.githubusercontent.com/u/199314?v=4)](https://github.com/uda "uda (3 commits)")[![dorianfm](https://avatars.githubusercontent.com/u/274287?v=4)](https://github.com/dorianfm "dorianfm (3 commits)")[![luiz-brandao](https://avatars.githubusercontent.com/u/704231?v=4)](https://github.com/luiz-brandao "luiz-brandao (3 commits)")[![AntoineAugusti](https://avatars.githubusercontent.com/u/295709?v=4)](https://github.com/AntoineAugusti "AntoineAugusti (2 commits)")[![W0rma](https://avatars.githubusercontent.com/u/20659830?v=4)](https://github.com/W0rma "W0rma (2 commits)")[![eryno](https://avatars.githubusercontent.com/u/691315?v=4)](https://github.com/eryno "eryno (2 commits)")[![odknt](https://avatars.githubusercontent.com/u/4185305?v=4)](https://github.com/odknt "odknt (2 commits)")

---

Tags

emailhacktoberfestmailmailparse-extensionmailparserparserphpphpparsermailmimemailparseMimeMailParser

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[zbateson/mail-mime-parser

MIME email message parser

54149.2M79](/packages/zbateson-mail-mime-parser)[bashkarev/email

Faster MIME Mail Parser could be used to parse emails in MIME format.

208.8k](/packages/bashkarev-email)[henrique-borba/php-sieve-manager

A modern (started in 2022) PHP library for the ManageSieve protocol (RFC5804) to create/edit Sieve scripts (RFC5228). Used by Cypht Webmail.

23125.7k2](/packages/henrique-borba-php-sieve-manager)

PHPackages © 2026

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