forked from DirectoryTree/ImapEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMbox.php
More file actions
45 lines (35 loc) · 1.05 KB
/
Mbox.php
File metadata and controls
45 lines (35 loc) · 1.05 KB
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
<?php
namespace DirectoryTree\ImapEngine;
use DirectoryTree\ImapEngine\Exceptions\RuntimeException;
use Generator;
class Mbox
{
/**
* Constructor.
*/
public function __construct(
protected string $filepath
) {}
/**
* Get the messages from the mbox file.
*/
public function messages(
string $delimiter = '/^From \\S+\\s+(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s+\\d{1,2}\\s+\\d{2}:\\d{2}:\\d{2}\\s+\\d{4}/'
): Generator {
if (! $handle = fopen($this->filepath, 'r')) {
throw new RuntimeException('Failed to open mbox file: '.$this->filepath);
}
$buffer = '';
while (($line = fgets($handle)) !== false) {
if (preg_match($delimiter, $line) && $buffer !== '') {
yield new FileMessage($buffer);
$buffer = '';
}
$buffer .= $line;
}
if ($buffer !== '') {
yield new FileMessage($buffer);
}
fclose($handle);
}
}