An open API service indexing awesome lists of open source software.

https://github.com/opcodesio/mail-parser

Parse emails using PHP without the mailparse extension
https://github.com/opcodesio/mail-parser

Last synced: about 2 months ago
JSON representation

Parse emails using PHP without the mailparse extension

Awesome Lists containing this project

README

        




Mail Parser for PHP
Simple, fast, no extensions required




Features |
Installation |
Credits


Packagist
Packagist
PHP from Packagist

## Features

[OPcodes's](https://www.opcodes.io/) **Mail Parser** has a very simple API to parse emails and their MIME contents. Unlike many other parsers out there, this package does not require the [mailparse](https://www.php.net/manual/en/book.mailparse.php) PHP extension.

Has not been fully tested against RFC 5322.

## Get Started

### Requirements

- **PHP 8.0+**

### Installation

To install the package via composer, Run:

```bash
composer require opcodesio/mail-parser
```

### Usage

```php
use Opcodes\MailParser\Message;

// Parse a message from a string
$message = Message::fromString('...');
// Or from a file location (accessible with file_get_contents())
$message = Message::fromFile('/path/to/email.eml');

$message->getHeaders(); // get all headers
$message->getHeader('Content-Type'); // 'multipart/mixed; boundary="----=_Part_1_1234567890"'
$message->getFrom(); // 'Arunas
$message->getTo(); // 'John Doe
$message->getSubject(); // 'Subject line'
$message->getDate(); // DateTime object when the email was sent
$message->getSize(); // Email size in bytes

$message->getParts(); // Returns an array of \Opcodes\MailParser\MessagePart, which can be html parts, text parts, attachments, etc.
$message->getHtmlPart(); // Returns the \Opcodes\MailParser\MessagePart containing the HTML body
$message->getTextPart(); // Returns the \Opcodes\MailParser\MessagePart containing the Text body
$message->getAttachments(); // Returns an array of \Opcodes\MailParser\MessagePart that represent attachments

$messagePart = $message->getParts()[0];

$messagePart->getHeaders(); // array of all headers for this message part
$messagePart->getHeader('Content-Type'); // value of a particular header
$messagePart->getContentType(); // 'text/html; charset="utf-8"'
$messagePart->getContent(); // '....'
$messagePart->getSize(); // 312
$messagePart->getFilename(); // name of the file, in case this is an attachment part
```

## Contributing

A guide for contributing is in progress...

## Security Vulnerabilities

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

## Credits

- [Arunas Skirius](https://github.com/arukompas)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.