Download the PHP package popphp/pop-mime without Composer
On this page you can find all versions of the php package popphp/pop-mime. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download popphp/pop-mime
More information about popphp/pop-mime
Files in popphp/pop-mime
Package pop-mime
Short Description Pop Mime Component for Pop PHP Framework
License BSD-3-Clause
Homepage https://github.com/popphp/pop-mime
Informations about the package pop-mime
pop-mime
- Overview
- Install
- Quickstart
- Parts
- Part Factories
- Attachments
- Encoding
- Headers
- Header Values
- Multiple Header Values
- Addresses
- Non-ASCII Header Values
- Message and Content IDs
- Multipart Messages
- Parsing
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.
Install
Install pop-mime using Composer.
composer require popphp/pop-mime
Or, require it in your composer.json file
"require": {
"popphp/pop-mime" : "^3.0.0"
}
Top
Quickstart
Creating a Simple MIME Message:
This will produce the following MIME message:
Top
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.
The example above would produce:
Top
Part Factories
Building simple text/plain and text/html parts by hand — creating a Part, adding a Content-Type
header, then setting the body — is common enough to have shortcuts. Part::text() and Part::html() do
all three in one call:
inferSubType() decides the multipart subtype for you instead of calling setSubType() by hand: if the
message has both a text/plain and a text/html part, it sets alternative; if any part is a file
attachment, it sets mixed (a file always wins, even alongside a text/html pair, so an attachment can
never be silently dropped by a mail client that only renders one part of an alternative message). It's
opt-in — nothing calls it automatically — so it's safe to keep using setSubType() directly if you want
full manual control.
This produces the same kind of output as the manual example above (with a freshly-generated boundary each run).
Top
Attachments
Part objects can be file attachments as well.
The example above would produce:
The $file part above can be built in one call instead, using Part::attachment(). It auto-detects the
Content-Type from the file's extension (falling back to application/octet-stream for an unrecognized
one), so the manual addHeader('Content-Type', ...) call isn't needed:
You can still pass an explicit content type as the second argument if you don't want auto-detection:
If you have file content in memory rather than an actual file on disk, Part::attachmentFromContent()
does the same thing without requiring a real file path:
Top
Encoding
addFile() and the attachment factories above default to base64 encoding, but any part's body can use
Pop\Mime\Part\Body\Encoding, a backed enum with these cases:
| Case | Wire value |
|---|---|
BASE64 |
base64 |
QUOTED_PRINTABLE |
quoted-printable |
BINARY |
binary |
_7BIT |
7bit |
_8BIT |
8bit |
URL |
URL |
RAW_URL |
RAW_URL |
Pass it as the third argument to addFile(), or the fourth to Part::attachment()/attachmentFromContent():
A part's body can also be constructed directly with an encoding, independent of the attachment factories:
When a part's body has an encoding set, a matching Content-Transfer-Encoding header is added
automatically on render, and getContents() decodes it back to the original content.
Top
Headers
The header and header value objects allow for easy creation and granular control over the header values of a MIME message.
Top
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:
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 2:
You can always get the header value as a string:
Top
Multiple Header Values
In some cases, a header may need to contain multiple values. They can be passed as an array to the constructor:
or, by individual header value object:
You can access each header value by index:
Top
Addresses
Address headers like To, From, Cc, Bcc and Reply-To get their own value type,
Pop\Mime\Part\Header\AddressList (a collection of Pop\Mime\Part\Header\Address objects), built on top
of the same header/value machinery above. It correctly handles a display name that itself contains a
comma — something naive explode(',', ...) splitting gets wrong — because it tokenizes the whole header
value instead of blindly splitting on every comma:
Two addresses, not three — the comma inside the quoted "Doe, John" display name is not treated as a
separator. render() (or a cast to string) turns the list back into a single header-ready value, adding
quotes only where the display name actually needs them:
You can build one up directly instead of parsing a string, and wire it straight into a header:
Top
Non-ASCII Header Values
A header value containing non-ASCII characters — most commonly a display name in an address, like
José García — is automatically RFC 2047 encoded-word encoded when rendered, so it's safe to pass
UTF-8 text straight into Address, Header\Value, or any address-bearing header without encoding it
yourself first:
Parsing does the inverse automatically — AddressList::parse()/Address::parse() decode an encoded-word
display name back to plain UTF-8 text. The encode/decode logic itself lives in
Pop\Mime\Part\Header\EncodedWord if you need it directly for something outside an address:
Plain ASCII text is left untouched — encoding only kicks in when it's actually needed.
Top
Message and Content IDs
Every message benefits from a unique Message-ID, and an individual part — an inline image referenced
by cid:, for example — can carry its own Content-ID. Both are generated the same way, via
generateId(), which lives on Part (not just Message) so a nested part can generate its own ID too:
setMessageId() generates and sets the header in one call; pass an explicit ID as the first argument if
you already have one, or a domain as the second argument to control what appears after the @
(defaults to $_SERVER['SERVER_NAME'], falling back to localhost):
A nested part uses setContentId() the same way, which sets Content-ID instead of Message-ID:
generateId() alone (without setting anything) is also available if you just want a raw ID string:
Top
Multipart Messages
There is an interface to assist in easily creating multipart messages, instead of doing it the more manual way outlined in the above examples.
HTTP Multipart Form
If you just need the main form parts without the top-level header and MIME preamble, you can do that like this:
And that will render just the form data content, removing the top-level header and the preamble:
HTTP Multipart Form with a File
You can also create form data with files in a couple of different ways as well:
Example 1:
Example 2:
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
Parsing
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.
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:
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 strings, split on the boundary. The boundary has to be passed in explicitly — it isn't detected from the body string itself, since a raw body string has no header to read it from:
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:
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:
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:
Top