decodellc/php-mime-mail-parser

(FORK) 完全测试过的PHP 5.4+邮件解析扩展包装器

2.9.2 2017-09-30 15:15 UTC

README

一个完全测试过的PHP 5.4+邮件解析扩展包装器

Latest Version Total Downloads Software License

为什么使用它?

此扩展可以用于...

  • 解析和读取来自Postfix的邮件
  • 创建webmail
  • 将电子邮件信息(如主题、HTML正文、附件等)存储到数据库中

它可靠吗?

是的。所有已知问题都已重现、修复并测试。

我们使用Travis CI来帮助确保代码质量。您可以在下面的实时统计信息中查看

Build Status Coverage Quality Score

如何安装它?

最简单的方法是通过 Composer

要安装PHP MIME Mail Parser的最新版本,请运行以下命令

composer require php-mime-mail-parser/php-mime-mail-parser

要求

以下版本的PHP得到支持

  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • PHP 7
  • HHVM

请确保您已正确安装mailparse扩展(https://php.ac.cn/manual/en/book.mailparse.php

pecl install mailparse      		#PHP Version = 7
pecl install mailparse-2.1.6		#PHP Version < 7

如果您在Ubuntu上安装mailparse遇到麻烦,请查看这篇教程

另外请注意,您可能需要创建 /etc/php5/mods-available/mailparse.ini 文件,并包含行 extension=mailparse.so。然后运行 sudo php5enmod mailparse 以启用它。

在Windows上,您需要从http://pecl.php.net/package/mailparse下载mailparse DLL,并将“extension=php_mailparse.dll”行添加到相应的php.ini中。

如何使用它?

<?php
// Include the library first
require_once __DIR__.'/vendor/autoload.php';

$path = 'path/to/mail.txt';
$Parser = new PhpMimeMailParser\Parser();

// There are four methods available to indicate which mime mail to parse.
// You only need to use one of the following four:

// 1. Specify a file path to the mime mail.
$Parser->setPath($path); 

// 2. Specify a php file resource (stream) to the mime mail.
$Parser->setStream(fopen($path, "r"));

// 3. Specify the raw mime mail text.
$Parser->setText(file_get_contents($path));

// 4.  Specify a stream to work with mail server
$Parser->setStream(fopen("php://stdin", "r"));

// Once we've indicated where to find the mail, we can parse out the data
$to = $Parser->getHeader('to');             // "test" <test@example.com>, "test2" <test2@example.com>
$addressesTo = $Parser->getAddresses('to'); //Return an array : [[test, test@example.com, false],[test2, test2@example.com, false]]

$from = $Parser->getHeader('from');             // "test" <test@example.com>
$addressesFrom = $Parser->getAddresses('from'); //Return an array : test, test@example.com, false

$subject = $Parser->getHeader('subject');

$text = $Parser->getMessageBody('text');

$html = $Parser->getMessageBody('html');
$htmlEmbedded = $Parser->getMessageBody('htmlEmbedded'); //HTML Body included data

$stringHeaders = $Parser->getHeadersRaw();	// Get all headers as a string, no charset conversion
$arrayHeaders = $Parser->getHeaders();		// Get all headers as an array, with charset conversion

// Pass in a writeable path to save attachments
$attach_dir = '/path/to/save/attachments/'; 	// Be sure to include the trailing slash
$include_inline = true;  			// Optional argument to include inline attachments (default: true)
$Parser->saveAttachments($attach_dir [,$include_inline]);

// Get an array of Attachment items from $Parser
$attachments = $Parser->getAttachments([$include_inline]);

//  Loop through all the Attachments
if (count($attachments) > 0) {
	foreach ($attachments as $attachment) {
		echo 'Filename : '.$attachment->getFilename().'<br />'; // logo.jpg
		echo 'Filesize : '.filesize($attach_dir.$attachment->getFilename()).'<br />'; // 1000
		echo 'Filetype : '.$attachment->getContentType().'<br />'; // image/jpeg
		echo 'MIME part string : '.$attachment->getMimePartStr().'<br />'; // (the whole MIME part of the attachment)
	}
}

?>

接下来,您需要将电子邮件转发到上面的脚本。为此,我使用 Postfix 作为邮件服务器,您需要配置 /etc/postfix/master.cf

在文件末尾添加此行(指定myhook以将所有邮件发送到脚本test.php)

myhook unix - n n - - pipe
  				flags=F user=www-data argv=php -c /etc/php5/apache2/php.ini -f /var/www/test.php ${sender} ${size} ${recipient}

编辑此行(注册myhook)

smtp      inet  n       -       -       -       -       smtpd
        			-o content_filter=myhook:dummy

PHP脚本必须使用第四种方法与这种配置一起工作。

我可以贡献吗?

请随意贡献!

git clone https://github.com/php-mime-mail-parser/php-mime-mail-parser
cd php-mime-mail-parser
composer install
./vendor/bin/phpunit

如果您报告问题,请提供触发它的原始电子邮件。这有助于我们更快地重现和修复问题。

许可证

php-mime-mail-parser/php-mime-mail-parser是开源软件,许可协议为MIT许可