定制 forked/mailreader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

forked/mailreader

Composer 安装命令:

composer require forked/mailreader

包简介

A fully tested simple email reader parser for PHP 5.6+.

README 文档

README

Build StatusBuild statuscodecovCodacy BadgeMaintainability

Receive mail and attachments with PHP

This package can be used to...

  • Parse, decode and read email from Postfix, and others still WIP.
  • For reading messages (Filename extension: eml)
  • Create webMail
  • Store email information such a subject, body, attachments, and etc. into a database

Usage

Full documentation is a work in progress, see Phpunit tests folder for more example usage.

MailReader.php contains the class that works with the incoming email, and the database.

MailParser.php contains the class that works with any file that's in an email format.

mailPipe.php is a sample script using the MailReader class.

mailPipe.php expects to receive raw emails via STDIN.

You can run the script manually by using cat

cat tests/testfile | ./mailPipe.php

Or type On Windows

type tests\testfile | php mailPipe.php

You will likely want to copy mailPipe.php to your own script and adjust the parameters to suite your needs.

This library also allows you to easily parse an email given its content (headers + body).

require 'vendor/autoload.php';

use Mail\MailParser;

$emailPath = "/var/mail/spool/dan/new/12323344323234234234";
$emailParser = new MailParser(file_get_contents($emailPath));

// You can use some predefined methods to retrieve headers...
$to = $emailParser->getTo();
$subject = $emailParser->getSubject();
$cc = $emailParser->getCc();
$from = $emailParser->getFrom();
$fromName = $emailParser->getFromName();
$fromEmail = $emailParser->getFromEmail();
$attachments = $emailParser->getAttachments();

$actualContent = $attachments[0]['content']

// ... or you can use the 'general purpose' method getHeader()
$emailDeliveredToHeader = $emailParser->getHeader('Delivered-To');

$emailBody = $emailParser->getPlain();

Installation

composer require forked/mailreader

Will pull composer forked/mail_mime-decode package in as dependency.

Setup

Configure your mail server to pipe emails to this script. See http://stuporglue.org/add-an-email-address-that-forwards-to-a-script/ for instructions.

Make this script executable, and edit the configuration options to suit your needs. Change permissions of the directories so that the user executing the script (probably the mail user) will have write permission to the file upload directory.

By default the script is configured to save pdf, zip, jpg, png and gif files. Edit the method array property $allowed_mime_types around line 47 to change this. Or call ->addMimeType() to add more.

Postfix configuration to manage email from a mail server

Next you need to forward emails to this script above. For that I'm using Postfix like a mail server, you need to configure /etc/postfix/master.cf

Add this line at the end of the file (specify myhook to send all emails to the script mailPipe.php)

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

Edit this line (register myhook)

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

License

Copyright 2012, Michael Moore stuporglue@gmail.com http://stuporglue.org

Licensed under the same terms as PHP itself and under the GPLv2 or Later. You are free to use this script for personal or commercial projects. Use at your own risk. No guarantees or warranties.

Support

  1. If you have problems, you are encouraged to post them on the MailReader GitHub issue tracker instead of as comments on my blog.
  2. MailReader is OpenSource. You can pay (or not) anyone you want (including yourself!) to work on MailReader, the code is here.
  3. I will accept GitHub pull requests that fix bugs or add features. This sort of maintenance will be done for free.

Thanks

Many thanks to forahobby of www.360-hq.com for testing this script and helping me find the initial bugs and Craig Hopson of twitterrooms.co.uk for help tracking down an iOS email handling bug.

Versions

July 9, 2019

  • many additions, library more OOP compliant.
  • added methods to easily retrieved records from the database.
  • added additional classes to work with any email formated file/folder.
  • added PSR-4 support, can now be installed using Composer.
  • added phpunit tests, and email files to test against.
  • removed allowed senders, any email received with script will get an reply if turned on.
  • the mailPipe script now setup to auto locate the database config and create database if not initialized.
  • general code clean up.

May 21, 2013

  • UUEncoded attachment support
  • It's now a class
  • Uses PHP PDO connection with prepared statements instead of mysql/mysql_real_escape_string
  • Support for inline content type (from mail app on mac?)

April 14, 2012

  • Uses PEAR's mimeDecode.php
  • Support for more mime part configurations

March 24, 2010

  • Initial release
  • Works for me, for gmail.com
  • Homemade parser!

forked/mailreader 适用场景与选型建议

forked/mailreader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.73k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 07 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「parser」 「email」 「decode」 「pipe」 「reader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 forked/mailreader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 forked/mailreader 我们能提供哪些服务?
定制开发 / 二次开发

基于 forked/mailreader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 39
  • 开发语言: PHP

其他信息

  • 授权协议: LGPL-3.0-or-later
  • 更新时间: 2019-07-10