|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ddeboer\Imap; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* An IMAP mailbox (commonly referred to as a ‘folder’) |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class Mailbox implements \Countable, \IteratorAggregate |
|
10
|
|
|
{ |
|
11
|
|
|
private $mailbox; |
|
12
|
|
|
private $name; |
|
13
|
|
|
private $connection; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Constructor |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $name Mailbox name |
|
19
|
|
|
* @param Connection $connection IMAP connection |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($name, Connection $connection) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->mailbox = $name; |
|
24
|
|
|
$this->connection = $connection; |
|
25
|
|
|
$this->name = substr($name, strpos($name, '}')+1); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get mailbox name |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getName() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->name; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get number of messages in this mailbox |
|
40
|
|
|
* |
|
41
|
|
|
* @return int |
|
42
|
|
|
*/ |
|
43
|
|
|
public function count() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->init(); |
|
46
|
|
|
|
|
47
|
|
|
return imap_num_msg($this->connection->getResource()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get message ids |
|
52
|
|
|
* |
|
53
|
|
|
* @param SearchExpression $search Search expression (optional) |
|
54
|
|
|
* |
|
55
|
|
|
* @return MessageIterator|Message[] |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getMessages(SearchExpression $search = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->init(); |
|
60
|
|
|
|
|
61
|
|
|
$query = ($search ? (string) $search : 'ALL'); |
|
62
|
|
|
|
|
63
|
|
|
$messageNumbers = imap_search($this->connection->getResource(), $query, \SE_UID); |
|
64
|
|
|
if (false == $messageNumbers) { |
|
65
|
|
|
// imap_search can also return false |
|
66
|
|
|
$messageNumbers = array(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return new MessageIterator($this->connection->getResource(), $messageNumbers); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get a message by message number |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $number Message number |
|
76
|
|
|
* |
|
77
|
|
|
* @return Message |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getMessage($number) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->init(); |
|
82
|
|
|
|
|
83
|
|
|
return new Message($this->connection->getResource(), $number); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get messages in this mailbox |
|
88
|
|
|
* |
|
89
|
|
|
* @return MessageIterator |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getIterator() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->init(); |
|
94
|
|
|
|
|
95
|
|
|
return $this->getMessages(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Delete this mailbox |
|
100
|
|
|
* |
|
101
|
|
|
*/ |
|
102
|
|
|
public function delete() |
|
103
|
|
|
{ |
|
104
|
|
|
$this->connection->deleteMailbox($this); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Delete all messages marked for deletion |
|
109
|
|
|
* |
|
110
|
|
|
* @return Mailbox |
|
111
|
|
|
*/ |
|
112
|
|
|
public function expunge() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->init(); |
|
115
|
|
|
|
|
116
|
|
|
imap_expunge($this->connection->getResource()); |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Add a message to the mailbox |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $message |
|
125
|
|
|
* |
|
126
|
|
|
* @return boolean |
|
127
|
|
|
*/ |
|
128
|
|
|
public function addMessage($message) |
|
129
|
|
|
{ |
|
130
|
|
|
return imap_append($this->connection->getResource(), $this->mailbox, $message); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* If connection is not currently in this mailbox, switch it to this mailbox |
|
135
|
|
|
*/ |
|
136
|
|
|
private function init() |
|
137
|
|
|
{ |
|
138
|
|
|
$check = imap_check($this->connection->getResource()); |
|
139
|
|
|
if ($check === false || $check->Mailbox != $this->mailbox) { |
|
140
|
|
|
imap_reopen($this->connection->getResource(), $this->mailbox); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: