forked from daniele-occhipinti/php-email-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlancakeEmailParser.php
338 lines (288 loc) · 10.9 KB
/
PlancakeEmailParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/*************************************************************************************
* ===================================================================================*
* Software by: Danyuki Software Limited *
* This file is part of Plancake. *
* *
* Copyright 2009-2010-2011 by: Danyuki Software Limited *
* Support, News, Updates at: http://www.plancake.com *
* Licensed under the LGPL version 3 license. * *
* Danyuki Software Limited is registered in England and Wales (Company No. 07554549) *
**************************************************************************************
* Plancake is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License v3.0 for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
**************************************************************************************
*
* Valuable contributions by:
* - Chris
*
* **************************************************************************************/
/**
* Extracts the headers and the body of an email
* Obviously it can't extract the bcc header because it doesn't appear in the content
* of the email.
*
* N.B.: if you deal with non-English languages, we recommend you install the IMAP PHP extension:
* the Plancake PHP Email Parser will detect it and used it automatically for better results.
*
* For more info, check:
* https://github.com/plancake/official-library-php-email-parser
*
* @author dan
*/
class PlancakeEmailParser {
const PLAINTEXT = 1;
const HTML = 2;
/**
*
* @var boolean
*/
private $isImapExtensionAvailable = false;
/**
*
* @var string
*/
private $emailRawContent;
/**
*
* @var associative array
*/
protected $rawFields;
/**
*
* @var array of string (each element is a line)
*/
protected $rawBodyLines;
/**
*
* @param string $emailRawContent
*/
public function __construct($emailRawContent) {
$this->emailRawContent = $emailRawContent;
$this->extractHeadersAndRawBody();
if (function_exists('imap_open')) {
$this->isImapExtensionAvailable = true;
}
}
private function extractHeadersAndRawBody()
{
$lines = preg_split("/(\r?\n|\r)/", $this->emailRawContent);
$currentHeader = '';
$i = 0;
foreach ($lines as $line)
{
if(self::isNewLine($line))
{
// end of headers
$this->rawBodyLines = array_slice($lines, $i);
break;
}
if ($this->isLineStartingWithPrintableChar($line)) // start of new header
{
preg_match('/([^:]+): ?(.*)$/', $line, $matches);
$newHeader = strtolower($matches[1]);
$value = $matches[2];
$this->rawFields[$newHeader] = $value;
$currentHeader = $newHeader;
}
else // more lines related to the current header
{
if ($currentHeader) { // to prevent notice from empty lines
$this->rawFields[$currentHeader] .= substr($line, 1);
}
}
$i++;
}
}
/**
*
* @return string (in UTF-8 format)
* @throws Exception if a subject header is not found
*/
public function getSubject()
{
if (!isset($this->rawFields['subject']))
{
throw new Exception("Couldn't find the subject of the email");
}
$ret = '';
if ($this->isImapExtensionAvailable) {
foreach (imap_mime_header_decode($this->rawFields['subject']) as $h) { // subject can span into several lines
$charset = ($h->charset == 'default') ? 'US-ASCII' : $h->charset;
$ret .= iconv($charset, "UTF-8//TRANSLIT", $h->text);
}
} else {
$ret = utf8_encode(iconv_mime_decode($this->rawFields['subject']));
}
return $ret;
}
/**
*
* @return array
*/
public function getCc()
{
if (!isset($this->rawFields['cc']))
{
return array();
}
return explode(',', $this->rawFields['cc']);
}
/**
*
* @return array
* @throws Exception if a to header is not found or if there are no recipient
*/
public function getTo()
{
if ( (!isset($this->rawFields['to'])) || (!count($this->rawFields['to'])))
{
throw new Exception("Couldn't find the recipients of the email");
}
return explode(',', $this->rawFields['to']);
}
/**
* return string - UTF8 encoded
*
* Example of an email body
*
--0016e65b5ec22721580487cb20fd
Content-Type: text/plain; charset=ISO-8859-1
Hi all. I am new to Android development.
Please help me.
--
My signature
email: [email protected]
web: http://www.example.com
--0016e65b5ec22721580487cb20fd
Content-Type: text/html; charset=ISO-8859-1
*/
public function getBody($returnType=self::PLAINTEXT)
{
$body = '';
$detectedContentType = false;
$contentTransferEncoding = null;
$charset = 'ASCII';
$waitingForContentStart = true;
if ($returnType == self::HTML)
$contentTypeRegex = '/^Content-Type: ?text\/html/i';
else
$contentTypeRegex = '/^Content-Type: ?text\/plain/i';
// there could be more than one boundary
preg_match_all('!boundary=(.*)$!mi', $this->emailRawContent, $matches);
$boundaries = $matches[1];
// sometimes boundaries are delimited by quotes - we want to remove them
foreach($boundaries as $i => $v) {
$boundaries[$i] = str_replace(array("'", '"'), '', $v);
}
foreach ($this->rawBodyLines as $line) {
if (!$detectedContentType) {
if (preg_match($contentTypeRegex, $line, $matches)) {
$detectedContentType = true;
}
if(preg_match('/charset=(.*)/i', $line, $matches)) {
$charset = strtoupper(trim($matches[1], '"'));
}
} else if ($detectedContentType && $waitingForContentStart) {
if(preg_match('/charset=(.*)/i', $line, $matches)) {
$charset = strtoupper(trim($matches[1], '"'));
}
if ($contentTransferEncoding == null && preg_match('/^Content-Transfer-Encoding: ?(.*)/i', $line, $matches)) {
$contentTransferEncoding = $matches[1];
}
if (self::isNewLine($line)) {
$waitingForContentStart = false;
}
} else { // ($detectedContentType && !$waitingForContentStart)
// collecting the actual content until we find the delimiter
// if the delimited is AAAAA, the line will be --AAAAA - that's why we use substr
if (is_array($boundaries)) {
if (in_array(substr($line, 2), $boundaries)) { // found the delimiter
break;
}
}
$body .= $line . "\n";
}
}
if (!$detectedContentType)
{
// if here, we missed the text/plain content-type (probably it was
// in the header), thus we assume the whole body is what we are after
$body = implode("\n", $this->rawBodyLines);
}
// removing trailing new lines
$body = preg_replace('/((\r?\n)*)$/', '', $body);
if ($contentTransferEncoding == 'base64')
$body = base64_decode($body);
else if ($contentTransferEncoding == 'quoted-printable')
$body = quoted_printable_decode($body);
if($charset != 'UTF-8') {
// FORMAT=FLOWED, despite being popular in emails, it is not
// supported by iconv
$charset = str_replace("FORMAT=FLOWED", "", $charset);
$bodyCopy = $body;
$body = iconv($charset, 'UTF-8//TRANSLIT', $body);
if ($body === FALSE) { // iconv returns FALSE on failure
$body = utf8_encode($bodyCopy);
}
}
return $body;
}
/**
* @return string - UTF8 encoded
*
*/
public function getPlainBody()
{
return $this->getBody(self::PLAINTEXT);
}
/**
* return string - UTF8 encoded
*/
public function getHTMLBody()
{
return $this->getBody(self::HTML);
}
/**
* N.B.: if the header doesn't exist an empty string is returned
*
* @param string $headerName - the header we want to retrieve
* @return string - the value of the header
*/
public function getHeader($headerName)
{
$headerName = strtolower($headerName);
if (isset($this->rawFields[$headerName]))
{
return $this->rawFields[$headerName];
}
return '';
}
/**
*
* @param string $line
* @return boolean
*/
public static function isNewLine($line)
{
$line = str_replace("\r", '', $line);
$line = str_replace("\n", '', $line);
return (strlen($line) === 0);
}
/**
*
* @param string $line
* @return boolean
*/
private function isLineStartingWithPrintableChar($line)
{
return preg_match('/^[A-Za-z]/', $line);
}
}
?>