Standalone, testable and PSR-compliant mail mime parser alternative to PHP's imap* functions and pear libraries for reading messages in Internet Message Format (RFC-5322, RFC-2822 and RFC-822).
The goals of this project are to be:
- Well written
- Standards-compliant but forgiving
- Includable via composer
- Tested where possible
To include it for use in your project, please install via composer:
composer require zbateson/mail-mime-parser
MailMimeParser requires PHP 5.4 or newer or HHVM. Tested on PHP 5.4, 5.5, 5.6 and 7 and HHVM 3.4.
$mailParser = new ZBateson\MailMimeParser\MailMimeParser();
$handle = fopen('file.mime', 'r');
$message = $mailParser->parse($handle); // returns a ZBateson\MailMimeParser\Message
fclose($handle);
echo $message->getHeaderValue('from'); // [email protected]
echo $message
->getHeader('from')
->getPersonName(); // Person Name
echo $message->getHeaderValue('subject'); // The email's subject
echo $message->getTextContent(); // or getHtmlContent
$att = $message->getAttachmentPart(0); // first attachment
echo $att->getHeaderValue('Content-Type'); // text/plain for instance
echo $att->getHeaderParameter( // value of "charset" part
'content-type',
'charset'
);
echo stream_get_contents(
$att->getContentResourceHandle()
);
BSD licensed - please see license agreement.