|
| 1 | +<div align="center"> |
| 2 | + <p> |
| 3 | + <h1>Mail Parser for PHP<br/>Simple, fast, no extensions required</h1> |
| 4 | + </p> |
| 5 | +</div> |
| 6 | + |
| 7 | +<p align="center"> |
| 8 | + <a href="#features">Features</a> | |
| 9 | + <a href="#installation">Installation</a> | |
| 10 | + <a href="#credits">Credits</a> |
| 11 | +</p> |
| 12 | + |
| 13 | +<p align="center"> |
| 14 | +<a href="https://packagist.org/packages/opcodesio/mail-parser"><img src="https://img.shields.io/packagist/v/opcodesio/mail-parser.svg?style=flat-square" alt="Packagist"></a> |
| 15 | +<a href="https://packagist.org/packages/opcodesio/mail-parser"><img src="https://img.shields.io/packagist/dm/opcodesio/mail-parser.svg?style=flat-square" alt="Packagist"></a> |
| 16 | +<a href="https://packagist.org/packages/opcodesio/mail-parser"><img src="https://img.shields.io/packagist/php-v/opcodesio/mail-parser.svg?style=flat-square" alt="PHP from Packagist"></a> |
| 17 | +</p> |
| 18 | + |
| 19 | +## Features |
| 20 | + |
| 21 | +[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. |
| 22 | + |
| 23 | +Has not been fully tested against RFC 5322. |
| 24 | + |
| 25 | +## Get Started |
| 26 | + |
| 27 | +### Requirements |
| 28 | + |
| 29 | +- **PHP 8.0+** |
| 30 | + |
| 31 | +### Installation |
| 32 | + |
| 33 | +To install the package via composer, Run: |
| 34 | + |
| 35 | +```bash |
| 36 | +composer require opcodesio/mail-parser |
| 37 | +``` |
| 38 | + |
| 39 | +### Usage |
| 40 | + |
| 41 | +```php |
| 42 | +use Opcodes\MailParser\Message; |
| 43 | + |
| 44 | +// Parse a message from a string |
| 45 | +$message = Message::fromString('...'); |
| 46 | +// Or from a file location (accessible with file_get_contents()) |
| 47 | +$message = Message::fromFile('/path/to/email.eml'); |
| 48 | + |
| 49 | +$message->getHeaders(); // get all headers |
| 50 | +$message->getHeader('Content-Type'); // 'multipart/mixed; boundary="----=_Part_1_1234567890"' |
| 51 | +$message->getFrom(); // 'Arunas <arunas@example.com> |
| 52 | +$message->getTo(); // 'John Doe <johndoe@example.com> |
| 53 | +$message->getSubject(); // 'Subject line' |
| 54 | +$message->getDate(); // DateTime object when the email was sent |
| 55 | +$message->getSize(); // Email size in bytes |
| 56 | + |
| 57 | +$message->getParts(); // Returns an array of \Opcodes\MailParser\MessagePart, which can be html parts, text parts, attachments, etc. |
| 58 | +$message->getHtmlPart(); // Returns the \Opcodes\MailParser\MessagePart containing the HTML body |
| 59 | +$message->getTextPart(); // Returns the \Opcodes\MailParser\MessagePart containing the Text body |
| 60 | +$message->getAttachments(); // Returns an array of \Opcodes\MailParser\MessagePart that represent attachments |
| 61 | + |
| 62 | +$messagePart = $message->getParts()[0]; |
| 63 | + |
| 64 | +$messagePart->getHeaders(); // array of all headers for this message part |
| 65 | +$messagePart->getHeader('Content-Type'); // value of a particular header |
| 66 | +$messagePart->getContentType(); // 'text/html; charset="utf-8"' |
| 67 | +$messagePart->getContent(); // '<html><body>....' |
| 68 | +$messagePart->getSize(); // 312 |
| 69 | +$messagePart->getFilename(); // name of the file, in case this is an attachment part |
| 70 | +``` |
| 71 | + |
| 72 | +## Contributing |
| 73 | + |
| 74 | +A guide for contributing is in progress... |
| 75 | + |
| 76 | +## Security Vulnerabilities |
| 77 | + |
| 78 | +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. |
| 79 | + |
| 80 | +## Credits |
| 81 | + |
| 82 | +- [Arunas Skirius](https://github.com/arukompas) |
| 83 | +- [All Contributors](../../contributors) |
| 84 | + |
| 85 | +## License |
| 86 | + |
| 87 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments