|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* File: Client.php |
|
4
|
|
|
* Category: - |
|
5
|
|
|
* Author: M. Goldenbaum |
|
6
|
|
|
* Created: 19.01.17 22:21 |
|
7
|
|
|
* Updated: - |
|
8
|
|
|
* |
|
9
|
|
|
* Description: |
|
10
|
|
|
* - |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Webklex\IMAP; |
|
14
|
|
|
|
|
15
|
|
|
use Webklex\IMAP\Exceptions\ConnectionFailedException; |
|
16
|
|
|
use Webklex\IMAP\Exceptions\GetMessagesFailedException; |
|
17
|
|
|
use Webklex\IMAP\Exceptions\InvalidImapTimeoutTypeException; |
|
18
|
|
|
use Webklex\IMAP\Exceptions\MailboxFetchingException; |
|
19
|
|
|
use Webklex\IMAP\Exceptions\MaskNotFoundException; |
|
20
|
|
|
use Webklex\IMAP\Exceptions\MessageSearchValidationException; |
|
21
|
|
|
use Webklex\IMAP\Support\FolderCollection; |
|
22
|
|
|
use Webklex\IMAP\Support\Masks\AttachmentMask; |
|
23
|
|
|
use Webklex\IMAP\Support\Masks\MessageMask; |
|
24
|
|
|
use Webklex\IMAP\Support\MessageCollection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Client |
|
28
|
|
|
* |
|
29
|
|
|
* @package Webklex\IMAP |
|
30
|
|
|
*/ |
|
31
|
|
|
class Client { |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var boolean|resource |
|
35
|
|
|
*/ |
|
36
|
|
|
public $connection = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Server hostname. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
public $host; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Server port. |
|
47
|
|
|
* |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
public $port; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Service protocol. |
|
54
|
|
|
* |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
public $protocol; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Server encryption. |
|
61
|
|
|
* Supported: none, ssl, tls, or notls. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
public $encryption; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* If server has to validate cert. |
|
69
|
|
|
* |
|
70
|
|
|
* @var mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public $validate_cert; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Account username/ |
|
76
|
|
|
* |
|
77
|
|
|
* @var mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public $username; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Account password. |
|
83
|
|
|
* |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
public $password; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Read only parameter. |
|
90
|
|
|
* |
|
91
|
|
|
* @var bool |
|
92
|
|
|
*/ |
|
93
|
|
|
protected $read_only = false; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Active folder. |
|
97
|
|
|
* |
|
98
|
|
|
* @var Folder |
|
99
|
|
|
*/ |
|
100
|
|
|
protected $active_folder = false; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Connected parameter |
|
104
|
|
|
* |
|
105
|
|
|
* @var bool |
|
106
|
|
|
*/ |
|
107
|
|
|
protected $connected = false; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* IMAP errors that might have ben occurred |
|
111
|
|
|
* |
|
112
|
|
|
* @var array $errors |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $errors = []; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* All valid and available account config parameters |
|
118
|
|
|
* |
|
119
|
|
|
* @var array $validConfigKeys |
|
120
|
|
|
*/ |
|
121
|
|
|
protected $valid_config_keys = ['host', 'port', 'encryption', 'validate_cert', 'username', 'password', 'protocol']; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @var string $default_message_mask |
|
125
|
|
|
*/ |
|
126
|
|
|
protected $default_message_mask = MessageMask::class; |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @var string $default_attachment_mask |
|
130
|
|
|
*/ |
|
131
|
|
|
protected $default_attachment_mask = AttachmentMask::class; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Client constructor. |
|
135
|
|
|
* @param array $config |
|
136
|
|
|
* |
|
137
|
|
|
* @throws MaskNotFoundException |
|
138
|
|
|
*/ |
|
139
|
|
|
public function __construct($config = []) { |
|
140
|
|
|
$this->setConfig($config); |
|
141
|
|
|
$this->setMaskFromConfig($config); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Client destructor |
|
146
|
|
|
*/ |
|
147
|
|
|
public function __destruct() { |
|
148
|
|
|
$this->disconnect(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Set the Client configuration |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $config |
|
155
|
|
|
* |
|
156
|
|
|
* @return self |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setConfig(array $config) { |
|
159
|
|
|
$default_account = config('imap.default'); |
|
160
|
|
|
$default_config = config("imap.accounts.$default_account"); |
|
161
|
|
|
|
|
162
|
|
|
foreach ($this->valid_config_keys as $key) { |
|
163
|
|
|
$this->$key = isset($config[$key]) ? $config[$key] : $default_config[$key]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Look for a possible mask in any available config |
|
171
|
|
|
* @param $config |
|
172
|
|
|
* |
|
173
|
|
|
* @throws MaskNotFoundException |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function setMaskFromConfig($config) { |
|
176
|
|
|
$default_config = config("imap.masks"); |
|
177
|
|
|
|
|
178
|
|
|
if(isset($config['masks'])){ |
|
179
|
|
|
if(isset($config['masks']['message'])) { |
|
180
|
|
|
if(class_exists($config['masks']['message'])) { |
|
181
|
|
|
$this->default_message_mask = $config['masks']['message']; |
|
182
|
|
|
}else{ |
|
183
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['message']); |
|
184
|
|
|
} |
|
185
|
|
|
}else{ |
|
186
|
|
|
if(class_exists($default_config['message'])) { |
|
187
|
|
|
$this->default_message_mask = $default_config['message']; |
|
188
|
|
|
}else{ |
|
189
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
if(isset($config['masks']['attachment'])) { |
|
193
|
|
|
if(class_exists($config['masks']['attachment'])) { |
|
194
|
|
|
$this->default_message_mask = $config['masks']['attachment']; |
|
195
|
|
|
}else{ |
|
196
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$config['masks']['attachment']); |
|
197
|
|
|
} |
|
198
|
|
|
}else{ |
|
199
|
|
|
if(class_exists($default_config['attachment'])) { |
|
200
|
|
|
$this->default_message_mask = $default_config['attachment']; |
|
201
|
|
|
}else{ |
|
202
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
}else{ |
|
206
|
|
|
if(class_exists($default_config['message'])) { |
|
207
|
|
|
$this->default_message_mask = $default_config['message']; |
|
208
|
|
|
}else{ |
|
209
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['message']); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
if(class_exists($default_config['attachment'])) { |
|
213
|
|
|
$this->default_message_mask = $default_config['attachment']; |
|
214
|
|
|
}else{ |
|
215
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$default_config['attachment']); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Get the current imap resource |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool|resource |
|
225
|
|
|
* @throws ConnectionFailedException |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getConnection() { |
|
228
|
|
|
$this->checkConnection(); |
|
229
|
|
|
return $this->connection; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Set read only property and reconnect if it's necessary. |
|
234
|
|
|
* |
|
235
|
|
|
* @param bool $read_only |
|
236
|
|
|
* |
|
237
|
|
|
* @return self |
|
238
|
|
|
*/ |
|
239
|
|
|
public function setReadOnly($read_only = true) { |
|
240
|
|
|
$this->read_only = $read_only; |
|
241
|
|
|
|
|
242
|
|
|
return $this; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Determine if connection was established. |
|
247
|
|
|
* |
|
248
|
|
|
* @return bool |
|
249
|
|
|
*/ |
|
250
|
|
|
public function isConnected() { |
|
251
|
|
|
return $this->connected; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Determine if connection is in read only mode. |
|
256
|
|
|
* |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
|
|
public function isReadOnly() { |
|
260
|
|
|
return $this->read_only; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Determine if connection was established and connect if not. |
|
265
|
|
|
* |
|
266
|
|
|
* @throws ConnectionFailedException |
|
267
|
|
|
*/ |
|
268
|
|
|
public function checkConnection() { |
|
269
|
|
|
if (!$this->isConnected() || $this->connection === false) { |
|
270
|
|
|
$this->connect(); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Connect to server. |
|
276
|
|
|
* |
|
277
|
|
|
* @param int $attempts |
|
278
|
|
|
* |
|
279
|
|
|
* @return $this |
|
280
|
|
|
* @throws ConnectionFailedException |
|
281
|
|
|
*/ |
|
282
|
|
|
public function connect($attempts = 3) { |
|
283
|
|
|
$this->disconnect(); |
|
284
|
|
|
|
|
285
|
|
|
try { |
|
286
|
|
|
$this->connection = \imap_open( |
|
287
|
|
|
$this->getAddress(), |
|
288
|
|
|
$this->username, |
|
289
|
|
|
$this->password, |
|
290
|
|
|
$this->getOptions(), |
|
291
|
|
|
$attempts, |
|
292
|
|
|
config('imap.options.open') |
|
293
|
|
|
); |
|
294
|
|
|
$this->connected = !!$this->connection; |
|
295
|
|
|
} catch (\ErrorException $e) { |
|
296
|
|
|
$errors = \imap_errors(); |
|
297
|
|
|
$message = $e->getMessage().'. '.implode("; ", (is_array($errors) ? $errors : array())); |
|
|
|
|
|
|
298
|
|
|
|
|
299
|
|
|
throw new ConnectionFailedException($message); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return $this; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Disconnect from server. |
|
307
|
|
|
* |
|
308
|
|
|
* @return $this |
|
309
|
|
|
*/ |
|
310
|
|
|
public function disconnect() { |
|
311
|
|
|
if ($this->isConnected() && $this->connection !== false && is_integer($this->connection) === false) { |
|
312
|
|
|
$this->errors = array_merge($this->errors, \imap_errors() ?: []); |
|
313
|
|
|
$this->connected = !\imap_close($this->connection, IMAP::CL_EXPUNGE); |
|
|
|
|
|
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
return $this; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Get a folder instance by a folder name |
|
321
|
|
|
* --------------------------------------------- |
|
322
|
|
|
* PLEASE NOTE: This is an experimental function |
|
323
|
|
|
* --------------------------------------------- |
|
324
|
|
|
* @param string $folder_name |
|
325
|
|
|
* @param int $attributes |
|
326
|
|
|
* @param null|string $delimiter |
|
327
|
|
|
* @param boolean $prefix_address |
|
328
|
|
|
* |
|
329
|
|
|
* @return Folder |
|
330
|
|
|
*/ |
|
331
|
|
|
public function getFolder($folder_name, $attributes = 32, $delimiter = null, $prefix_address = true) { |
|
332
|
|
|
|
|
333
|
|
|
$delimiter = $delimiter === null ? config('imap.options.delimiter', '/') : $delimiter; |
|
334
|
|
|
|
|
335
|
|
|
$folder_name = $prefix_address ? $this->getAddress().$folder_name : $folder_name; |
|
336
|
|
|
|
|
337
|
|
|
$oFolder = new Folder($this, (object) [ |
|
338
|
|
|
'name' => $folder_name, |
|
339
|
|
|
'attributes' => $attributes, |
|
340
|
|
|
'delimiter' => $delimiter |
|
341
|
|
|
]); |
|
342
|
|
|
|
|
343
|
|
|
return $oFolder; |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* Get folders list. |
|
348
|
|
|
* If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
|
349
|
|
|
* |
|
350
|
|
|
* @param boolean $hierarchical |
|
351
|
|
|
* @param string|null $parent_folder |
|
352
|
|
|
* |
|
353
|
|
|
* @return FolderCollection |
|
354
|
|
|
* @throws ConnectionFailedException |
|
355
|
|
|
* @throws MailboxFetchingException |
|
356
|
|
|
*/ |
|
357
|
|
|
public function getFolders($hierarchical = true, $parent_folder = null) { |
|
358
|
|
|
$this->checkConnection(); |
|
359
|
|
|
$folders = FolderCollection::make([]); |
|
360
|
|
|
|
|
361
|
|
|
$pattern = $parent_folder.($hierarchical ? '%' : '*'); |
|
362
|
|
|
|
|
363
|
|
|
$items = \imap_getmailboxes($this->connection, $this->getAddress(), $pattern); |
|
|
|
|
|
|
364
|
|
|
if(is_array($items)){ |
|
|
|
|
|
|
365
|
|
|
foreach ($items as $item) { |
|
366
|
|
|
$folder = new Folder($this, $item); |
|
367
|
|
|
|
|
368
|
|
|
if ($hierarchical && $folder->hasChildren()) { |
|
369
|
|
|
$pattern = $folder->full_name.$folder->delimiter.'%'; |
|
370
|
|
|
|
|
371
|
|
|
$children = $this->getFolders(true, $pattern); |
|
372
|
|
|
$folder->setChildren($children); |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
$folders->push($folder); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
return $folders; |
|
379
|
|
|
}else{ |
|
380
|
|
|
throw new MailboxFetchingException($this->getLastError()); |
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* Open folder. |
|
386
|
|
|
* |
|
387
|
|
|
* @param string|Folder $folder_path |
|
388
|
|
|
* @param int $attempts |
|
389
|
|
|
* |
|
390
|
|
|
* @throws ConnectionFailedException |
|
391
|
|
|
*/ |
|
392
|
|
|
public function openFolder($folder_path, $attempts = 3) { |
|
393
|
|
|
$this->checkConnection(); |
|
394
|
|
|
|
|
395
|
|
|
if(property_exists($folder_path, 'path')) { |
|
396
|
|
|
$folder_path = $folder_path->path; |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
if ($this->active_folder !== $folder_path) { |
|
400
|
|
|
$this->active_folder = $folder_path; |
|
|
|
|
|
|
401
|
|
|
|
|
402
|
|
|
\imap_reopen($this->getConnection(), $folder_path, $this->getOptions(), $attempts); |
|
|
|
|
|
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Create a new Folder |
|
408
|
|
|
* @param string $name |
|
409
|
|
|
* @param boolean $expunge |
|
410
|
|
|
* |
|
411
|
|
|
* @return bool |
|
412
|
|
|
* @throws ConnectionFailedException |
|
413
|
|
|
*/ |
|
414
|
|
|
public function createFolder($name, $expunge = true) { |
|
415
|
|
|
$this->checkConnection(); |
|
416
|
|
|
$status = \imap_createmailbox($this->getConnection(), $this->getAddress() . \imap_utf7_encode($name)); |
|
|
|
|
|
|
417
|
|
|
if($expunge) $this->expunge(); |
|
418
|
|
|
|
|
419
|
|
|
return $status; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Rename Folder |
|
424
|
|
|
* @param string $old_name |
|
425
|
|
|
* @param string $new_name |
|
426
|
|
|
* @param boolean $expunge |
|
427
|
|
|
* |
|
428
|
|
|
* @return bool |
|
429
|
|
|
* @throws ConnectionFailedException |
|
430
|
|
|
*/ |
|
431
|
|
|
public function renameFolder($old_name, $new_name, $expunge = true) { |
|
432
|
|
|
$this->checkConnection(); |
|
433
|
|
|
$status = \imap_renamemailbox($this->getConnection(), $this->getAddress() . \imap_utf7_encode($old_name), $this->getAddress() . \imap_utf7_encode($new_name)); |
|
|
|
|
|
|
434
|
|
|
if($expunge) $this->expunge(); |
|
435
|
|
|
|
|
436
|
|
|
return $status; |
|
437
|
|
|
} |
|
438
|
|
|
|
|
439
|
|
|
/** |
|
440
|
|
|
* Delete Folder |
|
441
|
|
|
* @param string $name |
|
442
|
|
|
* @param boolean $expunge |
|
443
|
|
|
* |
|
444
|
|
|
* @return bool |
|
445
|
|
|
* @throws ConnectionFailedException |
|
446
|
|
|
*/ |
|
447
|
|
|
public function deleteFolder($name, $expunge = true) { |
|
448
|
|
|
$this->checkConnection(); |
|
449
|
|
|
$status = \imap_deletemailbox($this->getConnection(), $this->getAddress() . \imap_utf7_encode($name)); |
|
|
|
|
|
|
450
|
|
|
if($expunge) $this->expunge(); |
|
451
|
|
|
|
|
452
|
|
|
return $status; |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
/** |
|
456
|
|
|
* Get messages from folder. |
|
457
|
|
|
* |
|
458
|
|
|
* @param Folder $folder |
|
459
|
|
|
* @param string $criteria |
|
460
|
|
|
* @param int|null $fetch_options |
|
461
|
|
|
* @param boolean $fetch_body |
|
462
|
|
|
* @param boolean $fetch_attachment |
|
463
|
|
|
* @param boolean $fetch_flags |
|
464
|
|
|
* |
|
465
|
|
|
* @return MessageCollection |
|
466
|
|
|
* @throws ConnectionFailedException |
|
467
|
|
|
* @throws Exceptions\InvalidWhereQueryCriteriaException |
|
468
|
|
|
* @throws GetMessagesFailedException |
|
469
|
|
|
* |
|
470
|
|
|
* @deprecated 1.0.5.2:2.0.0 No longer needed. Use Folder::getMessages() instead |
|
471
|
|
|
* @see Folder::getMessages() |
|
472
|
|
|
*/ |
|
473
|
|
|
public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $fetch_body = null, $fetch_attachment = null, $fetch_flags = null) { |
|
474
|
|
|
return $folder->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* Get all unseen messages from folder |
|
479
|
|
|
* |
|
480
|
|
|
* @param Folder $folder |
|
481
|
|
|
* @param string $criteria |
|
482
|
|
|
* @param int|null $fetch_options |
|
483
|
|
|
* @param boolean $fetch_body |
|
484
|
|
|
* @param boolean $fetch_attachment |
|
485
|
|
|
* @param boolean $fetch_flags |
|
486
|
|
|
* |
|
487
|
|
|
* @return MessageCollection |
|
488
|
|
|
* @throws ConnectionFailedException |
|
489
|
|
|
* @throws Exceptions\InvalidWhereQueryCriteriaException |
|
490
|
|
|
* @throws GetMessagesFailedException |
|
491
|
|
|
* @throws MessageSearchValidationException |
|
492
|
|
|
* |
|
493
|
|
|
* @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead |
|
494
|
|
|
* @see Folder::getMessages() |
|
495
|
|
|
*/ |
|
496
|
|
|
public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true, $fetch_flags = false) { |
|
497
|
|
|
return $folder->getUnseenMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment, $fetch_flags); |
|
|
|
|
|
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Search messages by a given search criteria |
|
502
|
|
|
* |
|
503
|
|
|
* @param array $where |
|
504
|
|
|
* @param Folder $folder |
|
505
|
|
|
* @param int|null $fetch_options |
|
506
|
|
|
* @param boolean $fetch_body |
|
507
|
|
|
* @param string $charset |
|
508
|
|
|
* @param boolean $fetch_attachment |
|
509
|
|
|
* @param boolean $fetch_flags |
|
510
|
|
|
* |
|
511
|
|
|
* @return MessageCollection |
|
512
|
|
|
* @throws ConnectionFailedException |
|
513
|
|
|
* @throws Exceptions\InvalidWhereQueryCriteriaException |
|
514
|
|
|
* @throws GetMessagesFailedException |
|
515
|
|
|
* |
|
516
|
|
|
* @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::searchMessages() instead |
|
517
|
|
|
* @see Folder::searchMessages() |
|
518
|
|
|
* |
|
519
|
|
|
*/ |
|
520
|
|
|
public function searchMessages(array $where, Folder $folder, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true, $fetch_flags = false) { |
|
521
|
|
|
return $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment, $fetch_flags); |
|
|
|
|
|
|
522
|
|
|
} |
|
523
|
|
|
|
|
524
|
|
|
/** |
|
525
|
|
|
* Get option for \imap_open and \imap_reopen. |
|
526
|
|
|
* It supports only isReadOnly feature. |
|
527
|
|
|
* |
|
528
|
|
|
* @return int |
|
529
|
|
|
*/ |
|
530
|
|
|
protected function getOptions() { |
|
531
|
|
|
return ($this->isReadOnly()) ? IMAP::OP_READONLY : 0; |
|
532
|
|
|
} |
|
533
|
|
|
|
|
534
|
|
|
/** |
|
535
|
|
|
* Get full address of mailbox. |
|
536
|
|
|
* |
|
537
|
|
|
* @return string |
|
538
|
|
|
*/ |
|
539
|
|
|
protected function getAddress() { |
|
540
|
|
|
$address = "{".$this->host.":".$this->port."/".($this->protocol ? $this->protocol : 'imap'); |
|
541
|
|
|
if (!$this->validate_cert) { |
|
542
|
|
|
$address .= '/novalidate-cert'; |
|
543
|
|
|
} |
|
544
|
|
|
if (in_array($this->encryption,['tls', 'notls', 'ssl'])) { |
|
545
|
|
|
$address .= '/'.$this->encryption; |
|
546
|
|
|
} elseif ($this->encryption === "starttls") { |
|
547
|
|
|
$address .= '/tls'; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
$address .= '}'; |
|
551
|
|
|
|
|
552
|
|
|
return $address; |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
/** |
|
556
|
|
|
* Retrieve the quota level settings, and usage statics per mailbox |
|
557
|
|
|
* |
|
558
|
|
|
* @return array |
|
559
|
|
|
* @throws ConnectionFailedException |
|
560
|
|
|
*/ |
|
561
|
|
|
public function getQuota() { |
|
562
|
|
|
$this->checkConnection(); |
|
563
|
|
|
return \imap_get_quota($this->getConnection(), 'user.'.$this->username); |
|
|
|
|
|
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
/** |
|
567
|
|
|
* Retrieve the quota settings per user |
|
568
|
|
|
* |
|
569
|
|
|
* @param string $quota_root |
|
570
|
|
|
* |
|
571
|
|
|
* @return array |
|
572
|
|
|
* @throws ConnectionFailedException |
|
573
|
|
|
*/ |
|
574
|
|
|
public function getQuotaRoot($quota_root = 'INBOX') { |
|
575
|
|
|
$this->checkConnection(); |
|
576
|
|
|
return \imap_get_quotaroot($this->getConnection(), $quota_root); |
|
|
|
|
|
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
|
|
/** |
|
580
|
|
|
* Gets the number of messages in the current mailbox |
|
581
|
|
|
* |
|
582
|
|
|
* @return int |
|
583
|
|
|
* @throws ConnectionFailedException |
|
584
|
|
|
*/ |
|
585
|
|
|
public function countMessages() { |
|
586
|
|
|
$this->checkConnection(); |
|
587
|
|
|
return \imap_num_msg($this->connection); |
|
|
|
|
|
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
/** |
|
591
|
|
|
* Gets the number of recent messages in current mailbox |
|
592
|
|
|
* |
|
593
|
|
|
* @return int |
|
594
|
|
|
* @throws ConnectionFailedException |
|
595
|
|
|
*/ |
|
596
|
|
|
public function countRecentMessages() { |
|
597
|
|
|
$this->checkConnection(); |
|
598
|
|
|
return \imap_num_recent($this->connection); |
|
|
|
|
|
|
599
|
|
|
} |
|
600
|
|
|
|
|
601
|
|
|
/** |
|
602
|
|
|
* Read an overview of the information in the headers of a given message or sequence |
|
603
|
|
|
* @param string $sequence |
|
604
|
|
|
* @param int $option |
|
605
|
|
|
* |
|
606
|
|
|
* @return \Illuminate\Support\Collection |
|
607
|
|
|
* @throws ConnectionFailedException |
|
608
|
|
|
*/ |
|
609
|
|
|
public function overview($sequence = "1:*", $option = IMAP::NIL) { |
|
610
|
|
|
$this->checkConnection(); |
|
611
|
|
|
return collect(\imap_fetch_overview($this->connection, $sequence, $option)); |
|
|
|
|
|
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
|
|
/** |
|
615
|
|
|
* Returns all IMAP alert messages that have occurred |
|
616
|
|
|
* |
|
617
|
|
|
* @return array |
|
618
|
|
|
*/ |
|
619
|
|
|
public function getAlerts() { |
|
620
|
|
|
return \imap_alerts(); |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Returns all of the IMAP errors that have occurred |
|
625
|
|
|
* |
|
626
|
|
|
* @return array |
|
627
|
|
|
*/ |
|
628
|
|
|
public function getErrors() { |
|
629
|
|
|
$this->errors = array_merge($this->errors, \imap_errors() ?: []); |
|
630
|
|
|
|
|
631
|
|
|
return $this->errors; |
|
632
|
|
|
} |
|
633
|
|
|
|
|
634
|
|
|
/** |
|
635
|
|
|
* Gets the last IMAP error that occurred during this page request |
|
636
|
|
|
* |
|
637
|
|
|
* @return string |
|
638
|
|
|
*/ |
|
639
|
|
|
public function getLastError() { |
|
640
|
|
|
return \imap_last_error(); |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
/** |
|
644
|
|
|
* Delete all messages marked for deletion |
|
645
|
|
|
* |
|
646
|
|
|
* @return bool |
|
647
|
|
|
* @throws ConnectionFailedException |
|
648
|
|
|
*/ |
|
649
|
|
|
public function expunge() { |
|
650
|
|
|
$this->checkConnection(); |
|
651
|
|
|
return \imap_expunge($this->connection); |
|
|
|
|
|
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
|
|
/** |
|
655
|
|
|
* Check current mailbox |
|
656
|
|
|
* |
|
657
|
|
|
* @return object { |
|
658
|
|
|
* Date [string(37) "Wed, 8 Mar 2017 22:17:54 +0100 (CET)"] current system time formatted according to » RFC2822 |
|
659
|
|
|
* Driver [string(4) "imap"] protocol used to access this mailbox: POP3, IMAP, NNTP |
|
660
|
|
|
* Mailbox ["{[email protected]:993/imap/user="[email protected]"}INBOX"] the mailbox name |
|
661
|
|
|
* Nmsgs [int(1)] number of messages in the mailbox |
|
662
|
|
|
* Recent [int(0)] number of recent messages in the mailbox |
|
663
|
|
|
* } |
|
664
|
|
|
* @throws ConnectionFailedException |
|
665
|
|
|
*/ |
|
666
|
|
|
public function checkCurrentMailbox() { |
|
667
|
|
|
$this->checkConnection(); |
|
668
|
|
|
return \imap_check($this->connection); |
|
|
|
|
|
|
669
|
|
|
} |
|
670
|
|
|
|
|
671
|
|
|
/** |
|
672
|
|
|
* Set the imap timeout for a given operation type |
|
673
|
|
|
* @param $type |
|
674
|
|
|
* @param $timeout |
|
675
|
|
|
* |
|
676
|
|
|
* @return mixed |
|
677
|
|
|
* @throws InvalidImapTimeoutTypeException |
|
678
|
|
|
*/ |
|
679
|
|
|
public function setTimeout($type, $timeout) { |
|
680
|
|
|
if(0 <= $type && $type <= 4) { |
|
681
|
|
|
return \imap_timeout($type, $timeout); |
|
682
|
|
|
} |
|
683
|
|
|
|
|
684
|
|
|
throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided."); |
|
685
|
|
|
} |
|
686
|
|
|
|
|
687
|
|
|
/** |
|
688
|
|
|
* Get the timeout for a certain operation |
|
689
|
|
|
* @param $type |
|
690
|
|
|
* |
|
691
|
|
|
* @return mixed |
|
692
|
|
|
* @throws InvalidImapTimeoutTypeException |
|
693
|
|
|
*/ |
|
694
|
|
|
public function getTimeout($type){ |
|
695
|
|
|
if(0 <= $type && $type <= 4) { |
|
696
|
|
|
return \imap_timeout($type); |
|
697
|
|
|
} |
|
698
|
|
|
|
|
699
|
|
|
throw new InvalidImapTimeoutTypeException("Invalid imap timeout type provided."); |
|
700
|
|
|
} |
|
701
|
|
|
|
|
702
|
|
|
/** |
|
703
|
|
|
* @return string |
|
704
|
|
|
*/ |
|
705
|
|
|
public function getDefaultMessageMask(){ |
|
706
|
|
|
return $this->default_message_mask; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* @param $mask |
|
711
|
|
|
* |
|
712
|
|
|
* @return $this |
|
713
|
|
|
* @throws MaskNotFoundException |
|
714
|
|
|
*/ |
|
715
|
|
|
public function setDefaultMessageMask($mask) { |
|
716
|
|
|
if(class_exists($mask)) { |
|
717
|
|
|
$this->default_message_mask = $mask; |
|
718
|
|
|
|
|
719
|
|
|
return $this; |
|
720
|
|
|
} |
|
721
|
|
|
|
|
722
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$mask); |
|
723
|
|
|
} |
|
724
|
|
|
|
|
725
|
|
|
/** |
|
726
|
|
|
* @return string |
|
727
|
|
|
*/ |
|
728
|
|
|
public function getDefaultAttachmentMask(){ |
|
729
|
|
|
return $this->default_attachment_mask; |
|
730
|
|
|
} |
|
731
|
|
|
|
|
732
|
|
|
/** |
|
733
|
|
|
* @param $mask |
|
734
|
|
|
* |
|
735
|
|
|
* @return $this |
|
736
|
|
|
* @throws MaskNotFoundException |
|
737
|
|
|
*/ |
|
738
|
|
|
public function setDefaultAttachmentMask($mask) { |
|
739
|
|
|
if(class_exists($mask)) { |
|
740
|
|
|
$this->default_attachment_mask = $mask; |
|
741
|
|
|
|
|
742
|
|
|
return $this; |
|
743
|
|
|
} |
|
744
|
|
|
|
|
745
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$mask); |
|
746
|
|
|
} |
|
747
|
|
|
|
|
748
|
|
|
/** |
|
749
|
|
|
* Get the current active folder |
|
750
|
|
|
* |
|
751
|
|
|
* @return Folder |
|
752
|
|
|
*/ |
|
753
|
|
|
public function getFolderPath(){ |
|
754
|
|
|
return $this->active_folder; |
|
755
|
|
|
} |
|
756
|
|
|
} |
|
757
|
|
|
|