|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rdx\imap; |
|
4
|
|
|
|
|
5
|
|
|
use rdx\imap\IMAPMessage; |
|
6
|
|
|
use rdx\imap\IMAPTransport; |
|
7
|
|
|
|
|
8
|
|
|
class IMAPMailbox { |
|
9
|
|
|
|
|
10
|
|
|
protected $server = ''; |
|
11
|
|
|
protected $username = ''; |
|
12
|
|
|
protected $password = ''; |
|
13
|
|
|
protected $mailbox = ''; |
|
14
|
|
|
protected $flags = []; |
|
15
|
|
|
|
|
16
|
|
|
protected $imap; // rdx\imap\IMAPTransport |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( $server, $username, $password, $mailbox = null, array $flags = [] ) { |
|
19
|
|
|
$this->server = $server; |
|
20
|
|
|
$this->username = $username; |
|
21
|
|
|
$this->password = $password; |
|
22
|
|
|
$this->mailbox = $mailbox ?: 'INBOX'; |
|
23
|
|
|
$this->flags = $flags; |
|
24
|
|
|
|
|
25
|
|
|
$this->connect(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function connect() { |
|
29
|
|
|
if ( !$this->imap ) { |
|
30
|
|
|
$this->imap = $this->createTransport()->open( |
|
31
|
|
|
$this->server, |
|
32
|
|
|
$this->username, |
|
33
|
|
|
$this->password, |
|
34
|
|
|
$this->mailbox, |
|
35
|
|
|
$this->flags |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $this->imap; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function imap() { |
|
43
|
|
|
return $this->imap; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function createTransport() { |
|
47
|
|
|
return new IMAPTransport; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function createMessage( $msgNum, $unseen = null ) { |
|
51
|
|
|
return new IMAPMessage($this, $msgNum, $unseen); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function headers( $newestFirst = true ) { |
|
55
|
|
|
$this->connect(); |
|
56
|
|
|
|
|
57
|
|
|
$headers = $this->imap()->headers(); |
|
58
|
|
|
|
|
59
|
|
|
if ( $newestFirst ) { |
|
60
|
|
|
$headers = array_reverse($headers); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $headers; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function message( $msgNum ) { |
|
67
|
|
|
$this->connect(); |
|
68
|
|
|
|
|
69
|
|
|
return $this->createMessage($msgNum); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function messages( array $options = [] ) { |
|
73
|
|
|
$options += [ |
|
74
|
|
|
'offset' => 0, |
|
75
|
|
|
'limit' => 0, |
|
76
|
|
|
'seen' => null, |
|
77
|
|
|
'newestFirst' => true, |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
$headers = $this->headers($options['newestFirst']); |
|
81
|
|
|
|
|
82
|
|
|
$messages = []; |
|
83
|
|
|
$eligibles = 0; |
|
84
|
|
|
foreach ( $headers AS $n => $header ) { |
|
85
|
|
|
if ( preg_match('/([UN]?)\s+(\d+)\)/', $header, $match) ) { |
|
86
|
|
|
$unseen = (bool) trim($match[1]); |
|
87
|
|
|
$msgNum = (int) $match[2]; |
|
88
|
|
|
|
|
89
|
|
|
$eligible = $options['seen'] === null || $unseen != $options['seen']; |
|
90
|
|
|
if ( $eligible ) { |
|
91
|
|
|
$eligibles++; |
|
92
|
|
|
|
|
93
|
|
|
if ( $eligibles > $options['offset'] ) { |
|
94
|
|
|
$messages[] = $this->createMessage($msgNum, $unseen); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ( $options['limit'] && isset($messages[$options['limit']-1]) ) { |
|
99
|
|
|
break; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $messages; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getTextSubtypes() { |
|
108
|
|
|
return ['PLAIN']; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function getHtmlSubtypes() { |
|
112
|
|
|
return ['HTML']; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function msgInfo() { |
|
116
|
|
|
return $this->imap()->mailboxmsginfo(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function vacuum() { |
|
120
|
|
|
return $this->imap()->expunge(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|