Need help with mail-mime-parser?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

zbateson
266 Stars 39 Forks BSD 2-Clause "Simplified" License 567 Commits 22 Opened issues

Description

An email parser written in PHP

Services available

!
?

Need anything else?

Contributors list

# 82,356
PHP
mime-pa...
PHPUnit
HTML
478 commits
# 44,695
slugify
twig-ex...
Nette
zend-fr...
28 commits
# 22,077
whois-l...
php7
Nette
zend-fr...
3 commits
# 51,491
PHP
wordpre...
WordPre...
mime-pa...
2 commits
# 367,664
mime-pa...
HTML
JavaScr...
jQuery
2 commits
# 55,317
PHP
mime-pa...
Shell
erp
2 commits
# 644,945
PHP
mime-pa...
2 commits
# 56,005
PHP
laminas
Shell
html-to...
2 commits
# 230,474
HTML
jquery-...
React
Node.js
1 commit
# 410,718
Symfony
forge
mime-pa...
Python
1 commit
# 499,401
mime-pa...
HTML
ast
Shell
1 commit
# 3,530
PHP
PHPUnit
Nette
seleniu...
1 commit
# 221,580
PHP
mime-pa...
Shell
data-tr...
1 commit
# 764,408
PHP
mime-pa...
1 commit
# 204,145
TypeScr...
Laravel
oauth2
Compose...
1 commit
# 110,624
Ruby
Shell
libreof...
ods
1 commit
# 16,035
vue-cli
vuetify
Sass
mjml
1 commit

zbateson/mail-mime-parser

Testable and PSR-compliant mail mime parser alternative to PHP's imap* functions and Pear libraries for reading messages in Internet Message Format RFC 822 (and later revisions RFC 2822, RFC 5322).

Build Status Code Coverage Scrutinizer Code Quality Total Downloads Latest Stable Version

The goals of this project are to be:

  • Well written
  • Standards-compliant but forgiving
  • Tested where possible

To include it for use in your project, please install via composer:

composer require zbateson/mail-mime-parser

Deprecation Notice (since 1.2.1)

getContentResourceHandle, getTextResourceHandle, and getHtmlResourceHandle have all been deprecated due to #106. fread() will only return a single byte of a multibyte char, and so will cause potentially unexpected results/warnings in some cases, and psr7 streams should be used instead. Note that this deprecation doesn’t apply to getBinaryContentResourceHandle or getResourceHandle.

Requirements

MailMimeParser requires PHP 5.4 or newer. Tested on PHP 5.4, 5.5, 5.6, 7, 7.1, 7.2, 7.3, 7.4 and 8.0 on travis.

Usage

use ZBateson\MailMimeParser\MailMimeParser;
use ZBateson\MailMimeParser\Message;
use ZBateson\MailMimeParser\Header\HeaderConsts;

// use an instance of MailMimeParser as a class dependency $mailParser = new MailMimeParser();

$handle = fopen('file.mime', 'r'); // parse() accepts a string, resource or Psr7 StreamInterface $message = $mailParser->parse($handle); // returns Message fclose($handle);

// OR: use this procedurally (Message::from also accepts a string, // resource or Psr7 StreamInterface $message = Message::from($string);

echo $message->getHeaderValue(HeaderConsts::FROM); // [email protected] echo $message ->getHeader(HeaderConsts::FROM) // AddressHeader ->getPersonName(); // Person Name echo $message->getHeaderValue(HeaderConsts::SUBJECT); // The email's subject echo $message ->getHeader(HeaderConsts::TO) // also AddressHeader ->getAddresses()[0] // AddressPart ->getName(); // Person Name echo $message ->getHeader(HeaderConsts::CC) // also AddressHeader ->getAddresses()[0] // AddressPart ->getEmail(); // [email protected]

echo $message->getTextContent(); // or getHtmlContent()

echo $message->getHeader('X-Foo'); // for custom or undocumented headers

$att = $message->getAttachmentPart(0); // first attachment echo $att->getHeaderValue(HeaderConsts::CONTENT_TYPE); // e.g. "text/plain" echo $att->getHeaderParameter( // value of "charset" part 'content-type', 'charset' ); echo $att->getContent(); // get the attached file's contents $stream = $att->getContentStream(); // the file is decoded automatically $dest = \GuzzleHttp\Psr7\stream_for( fopen('my-file.ext') ); \GuzzleHttp\Psr7\copy_to_stream( $stream, $dest ); // OR: more simply if saving or copying to another stream $att->saveContent('my-file.ext'); // writes to my-file.ext $att->saveContent($stream); // copies to the stream

Documentation

Upgrading to 1.x

License

BSD licensed - please see license agreement.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.