rsaccani / mail-mime-parser
MIME电子邮件消息解析器
Requires
- php: >=7.1
- guzzlehttp/psr7: ^1.7.0|^2.0
- pimple/pimple: ^3.0
- zbateson/mb-wrapper: ^1.0.1
- zbateson/stream-decorators: ^1.0.6
Requires (Dev)
- friendsofphp/php-cs-fixer: *
- mikey179/vfsstream: ^1.6.0
- phpstan/phpstan: *
- phpunit/phpunit: <10
Suggests
- ext-iconv: For best support/performance
- ext-mbstring: For best support/performance
This package is not auto-updated.
Last update: 2024-09-26 08:29:55 UTC
README
这是一个可测试且符合PSR标准的邮件MIME解析器,是PHP的imap*函数和Pear库的替代品,用于读取符合Internet消息格式(RFC 822)的消息(以及后来的修订版RFC 2822、RFC 5322)。
本项目的目标是:
- 编写得很好
- 符合标准但宽容
- 尽可能进行测试
要将其包含在您的项目中,请通过composer安装
composer require zbateson/mail-mime-parser
赞助商
非常感谢我的所有赞助商。所有赞助商 <3
如果这个项目对您有帮助,请考虑赞助我。
PHP 5.4-7.0 支持已被移除
从mail-mime-parser 2.3.0版本开始,对PHP 5.4、5.5、5.6和7.0的支持已被移除。
移除通知(自2.0.0版本起)
getContentResourceHandle、getTextResourceHandle和getHtmlResourceHandle自1.2.1版本开始已弃用,并在2.0.0版本中删除。fread()将仅返回多字节字符的单个字节,因此在某些情况下可能会导致意外结果/警告,应使用psr7流代替。请注意,getBinaryContentResourceHandle和getResourceHandle仍然可用。
2.0版本中的变更
升级到2.0以利用新的按需解析器,该解析器在请求时解析消息的部分。这意味着从较大的消息中只读取标题与读取较小消息一样快,因为不再解析整个消息(同样,只读取内容而不读取消息的大型附件也更快。)
由于按需解析,从2.0版本开始,传递的资源句柄或流必须在返回的消息对象仍在使用时保持打开状态。
旧代码
$handle = fopen('file.mime', 'r'); $message = $mailParser->parse($handle); // returned `Message` fclose($handle);
新代码
// attaches the resource handle to the returned `IMessage` if the second parameter // is true. The resource handle is closed when the IMessage is destroyed. $message = $mailParser->parse(fopen('file.mime', 'r'), true);
有关更改的更完整列表,请访问2.0升级指南。
要求
MailMimeParser需要PHP 7.1或更高版本。已在PHP 7.1、7.2、7.3、7.4、8.0、8.1和8.2上进行了测试。
用法
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(); // parse() accepts a string, resource or Psr7 StreamInterface // pass `true` as the second argument to attach the passed $handle and close // it when the returned IMessage is destroyed. $handle = fopen('file.mime', 'r'); $message = $mailParser->parse($handle, false); // returns `IMessage` // OR: use this procedurally (Message::from also accepts a string, // resource or Psr7 StreamInterface // true or false as second parameter doesn't matter if passing a string. $string = "Content-Type: text/plain\r\nSubject: Test\r\n\r\nMessage"; $message = Message::from($string, false); echo $message->getHeaderValue(HeaderConsts::FROM); // user@example.com 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 ->getPersonName(); // Person Name echo $message ->getHeader(HeaderConsts::CC) // also AddressHeader ->getAddresses()[0] // AddressPart ->getEmail(); // user@example.com 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 // close only when $message is no longer being used. fclose($handle);
文档
升级到1.x或2.x
许可证
BSD许可证 - 请参阅许可协议。