PHPackages                             popphp/pop-mime - 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. popphp/pop-mime

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

popphp/pop-mime
===============

Pop Mime Component for Pop PHP Framework

2.0.3(6mo ago)320.5k—8.3%3BSD-3-ClausePHPPHP &gt;=8.3.0CI passing

Since Nov 19Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/popphp/pop-mime)[ Packagist](https://packagist.org/packages/popphp/pop-mime)[ Docs](https://github.com/popphp/pop-mime)[ RSS](/packages/popphp-pop-mime/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (15)Used By (3)

pop-mime
========

[](#pop-mime)

[![Build Status](https://github.com/popphp/pop-mime/workflows/phpunit/badge.svg)](https://github.com/popphp/pop-mime/actions)[![Coverage Status](https://camo.githubusercontent.com/13dd5895fe5946ff3b886ca364f85368d2d08aef60a6e1d5e63512f404210611/687474703a2f2f63632e706f707068702e6f72672f636f7665726167652e7068703f636f6d703d706f702d6d696d65)](http://cc.popphp.org/pop-queue/)

[![Join the chat at https://discord.gg/TZjgT74U7E](https://camo.githubusercontent.com/acad7b0eeb78b78d08ffd2b85681ab243436388b5f86f8bcb956a69246e53739/68747470733a2f2f6d656469612e706f707068702e6f72672f696d672f646973636f72642e737667)](https://discord.gg/TZjgT74U7E)

- [Overview](#overview)
- [Install](#install)
- [Quickstart](#quickstart)
- [Parts](#parts)
    - [Attachments](#attachments)
- [Headers](#headers)
    - [Header Values](#header-values)
    - [Multiple Header Values](#multiple-header-values)
- [Multipart Messages](#multipart-messages)
- [Parsing](#parsing)

Overview
--------

[](#overview)

`pop-mime` is a component that provides the ability to work with MIME messages and content. With it, you can generate properly-formatted MIME messages with all their related headers and parts, or you can parse pre-existing MIME messages into their respective objects and work with them from there. This can be utilized with mail and HTTP components, such as `pop-mail` and `pop-http`.

`pop-mime` is a component of the [Pop PHP Framework](https://www.popphp.org/).

Install
-------

[](#install)

Install `pop-mime` using Composer.

```
composer require popphp/pop-mime

```

Or, require it in your composer.json file

```
"require": {
    "popphp/pop-mime" : "^2.0.3"
}

```

[Top](#pop-mime)

Quickstart
----------

[](#quickstart)

### Creating a Simple MIME Message:

[](#creating-a-simple-mime-message)

```
use Pop\Mime\Message;
use Pop\Mime\Part\Body;

$message = new Message();
$message->addHeaders([
    'Subject' => 'Hello World',
    'To'      => 'test@test.com',
    'Date'    => date('m/d/Y g:i A')
]);

$body = new Body('Hello World!');
$message->setBody($body);

echo $message;
```

This will produce the following MIME message:

```
Subject: Hello World
To: test@test.com
Date: 11/13/2023 5:38 PM

Hello World!

```

[Top](#pop-mime)

Parts
-----

[](#parts)

The main message object is essentially a top-level part object. A part object can contain headers, a body object or other nested part objects. When a part object has nested parts, this creates a multipart message. The required boundaries are automatically generated.

```
use Pop\Mime\Message;
use Pop\Mime\Part;

$message = new Message();
$message->addHeaders([
    'Subject' => 'Hello World',
    'To'      => 'test@test.com',
    'Date'    => date('m/d/Y g:i A')
]);

$message->setSubType('alternative');

$html = new Part();
$html->addHeader('Content-Type', 'text/html');
$html->setBody('This is the HTML message.');

$text = new Part();
$text->addHeader('Content-Type', 'text/plain');
$text->setBody('This is the text message.');

$message->addParts([$html, $text]);

echo $message;
```

The example above would produce:

```
Subject: Hello World
To: test@test.com
Date: 10/31/2023 6:12 PM
Content-Type: multipart/alternative; boundary=f79f7366a24132e15132142b0a830a9cac98010f

This is a multi-part message in MIME format.
--f79f7366a24132e15132142b0a830a9cac98010f
Content-Type: text/html

This is the HTML message.
--f79f7366a24132e15132142b0a830a9cac98010f
Content-Type: text/plain

This is the text message.
--f79f7366a24132e15132142b0a830a9cac98010f--

```

[Top](#pop-mime)

### Attachments

[](#attachments)

Part objects can be file attachments as well.

```
use Pop\Mime\Message;
use Pop\Mime\Part;

$message = new Message();
$message->addHeaders([
    'Subject'      => 'Hello World',
    'To'           => 'test@test.com',
    'Date'         => date('m/d/Y g:i A'),
    'MIME-Version' => '1.0'
]);

$message->setSubType('mixed');

$html = new Part();
$html->addHeader('Content-Type', 'text/html');
$html->setBody('This is the HTML message.');

$text = new Part();
$text->addHeader('Content-Type', 'text/plain');
$text->setBody('This is the text message.');

$file = new Part();
$file->addHeader('Content-Type', 'application/pdf');
$file->addFile('test.pdf');

$message->addParts([$html, $text, $file]);

echo $message;
```

The example above would produce:

```
Subject: Hello World
To: test@test.com
Date: 11/13/2023 5:46 PM
MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary=5bedb090b0b35ce8029464dbec97013c3615cc5a

This is a multi-part message in MIME format.
--5bedb090b0b35ce8029464dbec97013c3615cc5a
Content-Type: text/html

This is the HTML message.
--5bedb090b0b35ce8029464dbec97013c3615cc5a
Content-Type: text/plain

This is the text message.
--5bedb090b0b35ce8029464dbec97013c3615cc5a
Content-Type: application/pdf
Content-Disposition: attachment; filename=test.pdf
Content-Transfer-Encoding: base64

JVBERi0xLjQKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURl
Y29kZT4+CnN0cmVhbQp4nC3KPQvCQBCE4X5/xdRC4uya3F1gOUhAC7vAgYXY+dEJpvHv5yIyMMXL
[...base64 encoded file contents...]
QzQ2RUUyMDU1RkIxOEY3PiBdCi9Eb2NDaGVja3N1bSAvNUZDMzQxQzBFQzc0MTA2MTZEQzFGRjk4
MDdFMzNFRDgKPj4Kc3RhcnR4cmVmCjc2NDQKJSVFT0YK

--5bedb090b0b35ce8029464dbec97013c3615cc5a--

```

[Top](#pop-mime)

Headers
-------

[](#headers)

The header and header value objects allow for easy creation and granular control over the header values of a MIME message.

```
$header = new Header('Content-Type', 'text/html');
echo $header;
```

```
Content-Type: text/html

```

[Top](#pop-mime)

#### Header Values

[](#header-values)

Header values can be passed into a header object as strings, but they will become header value objects. When fetching them, you can get the value object like this:

```
$header = new Header('Content-Type', 'text/html');
print_r($header->getValue()); // Returns an instance of Pop\Mime\Header\Value
```

```
Pop\Mime\Part\Header\Value Object
(
    [scheme:protected] =>
    [value:protected] => text/html
    [parameters:protected] => Array
        (
        )

    [delimiter:protected] => ;
    [forceQuote:protected] =>
)

```

The benefit of the header value object is that it allows fine-grain control over the header value, including scheme, parameters, the delimiter and whether or not to force quotes.

##### Example 1:

[](#example-1)

```
use Pop\Mime\Part\Header;

$header = new Header('Content-Disposition');
$value  = new Header\Value('attachment');
$value->addParameter('filename', 'filename.jpg');

$header->addValue($value);
echo $header;
```

```
Content-Disposition: attachment; filename=filename.jpg

```

##### Example 2:

[](#example-2)

```
$header = new Header('Authorization');
$value  = new Header\Value();
$value->setDelimiter(',')
    ->setScheme('Digest ')
    ->setForceQuote(true)
    ->addParameter('username', 'my_username')
    ->addParameter('realm', 'my_realm')
    ->addParameter('nonce', 'my-nonce-123456')
    ->addParameter('uri', '/my-uri')
    ->addParameter('response', 'my-response-123456');

$header->addValue($value);
echo $header;
```

```
Authorization: Digest username="my_username", realm="my_realm", nonce="my-nonce-123456", uri="/my-uri", response="my-response-123456"

```

You can always get the header value as a string:

```
$headerString = $header->getValueAsString();
```

[Top](#pop-mime)

#### Multiple Header Values

[](#multiple-header-values)

In some cases, a header may need to contain multiple values. They can be passed as an array to the constructor:

```
$header = new Header('X-Multi-Header', ['value-1', 'value-2', 'value-3']);
```

or, by individual header value object:

```
$header = new Header('X-Multi-Header');
$header->addValue('value-1')
    ->addValue('value-2')
    ->addValue('value-3')
echo $header;
```

```
X-Multi-Header: value-1
X-Multi-Header: value-2
X-Multi-Header: value-3

```

You can access each header value by index:

```
$value = $header->getValue(2);
```

[Top](#pop-mime)

Multipart Messages
------------------

[](#multipart-messages)

There is a interface to assist in easily creating multipart messages, instead of doing it the more manual way outlined in the above examples.

#### HTTP Multipart Form

[](#http-multipart-form)

```
use Pop\Mime\Message;

$formData = [
    'username' => 'admin@test/whatever%DUDE!',
    'password' => '123456',
    'colors'   => ['Red', 'Green']
];

$formMessage = Message::createForm($formData);
echo $formMessage;
```

```
Content-Type: multipart/form-data; boundary=1f39a2798e049befa5b835a1434a6c7a21e9713a

This is a multi-part message in MIME format.
--1f39a2798e049befa5b835a1434a6c7a21e9713a
Content-Disposition: form-data; name=username

admin%40test%2Fwhatever%25DUDE%21
--1f39a2798e049befa5b835a1434a6c7a21e9713a
Content-Disposition: form-data; name=password

123456
--1f39a2798e049befa5b835a1434a6c7a21e9713a
Content-Disposition: form-data; name=colors[]

Red
--1f39a2798e049befa5b835a1434a6c7a21e9713a
Content-Disposition: form-data; name=colors[]

Green
--1f39a2798e049befa5b835a1434a6c7a21e9713a--

```

If you just need the main form parts without the top-level header and MIME preamble, you can do that like this:

```
use Pop\Mime\Message;

$formData = [
    'username' => 'admin@test/whatever%DUDE!',
    'password' => '123456',
    'colors'   => ['Red', 'Green']
];

$formMessage = Message::createForm($formData);
echo $formMessage->renderRaw();
```

And that will render just the form data content, removing the top-level header and the preamble:

```
--28fd350696733cf5d2c466383a7e0193a5cfffc3
Content-Disposition: form-data; name=username

admin%40test%2Fwhatever%25DUDE%21
--28fd350696733cf5d2c466383a7e0193a5cfffc3
Content-Disposition: form-data; name=password

123456
--28fd350696733cf5d2c466383a7e0193a5cfffc3
Content-Disposition: form-data; name=colors[]

Red
--28fd350696733cf5d2c466383a7e0193a5cfffc3
Content-Disposition: form-data; name=colors[]

Green
--28fd350696733cf5d2c466383a7e0193a5cfffc3--

```

#### HTTP Multipart Form with a File

[](#http-multipart-form-with-a-file)

You can also create form data with files in a couple of different ways as well:

*Example 1:*

```
$formData = [
    'file'     => [
        'filename'    => __DIR__ . '/test.pdf',
        'contentType' => 'application/pdf'
    ]
];
```

*Example 2:*

```
$formData = [
    'file'     => [
        'filename' => 'test.pdf',
        'contents' => file_get_contents(__DIR__ . '/test.pdf')
        'mimeType' => 'application/pdf'
    ]
];
```

In example 1, the file on disk is passed and put into the form data from there. In example 2, the file contents are explicitly passed to the `contents` key to set the file data into the form data. Also, for flexibility, the following case-insensitive keys are acceptable for `Content-Type`:

- Content-Type
- contentType
- Mime-Type
- mimeType
- mime

[Top](#pop-mime)

Parsing
-------

[](#parsing)

#### *Note:*

[](#note)

*This component adheres to the MIME standard which uses CRLF ("\\r\\n") for line breaks. If a mime message does not adhere to this standard, parsing may not work as intended.*

**Parsing a message:**

To parse MIME messages and content, you can take the string of MIME message content and pass it in the following method and it will return a message object with all of the related headers and parts.

```
use Pop\Mime\Message;

$message = Message::parseMessage($messageString);
```

**Parsing a header string:**

If you happen to have the MIME header string, you can parse just that like below. This will return an array of header objects:

```
use Pop\Mime\Message;

$headers = Message::parseMessage($headerString);
```

**Parsing a body string:**

If you happen to have the MIME body string, you can parse just that like below. This will return an array of part objects:

```
use Pop\Mime\Message;

$parts = Message::parseBody($bodyString);
```

**Parsing a single part string:**

And if you happen to have the string of a single MIME part, you can parse just that like below. This will return a part object:

```
use Pop\Mime\Message;

$part = Message::parsePart($partString);
```

**Parsing form data:**

As a special case, if you have `multipart/form-data` MIME content, you can parse it like below. This will return a form data array:

```
use Pop\Mime\Message;

$formData = Message::parseForm($formString);
```

It's important to note that in order for the above example to work properly, it has to have a header with at least the `Content-Type` defined, including the boundary that will be used in parsing the form data:

```
Content-Type: multipart/form-data;
    boundary=5bedb090b0b35ce8029464dbec97013c3615cc5a

--5bedb090b0b35ce8029464dbec97013c3615cc5a
Content-Disposition: form-data; name="username"

admin
--5bedb090b0b35ce8029464dbec97013c3615cc5a
Content-Disposition: form-data; name="password"

password
--5bedb090b0b35ce8029464dbec97013c3615cc5a--

```

[Top](#pop-mime)

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance66

Regular maintenance activity

Popularity30

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 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 ~241 days

Recently: every ~196 days

Total

10

Last Release

200d ago

Major Versions

1.2.1 → 2.0.02023-11-09

PHP version history (6 changes)1.0.0PHP &gt;=7.1.0

1.1.0PHP &gt;=7.3.0

1.1.2PHP &gt;=7.4.0

2.0.0PHP &gt;=8.1.0

2.0.1PHP &gt;=8.2.0

2.0.3PHP &gt;=8.3.0

### Community

Maintainers

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

---

Top Contributors

[![nicksagona](https://avatars.githubusercontent.com/u/898670?v=4)](https://github.com/nicksagona "nicksagona (57 commits)")

---

Tags

phpmimemime-parserpoppop phpmime generator

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/popphp-pop-mime/health.svg)

```
[![Health](https://phpackages.com/badges/popphp-pop-mime/health.svg)](https://phpackages.com/packages/popphp-pop-mime)
```

###  Alternatives

[php-mime-mail-parser/php-mime-mail-parser

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

9979.6M27](/packages/php-mime-mail-parser-php-mime-mail-parser)[fileeye/mimemap

A PHP library to handle MIME Content-Type fields and their related file extensions.

259.2M9](/packages/fileeye-mimemap)[bashkarev/email

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

208.8k](/packages/bashkarev-email)[popphp/pop-db

Pop Db Component for Pop PHP Framework

1814.6k11](/packages/popphp-pop-db)[popphp/pop-http

Pop Http Component for Pop PHP Framework

1018.5k13](/packages/popphp-pop-http)

PHPackages © 2026

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