Skip to content

Commit e0c3c95

Browse files
committed
initial restructuring and overall modernizing
- many changes, move library around to be more in an **OOP** format. - added methods to easliy retrieved records from the database. - added additional classes to work with any email formated file/folder. - added PSR-4 support, now composer installable. - added phpunit tests, and email files to test against. - removed allowed senders, now any email received will get an reply if turned on. - the mailPipe script now setup to auto locate the database config and create database if not initialized.
1 parent b904c4b commit e0c3c95

29 files changed

+2779
-726
lines changed

.gitattributes

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/examples export-ignore
2+
/tests export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
5+
/.travis.yml export-ignore
6+
/CHANGELOG.md export-ignore
7+
README.md export-ignore
8+
/_config.yml export-ignore
9+
/phpunit.xml.dist export-ignore
10+
/favicon.ico export-ignore
11+
/index.html export-ignore
12+
/index.php export-ignore
13+
/php.png export-ignore
14+
/test.php export-ignore
15+
/mailPiped export-ignore

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build/
2+
composer.lock
3+
composer.phar
4+
phpunit.*
5+
vendor/
6+
mailPiped/
7+
*.lock
8+
*.cache
9+
*.crt
10+
*.pem
11+
*.csr

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
language: php
2+
3+
php:
4+
- 5.6
5+
- 7.0
6+
7+
matrix:
8+
allow_failures:
9+
- php: 5.6
10+
11+
env:
12+
- MYSQL_HOST=127.0.0.1 MYSQL_USER=root
13+
14+
services:
15+
- mysql
16+
17+
before_script:
18+
- composer self-update
19+
- composer require php-coveralls/php-coveralls
20+
- mysql -e 'CREATE DATABASE IF NOT EXISTS ez_test;'
21+
- mysql -e "CREATE USER ez_test@localhost IDENTIFIED BY 'ezTest'; GRANT ALL ON ez_test.* TO ez_test@localhost; FLUSH PRIVILEGES;"
22+
23+
after_success:
24+
- bash <(curl -s https://codecov.io/bash)
25+
26+
script:
27+
- vendor/bin/phpunit --coverage-clover=coverage.xml

.vscode/settings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"cSpell.words": [
3+
"Stubbs",
4+
"autoload",
5+
"gmail",
6+
"lstubbs",
7+
"mailreader",
8+
"phpunit",
9+
"stuporglue",
10+
"testfile"
11+
]
12+
}

LICENSE

Lines changed: 160 additions & 335 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 116 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,157 @@
1-
mailReader.php
1+
mailreader
22
====================================
33

4-
Recieve mail and attachments with PHP
4+
[![Build Status](https://travis-ci.org/techno-express/mailreader.svg?branch=master)](https://travis-ci.org/techno-express/mailreader)[![codecov](https://codecov.io/gh/techno-express/mailreader/branch/master/graph/badge.svg)](https://codecov.io/gh/techno-express/mailreader)
5+
6+
Receive mail and attachments with PHP
7+
8+
This package can be used to...
9+
10+
- Parse, decode and read email from Postfix, and others still WIP.
11+
- For reading messages (Filename extension: eml)
12+
- Create webMail
13+
- Store email information such a subject, body, attachments, and etc. into a database
514

615
Usage
716
-------------------------------------
8-
mailReader.php contains the class that works with the incoming email.
917

10-
mailPipe.php is a sample script using the mailReader class.
18+
Full documentation is a work in progress, see **Phpunit** [tests](#./tests/) folder for more example usage.
19+
20+
**MailReader.php** contains the class that works with the incoming email, and the database.
21+
22+
**MailParser.php** contains the class that works with any file that's in an email format.
23+
24+
**mailPipe.php** is a sample script using the MailReader class.
25+
26+
**mailPipe.php** expects to receive raw emails via **STDIN**.
27+
28+
You can run the script manually by using **`cat`**
1129

30+
```sh
31+
cat tests/testfile | ./mailPipe.php
32+
```
1233

13-
mailPipe.php expects to recieve raw emails via STDIN.
34+
Or **`type`** On Windows
1435

15-
You can run the script manually by using cat
36+
```cmd
37+
type tests\testfile | php mailPipe.php
38+
```
1639

17-
cat testfile.txt | ./mailPipe.php
40+
You will likely want to copy *mailPipe.php* to your own script and adjust the parameters to suite your needs.
1841

19-
You will likely want to copy mailPipe.php to your own script and adjust
20-
the parameters to suite your needs.
42+
This library also allows you to easily parse an email given its content (headers + body).
2143

44+
```php
45+
require 'vendor/autoload.php';
2246

23-
Requirements
47+
use Mail\MailParser;
48+
49+
$emailPath = "/var/mail/spool/dan/new/12323344323234234234";
50+
$emailParser = new MailParser(file_get_contents($emailPath));
51+
52+
// You can use some predefined methods to retrieve headers...
53+
$to = $emailParser->getTo();
54+
$subject = $emailParser->getSubject();
55+
$cc = $emailParser->getCc();
56+
$from = $emailParser->getFrom();
57+
$fromName = $emailParser->getFromName();
58+
$fromEmail = $emailParser->getFromEmail();
59+
$attachments = $emailParser->getAttachments();
60+
61+
$actualContent = $attachments[0]['content']
62+
63+
// ... or you can use the 'general purpose' method getHeader()
64+
$emailDeliveredToHeader = $emailParser->getHeader('Delivered-To');
65+
66+
$emailBody = $emailParser->getPlain();
67+
```
68+
69+
Installation
2470
-------------------------------------
25-
You will need mimeDecode.php from http://pear.php.net/package/Mail_mimeDecode/
2671

27-
I used version 1.5.5.
72+
```shell
73+
composer require forked/mailreader
74+
```
75+
76+
Will pull composer [forked/mail_mime-decode](https://packagist.org/packages/forked/mail_mime-decode) package in as dependency.
2877

2978
Setup
3079
-------------------------------------
80+
3181
Configure your mail server to pipe emails to this script. See
32-
http://stuporglue.org/add-an-email-address-that-forwards-to-a-script/
82+
<http://stuporglue.org/add-an-email-address-that-forwards-to-a-script/>
3383
for instructions.
3484

35-
Make this script executable, and edit the configuration options to suit your needs. Change permissions
36-
of the directories so that the user executing the script (probably the
37-
mail user) will have write permission to the file upload directory.
85+
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.
3886

39-
By default the script is configured to save pdf, zip, jpg, png and gif files.
40-
Edit the switch statements around line 200 to change this.
87+
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.
4188

89+
___Postfix configuration to manage email from a mail server___
4290

43-
License
44-
-------------------------------------
45-
Copyright 2012,
46-
Michael Moore <stuporglue@gmail.com>
47-
http://stuporglue.org
91+
Next you need to forward emails to this script above. For that I'm using [Postfix](http://www.postfix.org/) like a mail server, you need to configure /etc/postfix/master.cf
4892

49-
Licensed under the same terms as PHP itself and under the GPLv2.
93+
Add this line at the end of the file (specify myhook to send all emails to the script mailPipe.php)
5094

51-
You are free to use this script for personal or commercial projects.
95+
```sh
96+
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}
97+
```
5298

53-
Use at your own risk. No guarantees or warranties.
99+
Edit this line (register myhook)
54100

101+
```sh
102+
smtp inet n - - - - smtpd -o content_filter=myhook:dummy
103+
```
55104

56-
Support
105+
License
57106
-------------------------------------
58-
MailReader is No Longer Being Supported For Free (But it’s Still Free)
59107

60-
It has been a fun ride, and many people are still interested in MailReader, but my own interests have moved elsewhere. I haven’t used MailReader for my own projects for nearly 2 years and there has never been any money in it for me or anything like that.
108+
Copyright 2012,
109+
Michael Moore <stuporglue@gmail.com>
110+
<http://stuporglue.org>
61111

62-
I will no longer be supporting MailReader.
112+
Licensed under the same terms as PHP itself and under the GPLv2 or Later.
113+
You are free to use this script for personal or commercial projects. Use at your own risk. No guarantees or warranties.
63114

64-
1. You can still download and use MailReader. Its code will live on GitHub for as long as GitHub is around.
65-
2. If you have problems, you are encouraged to post them on the MailReader GitHub issue tracker instead of as comments on my blog.
66-
3. MailReader is OpenSource. You can pay (or not) anyone you want (including yourself!) to work on MailReader, the code is here.
67-
4. I will accept GitHub pull requests that fix bugs or add features. This sort of maintenance will be done for free.
115+
Support
116+
-------------------------------------
68117

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

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

125+
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.
75126

76127
Versions
77128
-------------------------------------
78-
May 21, 2013
79-
* UUEncoded attachment support
80-
* It's now a class
81-
* Uses PHP PDO connection with prepared statements instead of mysql/mysql_real_escape_string
82-
* Support for inline content type (from mail app on mac?)
83-
84-
April 14, 2012
85-
* Uses PEAR's mimeDecode.php
86-
* Support for more mime part configurations
87-
88-
March 24, 2010
89-
* Initial release
90-
* Works for me, for Gmail.
91-
* Homemade parser!
129+
130+
___July 9, 2019___
131+
132+
- many additions, library more **OOP** compliant.
133+
- added methods to easily retrieved records from the database.
134+
- added additional classes to work with any email formated file/folder.
135+
- added PSR-4 support, can now be installed using [Composer](https://getcomposer.org).
136+
- added phpunit tests, and email files to test against.
137+
- removed allowed senders, any email received with script will get an reply if turned on.
138+
- the mailPipe script now setup to auto locate the database config and create database if not initialized.
139+
- general code clean up.
140+
141+
___May 21, 2013___
142+
143+
- UUEncoded attachment support
144+
- It's now a class
145+
- Uses PHP PDO connection with prepared statements instead of mysql/mysql_real_escape_string
146+
- Support for inline content type (from mail app on mac?)
147+
148+
___April 14, 2012___
149+
150+
- Uses PEAR's mimeDecode.php
151+
- Support for more mime part configurations
152+
153+
___March 24, 2010___
154+
155+
- Initial release
156+
- Works for me, for gmail.com
157+
- Homemade parser!

composer.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "stuporglue/mailreader",
3+
"type": "library",
4+
"description": "A fully tested simple email reader parser for PHP 5.6+.",
5+
"keywords": ["pipe", "email", "reader", "parser", "decode"],
6+
"homepage": "https://github.com/stuporglue/mailreader",
7+
"license": ["LGPL-3.0-or-later", "GPLv2"],
8+
"authors": [
9+
{
10+
"name": "Michael Moore",
11+
"email": "stuporglue@gmail.com",
12+
"homepage": "http://stuporglue.org"
13+
},
14+
{
15+
"name":"Lawrence Stubbs",
16+
"email":"lstubbs@techno.express"
17+
}
18+
],
19+
"require": {
20+
"php": ">5.6",
21+
"forked/mail_mime-decode": "^1.5.6.1"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": ">5.7"
25+
},
26+
"autoload": {
27+
"files": [
28+
"decode/functions.php"
29+
],
30+
"psr-4": {
31+
"Mail\\": "decode/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"Mail\\Tests\\": "tests/"
37+
}
38+
},
39+
"scripts": {
40+
"test": "phpunit --bootstrap vendor/autoload.php tests"
41+
}
42+
}

db.sql

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)