|
1 | | -mailReader.php |
| 1 | +mailreader |
2 | 2 | ==================================== |
3 | 3 |
|
4 | | -Recieve mail and attachments with PHP |
| 4 | +[](https://travis-ci.org/techno-express/mailreader)[](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 |
5 | 14 |
|
6 | 15 | Usage |
7 | 16 | ------------------------------------- |
8 | | -mailReader.php contains the class that works with the incoming email. |
9 | 17 |
|
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`** |
11 | 29 |
|
| 30 | +```sh |
| 31 | +cat tests/testfile | ./mailPipe.php |
| 32 | +``` |
12 | 33 |
|
13 | | -mailPipe.php expects to recieve raw emails via STDIN. |
| 34 | +Or **`type`** On Windows |
14 | 35 |
|
15 | | -You can run the script manually by using cat |
| 36 | +```cmd |
| 37 | +type tests\testfile | php mailPipe.php |
| 38 | +``` |
16 | 39 |
|
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. |
18 | 41 |
|
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). |
21 | 43 |
|
| 44 | +```php |
| 45 | +require 'vendor/autoload.php'; |
22 | 46 |
|
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 |
24 | 70 | ------------------------------------- |
25 | | -You will need mimeDecode.php from http://pear.php.net/package/Mail_mimeDecode/ |
26 | 71 |
|
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. |
28 | 77 |
|
29 | 78 | Setup |
30 | 79 | ------------------------------------- |
| 80 | + |
31 | 81 | 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/> |
33 | 83 | for instructions. |
34 | 84 |
|
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. |
38 | 86 |
|
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. |
41 | 88 |
|
| 89 | +___Postfix configuration to manage email from a mail server___ |
42 | 90 |
|
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 |
48 | 92 |
|
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) |
50 | 94 |
|
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 | +``` |
52 | 98 |
|
53 | | -Use at your own risk. No guarantees or warranties. |
| 99 | +Edit this line (register myhook) |
54 | 100 |
|
| 101 | +```sh |
| 102 | +smtp inet n - - - - smtpd -o content_filter=myhook:dummy |
| 103 | +``` |
55 | 104 |
|
56 | | -Support |
| 105 | +License |
57 | 106 | ------------------------------------- |
58 | | -MailReader is No Longer Being Supported For Free (But it’s Still Free) |
59 | 107 |
|
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> |
61 | 111 |
|
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. |
63 | 114 |
|
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 | +------------------------------------- |
68 | 117 |
|
| 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. |
69 | 121 |
|
70 | 122 | Thanks |
71 | 123 | ------------------------------------- |
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. |
74 | 124 |
|
| 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. |
75 | 126 |
|
76 | 127 | Versions |
77 | 128 | ------------------------------------- |
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! |
0 commit comments