1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* File: Message.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 Carbon\Carbon; |
16
|
|
|
use Illuminate\Support\Str; |
17
|
|
|
use Webklex\IMAP\Events\MessageDeletedEvent; |
18
|
|
|
use Webklex\IMAP\Events\MessageMovedEvent; |
19
|
|
|
use Webklex\IMAP\Events\MessageRestoredEvent; |
20
|
|
|
use Webklex\IMAP\Exceptions\InvalidMessageDateException; |
21
|
|
|
use Webklex\IMAP\Exceptions\MaskNotFoundException; |
22
|
|
|
use Webklex\IMAP\Exceptions\MethodNotFoundException; |
23
|
|
|
use Webklex\IMAP\Support\AttachmentCollection; |
24
|
|
|
use Webklex\IMAP\Support\FlagCollection; |
25
|
|
|
use Webklex\IMAP\Support\Masks\MessageMask; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class Message |
29
|
|
|
* |
30
|
|
|
* @package Webklex\IMAP |
31
|
|
|
* |
32
|
|
|
* @property integer msglist |
33
|
|
|
* @property integer uid |
34
|
|
|
* @property integer msgn |
35
|
|
|
* @property integer priority |
36
|
|
|
* @property string subject |
37
|
|
|
* @property string message_id |
38
|
|
|
* @property string message_no |
39
|
|
|
* @property string references |
40
|
|
|
* @property carbon date |
41
|
|
|
* @property array from |
42
|
|
|
* @property array to |
43
|
|
|
* @property array cc |
44
|
|
|
* @property array bcc |
45
|
|
|
* @property array reply_to |
46
|
|
|
* @property array in_reply_to |
47
|
|
|
* @property array sender |
48
|
|
|
* |
49
|
|
|
* @method integer getMsglist() |
50
|
|
|
* @method integer setMsglist(integer $msglist) |
51
|
|
|
* @method integer getUid() |
52
|
|
|
* @method integer setUid(integer $uid) |
53
|
|
|
* @method integer getMsgn() |
54
|
|
|
* @method integer setMsgn(integer $msgn) |
55
|
|
|
* @method integer getPriority() |
56
|
|
|
* @method integer setPriority(integer $priority) |
57
|
|
|
* @method string getSubject() |
58
|
|
|
* @method string setSubject(string $subject) |
59
|
|
|
* @method string getMessageId() |
60
|
|
|
* @method string setMessageId(string $message_id) |
61
|
|
|
* @method string getMessageNo() |
62
|
|
|
* @method string setMessageNo(string $message_no) |
63
|
|
|
* @method string getReferences() |
64
|
|
|
* @method string setReferences(string $references) |
65
|
|
|
* @method carbon getDate() |
66
|
|
|
* @method carbon setDate(carbon $date) |
67
|
|
|
* @method array getFrom() |
68
|
|
|
* @method array setFrom(array $from) |
69
|
|
|
* @method array getTo() |
70
|
|
|
* @method array setTo(array $to) |
71
|
|
|
* @method array getCc() |
72
|
|
|
* @method array setCc(array $cc) |
73
|
|
|
* @method array getBcc() |
74
|
|
|
* @method array setBcc(array $bcc) |
75
|
|
|
* @method array getReplyTo() |
76
|
|
|
* @method array setReplyTo(array $reply_to) |
77
|
|
|
* @method array getInReplyTo() |
78
|
|
|
* @method array setInReplyTo(array $in_reply_to) |
79
|
|
|
* @method array getSender() |
80
|
|
|
* @method array setSender(array $sender) |
81
|
|
|
*/ |
82
|
|
|
class Message { |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Client instance |
86
|
|
|
* |
87
|
|
|
* @var Client |
88
|
|
|
*/ |
89
|
|
|
private $client = Client::class; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Default mask |
93
|
|
|
* @var string $mask |
94
|
|
|
*/ |
95
|
|
|
protected $mask = MessageMask::class; |
96
|
|
|
|
97
|
|
|
/** @var array $config */ |
98
|
|
|
protected $config = []; |
99
|
|
|
|
100
|
|
|
/** @var array $attributes */ |
101
|
|
|
protected $attributes = [ |
102
|
|
|
'message_id' => '', |
103
|
|
|
'message_no' => null, |
104
|
|
|
'subject' => '', |
105
|
|
|
'references' => null, |
106
|
|
|
'date' => null, |
107
|
|
|
'from' => [], |
108
|
|
|
'to' => [], |
109
|
|
|
'cc' => [], |
110
|
|
|
'bcc' => [], |
111
|
|
|
'reply_to' => [], |
112
|
|
|
'in_reply_to' => '', |
113
|
|
|
'sender' => [], |
114
|
|
|
'priority' => 0, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The message folder path |
119
|
|
|
* |
120
|
|
|
* @var string $folder_path |
121
|
|
|
*/ |
122
|
|
|
protected $folder_path; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Fetch body options |
126
|
|
|
* |
127
|
|
|
* @var integer |
128
|
|
|
*/ |
129
|
|
|
public $fetch_options = null; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Fetch body options |
133
|
|
|
* |
134
|
|
|
* @var bool |
135
|
|
|
*/ |
136
|
|
|
public $fetch_body = null; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Fetch attachments options |
140
|
|
|
* |
141
|
|
|
* @var bool |
142
|
|
|
*/ |
143
|
|
|
public $fetch_attachment = null; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fetch flags options |
147
|
|
|
* |
148
|
|
|
* @var bool |
149
|
|
|
*/ |
150
|
|
|
public $fetch_flags = null; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @var string $header |
154
|
|
|
*/ |
155
|
|
|
public $header = null; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @var null|object $header_info |
159
|
|
|
*/ |
160
|
|
|
public $header_info = null; |
161
|
|
|
|
162
|
|
|
/** @var null|string $raw_body */ |
163
|
|
|
public $raw_body = null; |
164
|
|
|
|
165
|
|
|
/** @var null $structure */ |
166
|
|
|
protected $structure = null; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Message body components |
170
|
|
|
* |
171
|
|
|
* @var array $bodies |
172
|
|
|
* @var AttachmentCollection|array $attachments |
173
|
|
|
* @var FlagCollection|array $flags |
174
|
|
|
*/ |
175
|
|
|
public $bodies = []; |
176
|
|
|
public $attachments = []; |
177
|
|
|
public $flags = []; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* A list of all available and supported flags |
181
|
|
|
* |
182
|
|
|
* @var array $available_flags |
183
|
|
|
*/ |
184
|
|
|
private $available_flags = ['recent', 'flagged', 'answered', 'deleted', 'seen', 'draft']; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Message constructor. |
188
|
|
|
* |
189
|
|
|
* @param integer $uid |
190
|
|
|
* @param integer|null $msglist |
191
|
|
|
* @param Client $client |
192
|
|
|
* @param integer|null $fetch_options |
193
|
|
|
* @param boolean $fetch_body |
194
|
|
|
* @param boolean $fetch_attachment |
195
|
|
|
* @param boolean $fetch_flags |
196
|
|
|
* |
197
|
|
|
* @throws Exceptions\ConnectionFailedException |
198
|
|
|
* @throws InvalidMessageDateException |
199
|
|
|
*/ |
200
|
|
|
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $fetch_body = null, $fetch_attachment = null, $fetch_flags = null) { |
201
|
|
|
|
202
|
|
|
$default_mask = $client->getDefaultMessageMask(); |
203
|
|
|
if($default_mask != null) { |
204
|
|
|
$this->mask = $default_mask; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->folder_path = $client->getFolderPath(); |
|
|
|
|
208
|
|
|
|
209
|
|
|
$this->config = config('imap.options'); |
210
|
|
|
|
211
|
|
|
$this->setFetchOption($fetch_options); |
212
|
|
|
$this->setFetchBodyOption($fetch_body); |
213
|
|
|
$this->setFetchAttachmentOption($fetch_attachment); |
214
|
|
|
$this->setFetchFlagsOption($fetch_flags); |
215
|
|
|
|
216
|
|
|
$this->attachments = AttachmentCollection::make([]); |
217
|
|
|
$this->flags = FlagCollection::make([]); |
218
|
|
|
|
219
|
|
|
$this->msglist = $msglist; |
220
|
|
|
$this->client = $client; |
221
|
|
|
|
222
|
|
|
$this->uid = ($this->fetch_options == IMAP::FT_UID) ? $uid : $uid; |
223
|
|
|
$this->msgn = ($this->fetch_options == IMAP::FT_UID) ? \imap_msgno($this->client->getConnection(), $uid) : $uid; |
|
|
|
|
224
|
|
|
|
225
|
|
|
$this->parseHeader(); |
226
|
|
|
|
227
|
|
|
if ($this->getFetchFlagsOption() === true) { |
228
|
|
|
$this->parseFlags(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if ($this->getFetchBodyOption() === true) { |
232
|
|
|
$this->parseBody(); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Call dynamic attribute setter and getter methods |
238
|
|
|
* @param string $method |
239
|
|
|
* @param array $arguments |
240
|
|
|
* |
241
|
|
|
* @return mixed |
242
|
|
|
* @throws MethodNotFoundException |
243
|
|
|
*/ |
244
|
|
|
public function __call($method, $arguments) { |
245
|
|
|
if(strtolower(substr($method, 0, 3)) === 'get') { |
246
|
|
|
$name = Str::snake(substr($method, 3)); |
247
|
|
|
|
248
|
|
|
if(in_array($name, array_keys($this->attributes))) { |
249
|
|
|
return $this->attributes[$name]; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
}elseif (strtolower(substr($method, 0, 3)) === 'set') { |
253
|
|
|
$name = Str::snake(substr($method, 3)); |
254
|
|
|
|
255
|
|
|
if(in_array($name, array_keys($this->attributes))) { |
256
|
|
|
$this->attributes[$name] = array_pop($arguments); |
257
|
|
|
|
258
|
|
|
return $this->attributes[$name]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param $name |
268
|
|
|
* @param $value |
269
|
|
|
* |
270
|
|
|
* @return mixed |
271
|
|
|
*/ |
272
|
|
|
public function __set($name, $value) { |
273
|
|
|
$this->attributes[$name] = $value; |
274
|
|
|
|
275
|
|
|
return $this->attributes[$name]; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param $name |
280
|
|
|
* |
281
|
|
|
* @return mixed|null |
282
|
|
|
*/ |
283
|
|
|
public function __get($name) { |
284
|
|
|
if(isset($this->attributes[$name])) { |
285
|
|
|
return $this->attributes[$name]; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return null; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Copy the current Messages to a mailbox |
293
|
|
|
* |
294
|
|
|
* @param $mailbox |
295
|
|
|
* @param int $options |
296
|
|
|
* |
297
|
|
|
* @return bool |
298
|
|
|
* @throws Exceptions\ConnectionFailedException |
299
|
|
|
*/ |
300
|
|
|
public function copy($mailbox, $options = 0) { |
|
|
|
|
301
|
|
|
$this->client->openFolder($this->folder_path); |
302
|
|
|
return \imap_mail_copy($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID); |
|
|
|
|
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Move the current Messages to a mailbox |
307
|
|
|
* |
308
|
|
|
* @param $mailbox |
309
|
|
|
* @param int $options |
310
|
|
|
* |
311
|
|
|
* @return bool |
312
|
|
|
* @throws Exceptions\ConnectionFailedException |
313
|
|
|
*/ |
314
|
|
|
public function move($mailbox, $options = 0) { |
|
|
|
|
315
|
|
|
$this->client->openFolder($this->folder_path); |
316
|
|
|
return \imap_mail_move($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID); |
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Check if the Message has a text body |
321
|
|
|
* |
322
|
|
|
* @return bool |
323
|
|
|
*/ |
324
|
|
|
public function hasTextBody() { |
325
|
|
|
return isset($this->bodies['text']); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Get the Message text body |
330
|
|
|
* |
331
|
|
|
* @return mixed |
332
|
|
|
*/ |
333
|
|
|
public function getTextBody() { |
334
|
|
|
if (!isset($this->bodies['text'])) { |
335
|
|
|
return false; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
return $this->bodies['text']->content; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Check if the Message has a html body |
343
|
|
|
* |
344
|
|
|
* @return bool |
345
|
|
|
*/ |
346
|
|
|
public function hasHTMLBody() { |
347
|
|
|
return isset($this->bodies['html']); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Get the Message html body |
352
|
|
|
* |
353
|
|
|
* @return string|null |
354
|
|
|
*/ |
355
|
|
|
public function getHTMLBody() { |
356
|
|
|
if (!isset($this->bodies['html'])) { |
357
|
|
|
return null; |
358
|
|
|
} |
359
|
|
|
return $this->bodies['html']->content; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Parse all defined headers |
364
|
|
|
* |
365
|
|
|
* @return void |
366
|
|
|
* @throws Exceptions\ConnectionFailedException |
367
|
|
|
* @throws InvalidMessageDateException |
368
|
|
|
*/ |
369
|
|
|
private function parseHeader() { |
370
|
|
|
$this->client->openFolder($this->folder_path); |
371
|
|
|
$this->header = $header = \imap_fetchheader($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
|
|
|
|
372
|
|
|
|
373
|
|
|
$this->priority = $this->extractPriority($this->header); |
374
|
|
|
|
375
|
|
|
if ($this->header) { |
376
|
|
|
$header = \imap_rfc822_parse_headers($this->header); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
if (property_exists($header, 'subject')) { |
380
|
|
|
if($this->config['decoder']['message']['subject'] === 'utf-8') { |
381
|
|
|
$this->subject = \imap_utf8($header->subject); |
382
|
|
|
}elseif($this->config['decoder']['message']['subject'] === 'iconv') { |
383
|
|
|
$this->subject = iconv_mime_decode($header->subject); |
384
|
|
|
}else{ |
385
|
|
|
$this->subject = mb_decode_mimeheader($header->subject); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $part){ |
390
|
|
|
$this->extractHeaderAddressPart($header, $part); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
if (property_exists($header, 'references')) { |
394
|
|
|
$this->references = $header->references; |
395
|
|
|
} |
396
|
|
|
if (property_exists($header, 'in_reply_to')) { |
397
|
|
|
$this->in_reply_to = str_replace(['<', '>'], '', $header->in_reply_to); |
398
|
|
|
} |
399
|
|
|
if (property_exists($header, 'message_id')) { |
400
|
|
|
$this->message_id = str_replace(['<', '>'], '', $header->message_id); |
401
|
|
|
} |
402
|
|
|
if (property_exists($header, 'Msgno')) { |
403
|
|
|
$messageNo = (int) trim($header->Msgno); |
404
|
|
|
$this->message_no = ($this->fetch_options == IMAP::FT_UID) ? $messageNo : \imap_msgno($this->client->getConnection(), $messageNo); |
|
|
|
|
405
|
|
|
} else { |
406
|
|
|
$this->message_no = \imap_msgno($this->client->getConnection(), $this->getUid()); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
$this->date = $this->parseDate($header); |
|
|
|
|
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Try to extract the priority from a given raw header string |
414
|
|
|
* @param string $header |
415
|
|
|
* |
416
|
|
|
* @return int|null |
417
|
|
|
*/ |
418
|
|
|
private function extractPriority($header) { |
419
|
|
|
if(preg_match('/x\-priority\:.*([0-9]{1,2})/i', $header, $priority)){ |
420
|
|
|
$priority = isset($priority[1]) ? (int) $priority[1] : 0; |
421
|
|
|
switch($priority){ |
422
|
|
|
case IMAP::MESSAGE_PRIORITY_HIGHEST; |
423
|
|
|
$priority = IMAP::MESSAGE_PRIORITY_HIGHEST; |
424
|
|
|
break; |
425
|
|
|
case IMAP::MESSAGE_PRIORITY_HIGH; |
426
|
|
|
$priority = IMAP::MESSAGE_PRIORITY_HIGH; |
427
|
|
|
break; |
428
|
|
|
case IMAP::MESSAGE_PRIORITY_NORMAL; |
429
|
|
|
$priority = IMAP::MESSAGE_PRIORITY_NORMAL; |
430
|
|
|
break; |
431
|
|
|
case IMAP::MESSAGE_PRIORITY_LOW; |
432
|
|
|
$priority = IMAP::MESSAGE_PRIORITY_LOW; |
433
|
|
|
break; |
434
|
|
|
case IMAP::MESSAGE_PRIORITY_LOWEST; |
435
|
|
|
$priority = IMAP::MESSAGE_PRIORITY_LOWEST; |
436
|
|
|
break; |
437
|
|
|
default: |
438
|
|
|
$priority = IMAP::MESSAGE_PRIORITY_UNKNOWN; |
439
|
|
|
break; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
return $priority; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* Exception handling for invalid dates |
448
|
|
|
* |
449
|
|
|
* Currently known invalid formats: |
450
|
|
|
* ^ Datetime ^ Problem ^ Cause |
451
|
|
|
* | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
452
|
|
|
* | Thu, 8 Nov 2018 08:54:58 -0200 (-02) | |
453
|
|
|
* | | and invalid timezone (max 6 char) | |
454
|
|
|
* | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
455
|
|
|
* | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown |
456
|
|
|
* | | mail server | |
457
|
|
|
* | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/ |
458
|
|
|
* |
459
|
|
|
* Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45) |
460
|
|
|
* |
461
|
|
|
* @param object $header |
462
|
|
|
* |
463
|
|
|
* @return Carbon|null |
464
|
|
|
* @throws InvalidMessageDateException |
465
|
|
|
*/ |
466
|
|
|
private function parseDate($header) { |
467
|
|
|
$parsed_date = null; |
468
|
|
|
|
469
|
|
|
if (property_exists($header, 'date')) { |
470
|
|
|
$date = $header->date; |
471
|
|
|
|
472
|
|
|
if(preg_match('/\+0580/', $date)) { |
473
|
|
|
$date = str_replace('+0580', '+0530', $date); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
$date = trim(rtrim($date)); |
477
|
|
|
try { |
478
|
|
|
$parsed_date = Carbon::parse($date); |
479
|
|
|
} catch (\Exception $e) { |
480
|
|
|
switch (true) { |
481
|
|
|
case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0: |
482
|
|
|
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0: |
483
|
|
|
$date .= 'C'; |
484
|
|
|
break; |
485
|
|
|
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ \+[0-9]{2,4}\ \(\+[0-9]{1,2}\))+$/i', $date) > 0: |
486
|
|
|
case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0: |
487
|
|
|
case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: |
488
|
|
|
case preg_match('/([A-Z]{2,3}\, \ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0: |
489
|
|
|
case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0: |
490
|
|
|
$array = explode('(', $date); |
491
|
|
|
$array = array_reverse($array); |
492
|
|
|
$date = trim(array_pop($array)); |
493
|
|
|
break; |
494
|
|
|
} |
495
|
|
|
try{ |
496
|
|
|
$parsed_date = Carbon::parse($date); |
497
|
|
|
} catch (\Exception $_e) { |
498
|
|
|
throw new InvalidMessageDateException("Invalid message date. ID:".$this->getMessageId(), 1000, $e); |
499
|
|
|
} |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
return $parsed_date; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Parse additional flags |
508
|
|
|
* |
509
|
|
|
* @return void |
510
|
|
|
* @throws Exceptions\ConnectionFailedException |
511
|
|
|
*/ |
512
|
|
|
private function parseFlags() { |
513
|
|
|
$this->flags = FlagCollection::make([]); |
514
|
|
|
|
515
|
|
|
$this->client->openFolder($this->folder_path); |
516
|
|
|
$flags = \imap_fetch_overview($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
|
|
|
|
517
|
|
|
if (is_array($flags) && isset($flags[0])) { |
518
|
|
|
foreach($this->available_flags as $flag) { |
519
|
|
|
$this->parseFlag($flags, $flag); |
520
|
|
|
} |
521
|
|
|
} |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Extract a possible flag information from a given array |
526
|
|
|
* @param array $flags |
527
|
|
|
* @param string $flag |
528
|
|
|
*/ |
529
|
|
|
private function parseFlag($flags, $flag) { |
530
|
|
|
$flag = strtolower($flag); |
531
|
|
|
|
532
|
|
|
if (property_exists($flags[0], strtoupper($flag))) { |
533
|
|
|
$this->flags->put($flag, $flags[0]->{strtoupper($flag)}); |
534
|
|
|
} elseif (property_exists($flags[0], ucfirst($flag))) { |
535
|
|
|
$this->flags->put($flag, $flags[0]->{ucfirst($flag)}); |
536
|
|
|
} elseif (property_exists($flags[0], $flag)) { |
537
|
|
|
$this->flags->put($flag, $flags[0]->$flag); |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Get the current Message header info |
543
|
|
|
* |
544
|
|
|
* @return object |
545
|
|
|
* @throws Exceptions\ConnectionFailedException |
546
|
|
|
*/ |
547
|
|
|
public function getHeaderInfo() { |
548
|
|
|
if ($this->header_info == null) { |
549
|
|
|
$this->client->openFolder($this->folder_path); |
550
|
|
|
$this->header_info = \imap_headerinfo($this->client->getConnection(), $this->getMessageNo()); |
|
|
|
|
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
return $this->header_info; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* Extract a given part as address array from a given header |
558
|
|
|
* @param object $header |
559
|
|
|
* @param string $part |
560
|
|
|
*/ |
561
|
|
|
private function extractHeaderAddressPart($header, $part) { |
562
|
|
|
if (property_exists($header, $part)) { |
563
|
|
|
$this->$part = $this->parseAddresses($header->$part); |
564
|
|
|
} |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
/** |
568
|
|
|
* Parse Addresses |
569
|
|
|
* @param $list |
570
|
|
|
* |
571
|
|
|
* @return array |
572
|
|
|
*/ |
573
|
|
|
private function parseAddresses($list) { |
574
|
|
|
$addresses = []; |
575
|
|
|
|
576
|
|
|
foreach ($list as $item) { |
577
|
|
|
$address = (object) $item; |
578
|
|
|
|
579
|
|
|
if (!property_exists($address, 'mailbox')) { |
580
|
|
|
$address->mailbox = false; |
581
|
|
|
} |
582
|
|
|
if (!property_exists($address, 'host')) { |
583
|
|
|
$address->host = false; |
584
|
|
|
} |
585
|
|
|
if (!property_exists($address, 'personal')) { |
586
|
|
|
$address->personal = false; |
587
|
|
|
} else { |
588
|
|
|
$personalParts = \imap_mime_header_decode($address->personal); |
589
|
|
|
|
590
|
|
|
if(is_array($personalParts)) { |
591
|
|
|
$address->personal = ''; |
592
|
|
|
foreach ($personalParts as $p) { |
593
|
|
|
$encoding = (property_exists($p, 'charset')) ? $p->charset : $this->getEncoding($p->text); |
594
|
|
|
$address->personal .= $this->convertEncoding($p->text, $encoding); |
595
|
|
|
} |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
|
599
|
|
|
$address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false; |
600
|
|
|
$address->full = ($address->personal) ? $address->personal.' <'.$address->mail.'>' : $address->mail; |
|
|
|
|
601
|
|
|
|
602
|
|
|
$addresses[] = $address; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
return $addresses; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Parse the Message body |
610
|
|
|
* |
611
|
|
|
* @return $this |
612
|
|
|
* @throws Exceptions\ConnectionFailedException |
613
|
|
|
*/ |
614
|
|
|
public function parseBody() { |
615
|
|
|
$this->client->openFolder($this->folder_path); |
616
|
|
|
$this->structure = \imap_fetchstructure($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
|
|
|
|
617
|
|
|
|
618
|
|
|
if(property_exists($this->structure, 'parts')){ |
619
|
|
|
$parts = $this->structure->parts; |
620
|
|
|
|
621
|
|
|
foreach ($parts as $part) { |
622
|
|
|
foreach ($part->parameters as $parameter) { |
623
|
|
|
if($parameter->attribute == "charset") { |
624
|
|
|
$encoding = $parameter->value; |
625
|
|
|
|
626
|
|
|
$encoding = preg_replace('/Content-Transfer-Encoding/', '', $encoding); |
627
|
|
|
$encoding = preg_replace('/iso-8859-8-i/', 'iso-8859-8', $encoding); |
628
|
|
|
|
629
|
|
|
$parameter->value = $encoding; |
630
|
|
|
} |
631
|
|
|
} |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
$this->fetchStructure($this->structure); |
636
|
|
|
|
637
|
|
|
return $this; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Fetch the Message structure |
642
|
|
|
* |
643
|
|
|
* @param $structure |
644
|
|
|
* @param mixed $partNumber |
645
|
|
|
* |
646
|
|
|
* @throws Exceptions\ConnectionFailedException |
647
|
|
|
*/ |
648
|
|
|
private function fetchStructure($structure, $partNumber = null) { |
649
|
|
|
$this->client->openFolder($this->folder_path); |
650
|
|
|
|
651
|
|
|
if ($structure->type == IMAP::MESSAGE_TYPE_TEXT && |
652
|
|
|
(empty($structure->disposition) || strtolower($structure->disposition) != 'attachment') |
653
|
|
|
) { |
654
|
|
|
if (strtolower($structure->subtype) == "plain" || strtolower($structure->subtype) == "csv") { |
655
|
|
|
if (!$partNumber) { |
656
|
|
|
$partNumber = 1; |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
$encoding = $this->getEncoding($structure); |
660
|
|
|
|
661
|
|
|
$content = \imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | IMAP::FT_UID); |
|
|
|
|
662
|
|
|
$content = $this->decodeString($content, $structure->encoding); |
663
|
|
|
|
664
|
|
|
// We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
665
|
|
|
// ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
666
|
|
|
// https://stackoverflow.com/a/11303410 |
667
|
|
|
// |
668
|
|
|
// us-ascii is the same as ASCII: |
669
|
|
|
// ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
670
|
|
|
// prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
671
|
|
|
// based on the typographical symbols predominantly in use there. |
672
|
|
|
// https://en.wikipedia.org/wiki/ASCII |
673
|
|
|
// |
674
|
|
|
// convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
675
|
|
|
if ($encoding != 'us-ascii') { |
676
|
|
|
$content = $this->convertEncoding($content, $encoding); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
$body = new \stdClass; |
680
|
|
|
$body->type = "text"; |
681
|
|
|
$body->content = $content; |
682
|
|
|
|
683
|
|
|
$this->bodies['text'] = $body; |
684
|
|
|
|
685
|
|
|
$this->fetchAttachment($structure, $partNumber); |
686
|
|
|
|
687
|
|
|
} elseif (strtolower($structure->subtype) == "html") { |
688
|
|
|
if (!$partNumber) { |
689
|
|
|
$partNumber = 1; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
$encoding = $this->getEncoding($structure); |
693
|
|
|
|
694
|
|
|
$content = \imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | IMAP::FT_UID); |
695
|
|
|
$content = $this->decodeString($content, $structure->encoding); |
696
|
|
|
if ($encoding != 'us-ascii') { |
697
|
|
|
$content = $this->convertEncoding($content, $encoding); |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
$body = new \stdClass; |
701
|
|
|
$body->type = "html"; |
702
|
|
|
$body->content = $content; |
703
|
|
|
|
704
|
|
|
$this->bodies['html'] = $body; |
705
|
|
|
} elseif ($structure->ifdisposition == 1 && strtolower($structure->disposition) == 'attachment') { |
706
|
|
|
if ($this->getFetchAttachmentOption() === true) { |
707
|
|
|
$this->fetchAttachment($structure, $partNumber); |
708
|
|
|
} |
709
|
|
|
} |
710
|
|
|
} elseif ($structure->type == IMAP::MESSAGE_TYPE_MULTIPART) { |
711
|
|
|
foreach ($structure->parts as $index => $subStruct) { |
712
|
|
|
$prefix = ""; |
713
|
|
|
if ($partNumber) { |
714
|
|
|
$prefix = $partNumber."."; |
715
|
|
|
} |
716
|
|
|
$this->fetchStructure($subStruct, $prefix.($index + 1)); |
717
|
|
|
} |
718
|
|
|
} else { |
719
|
|
|
if ($this->getFetchAttachmentOption() === true) { |
720
|
|
|
$this->fetchAttachment($structure, $partNumber); |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Fetch the Message attachment |
727
|
|
|
* |
728
|
|
|
* @param object $structure |
729
|
|
|
* @param mixed $partNumber |
730
|
|
|
* |
731
|
|
|
* @throws Exceptions\ConnectionFailedException |
732
|
|
|
*/ |
733
|
|
|
protected function fetchAttachment($structure, $partNumber) { |
734
|
|
|
|
735
|
|
|
$oAttachment = new Attachment($this, $structure, $partNumber); |
736
|
|
|
|
737
|
|
|
if ($oAttachment->getName() !== null) { |
|
|
|
|
738
|
|
|
if ($oAttachment->getId() !== null) { |
|
|
|
|
739
|
|
|
$this->attachments->put($oAttachment->getId(), $oAttachment); |
740
|
|
|
} else { |
741
|
|
|
$this->attachments->push($oAttachment); |
742
|
|
|
} |
743
|
|
|
} |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* Fail proof setter for $fetch_option |
748
|
|
|
* |
749
|
|
|
* @param $option |
750
|
|
|
* |
751
|
|
|
* @return $this |
752
|
|
|
*/ |
753
|
|
|
public function setFetchOption($option) { |
754
|
|
|
if (is_long($option) === true) { |
755
|
|
|
$this->fetch_options = $option; |
756
|
|
|
} elseif (is_null($option) === true) { |
757
|
|
|
$config = config('imap.options.fetch', IMAP::FT_UID); |
758
|
|
|
$this->fetch_options = is_long($config) ? $config : 1; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
return $this; |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Fail proof setter for $fetch_body |
766
|
|
|
* |
767
|
|
|
* @param $option |
768
|
|
|
* |
769
|
|
|
* @return $this |
770
|
|
|
*/ |
771
|
|
|
public function setFetchBodyOption($option) { |
772
|
|
|
if (is_bool($option)) { |
773
|
|
|
$this->fetch_body = $option; |
774
|
|
|
} elseif (is_null($option)) { |
775
|
|
|
$config = config('imap.options.fetch_body', true); |
776
|
|
|
$this->fetch_body = is_bool($config) ? $config : true; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
return $this; |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
/** |
783
|
|
|
* Fail proof setter for $fetch_attachment |
784
|
|
|
* |
785
|
|
|
* @param $option |
786
|
|
|
* |
787
|
|
|
* @return $this |
788
|
|
|
*/ |
789
|
|
|
public function setFetchAttachmentOption($option) { |
790
|
|
|
if (is_bool($option)) { |
791
|
|
|
$this->fetch_attachment = $option; |
792
|
|
|
} elseif (is_null($option)) { |
793
|
|
|
$config = config('imap.options.fetch_attachment', true); |
794
|
|
|
$this->fetch_attachment = is_bool($config) ? $config : true; |
795
|
|
|
} |
796
|
|
|
|
797
|
|
|
return $this; |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* Fail proof setter for $fetch_flags |
802
|
|
|
* |
803
|
|
|
* @param $option |
804
|
|
|
* |
805
|
|
|
* @return $this |
806
|
|
|
*/ |
807
|
|
|
public function setFetchFlagsOption($option) { |
808
|
|
|
if (is_bool($option)) { |
809
|
|
|
$this->fetch_flags = $option; |
810
|
|
|
} elseif (is_null($option)) { |
811
|
|
|
$config = config('imap.options.fetch_flags', true); |
812
|
|
|
$this->fetch_flags = is_bool($config) ? $config : true; |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
return $this; |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
/** |
819
|
|
|
* Decode a given string |
820
|
|
|
* |
821
|
|
|
* @param $string |
822
|
|
|
* @param $encoding |
823
|
|
|
* |
824
|
|
|
* @return string |
825
|
|
|
*/ |
826
|
|
|
public function decodeString($string, $encoding) { |
827
|
|
|
switch ($encoding) { |
828
|
|
|
case IMAP::MESSAGE_ENC_7BIT: |
829
|
|
|
return $string; |
830
|
|
|
case IMAP::MESSAGE_ENC_8BIT: |
831
|
|
|
return quoted_printable_decode(\imap_8bit($string)); |
832
|
|
|
case IMAP::MESSAGE_ENC_BINARY: |
833
|
|
|
return \imap_binary($string); |
834
|
|
|
case IMAP::MESSAGE_ENC_BASE64: |
835
|
|
|
return \imap_base64($string); |
836
|
|
|
case IMAP::MESSAGE_ENC_QUOTED_PRINTABLE: |
837
|
|
|
return quoted_printable_decode($string); |
838
|
|
|
case IMAP::MESSAGE_ENC_OTHER: |
839
|
|
|
return $string; |
840
|
|
|
default: |
841
|
|
|
return $string; |
842
|
|
|
} |
843
|
|
|
} |
844
|
|
|
|
845
|
|
|
/** |
846
|
|
|
* Convert the encoding |
847
|
|
|
* |
848
|
|
|
* @param $str |
849
|
|
|
* @param string $from |
850
|
|
|
* @param string $to |
851
|
|
|
* |
852
|
|
|
* @return mixed|string |
853
|
|
|
*/ |
854
|
|
|
public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
855
|
|
|
|
856
|
|
|
$from = EncodingAliases::get($from); |
857
|
|
|
$to = EncodingAliases::get($to); |
858
|
|
|
|
859
|
|
|
if ($from === $to) { |
860
|
|
|
return $str; |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
// We don't need to do convertEncoding() if charset is ASCII (us-ascii): |
864
|
|
|
// ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded |
865
|
|
|
// https://stackoverflow.com/a/11303410 |
866
|
|
|
// |
867
|
|
|
// us-ascii is the same as ASCII: |
868
|
|
|
// ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA) |
869
|
|
|
// prefers the updated name US-ASCII, which clarifies that this system was developed in the US and |
870
|
|
|
// based on the typographical symbols predominantly in use there. |
871
|
|
|
// https://en.wikipedia.org/wiki/ASCII |
872
|
|
|
// |
873
|
|
|
// convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken. |
874
|
|
|
if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') { |
875
|
|
|
return $str; |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { |
879
|
|
|
return @iconv($from, $to.'//IGNORE', $str); |
880
|
|
|
} else { |
881
|
|
|
if (!$from) { |
882
|
|
|
return mb_convert_encoding($str, $to); |
883
|
|
|
} |
884
|
|
|
return mb_convert_encoding($str, $to, $from); |
885
|
|
|
} |
886
|
|
|
} |
887
|
|
|
|
888
|
|
|
/** |
889
|
|
|
* Get the encoding of a given abject |
890
|
|
|
* |
891
|
|
|
* @param object|string $structure |
892
|
|
|
* |
893
|
|
|
* @return string |
894
|
|
|
*/ |
895
|
|
|
public function getEncoding($structure) { |
896
|
|
|
if (property_exists($structure, 'parameters')) { |
897
|
|
|
foreach ($structure->parameters as $parameter) { |
898
|
|
|
if (strtolower($parameter->attribute) == "charset") { |
899
|
|
|
return EncodingAliases::get($parameter->value); |
900
|
|
|
} |
901
|
|
|
} |
902
|
|
|
}elseif (is_string($structure) === true){ |
903
|
|
|
return mb_detect_encoding($structure); |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
return 'UTF-8'; |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
/** |
910
|
|
|
* Find the folder containing this message. |
911
|
|
|
* @param null|Folder $folder where to start searching from (top-level inbox by default) |
912
|
|
|
* |
913
|
|
|
* @return mixed|null|Folder |
914
|
|
|
* @throws Exceptions\ConnectionFailedException |
915
|
|
|
* @throws Exceptions\MailboxFetchingException |
916
|
|
|
* @throws InvalidMessageDateException |
917
|
|
|
* @throws MaskNotFoundException |
918
|
|
|
*/ |
919
|
|
|
public function getContainingFolder(Folder $folder = null) { |
920
|
|
|
$folder = $folder ?: $this->client->getFolders()->first(); |
921
|
|
|
$this->client->checkConnection(); |
922
|
|
|
|
923
|
|
|
// Try finding the message by uid in the current folder |
924
|
|
|
$client = new Client; |
925
|
|
|
$client->openFolder($folder->path); |
926
|
|
|
$uidMatches = \imap_fetch_overview($client->getConnection(), $this->uid, IMAP::FT_UID); |
|
|
|
|
927
|
|
|
$uidMatch = count($uidMatches) |
928
|
|
|
? new Message($uidMatches[0]->uid, $uidMatches[0]->msgno, $client) |
929
|
|
|
: null; |
930
|
|
|
$client->disconnect(); |
931
|
|
|
|
932
|
|
|
// \imap_fetch_overview() on a parent folder will return the matching message |
933
|
|
|
// even when the message is in a child folder so we need to recursively |
934
|
|
|
// search the children |
935
|
|
|
foreach ($folder->children as $child) { |
936
|
|
|
$childFolder = $this->getContainingFolder($child); |
937
|
|
|
|
938
|
|
|
if ($childFolder) { |
939
|
|
|
return $childFolder; |
940
|
|
|
} |
941
|
|
|
} |
942
|
|
|
|
943
|
|
|
// before returning the parent |
944
|
|
|
if ($this->is($uidMatch)) { |
945
|
|
|
return $folder; |
946
|
|
|
} |
947
|
|
|
|
948
|
|
|
// or signalling that the message was not found in any folder |
949
|
|
|
return null; |
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
public function getFolder(){ |
953
|
|
|
return $this->client->getFolder($this->folder_path); |
954
|
|
|
} |
955
|
|
|
|
956
|
|
|
/** |
957
|
|
|
* Move the Message into an other Folder |
958
|
|
|
* @param string $mailbox |
959
|
|
|
* @param bool $expunge |
960
|
|
|
* @param bool $create_folder |
961
|
|
|
* |
962
|
|
|
* @return null|Message |
963
|
|
|
* @throws Exceptions\ConnectionFailedException |
964
|
|
|
* @throws InvalidMessageDateException |
965
|
|
|
*/ |
966
|
|
|
public function moveToFolder($mailbox = 'INBOX', $expunge = false, $create_folder = true) { |
967
|
|
|
|
968
|
|
|
if($create_folder) $this->client->createFolder($mailbox, true); |
969
|
|
|
|
970
|
|
|
$target_folder = $this->client->getFolder($mailbox); |
971
|
|
|
$target_status = $target_folder->getStatus(IMAP::SA_ALL); |
972
|
|
|
|
973
|
|
|
$this->client->openFolder($this->folder_path); |
974
|
|
|
$status = \imap_mail_move($this->client->getConnection(), $this->uid, $mailbox, IMAP::CP_UID); |
|
|
|
|
975
|
|
|
|
976
|
|
|
if($status === true){ |
977
|
|
|
if($expunge) $this->client->expunge(); |
978
|
|
|
$this->client->openFolder($target_folder->path); |
979
|
|
|
|
980
|
|
|
$message = $target_folder->getMessage($target_status->uidnext, null, $this->fetch_options, $this->fetch_body, $this->fetch_attachment, $this->fetch_flags); |
981
|
|
|
MessageMovedEvent::dispatch($this, $message); |
982
|
|
|
return $message; |
983
|
|
|
} |
984
|
|
|
|
985
|
|
|
return null; |
986
|
|
|
} |
987
|
|
|
|
988
|
|
|
/** |
989
|
|
|
* Delete the current Message |
990
|
|
|
* @param bool $expunge |
991
|
|
|
* |
992
|
|
|
* @return bool |
993
|
|
|
* @throws Exceptions\ConnectionFailedException |
994
|
|
|
*/ |
995
|
|
|
public function delete($expunge = true) { |
996
|
|
|
$this->client->openFolder($this->folder_path); |
997
|
|
|
|
998
|
|
|
$status = \imap_delete($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
|
|
|
|
999
|
|
|
if($expunge) $this->client->expunge(); |
1000
|
|
|
MessageDeletedEvent::dispatch($this); |
1001
|
|
|
|
1002
|
|
|
return $status; |
1003
|
|
|
} |
1004
|
|
|
|
1005
|
|
|
/** |
1006
|
|
|
* Restore a deleted Message |
1007
|
|
|
* @param boolean $expunge |
1008
|
|
|
* |
1009
|
|
|
* @return bool |
1010
|
|
|
* @throws Exceptions\ConnectionFailedException |
1011
|
|
|
*/ |
1012
|
|
|
public function restore($expunge = true) { |
1013
|
|
|
$this->client->openFolder($this->folder_path); |
1014
|
|
|
|
1015
|
|
|
$status = \imap_undelete($this->client->getConnection(), $this->uid, IMAP::FT_UID); |
|
|
|
|
1016
|
|
|
if($expunge) $this->client->expunge(); |
1017
|
|
|
MessageRestoredEvent::dispatch($this); |
1018
|
|
|
|
1019
|
|
|
return $status; |
1020
|
|
|
} |
1021
|
|
|
|
1022
|
|
|
/** |
1023
|
|
|
* Get all message attachments. |
1024
|
|
|
* |
1025
|
|
|
* @return AttachmentCollection |
1026
|
|
|
*/ |
1027
|
|
|
public function getAttachments() { |
1028
|
|
|
return $this->attachments; |
1029
|
|
|
} |
1030
|
|
|
|
1031
|
|
|
/** |
1032
|
|
|
* Checks if there are any attachments present |
1033
|
|
|
* |
1034
|
|
|
* @return boolean |
1035
|
|
|
*/ |
1036
|
|
|
public function hasAttachments() { |
1037
|
|
|
return $this->attachments->isEmpty() === false; |
1038
|
|
|
} |
1039
|
|
|
|
1040
|
|
|
/** |
1041
|
|
|
* Set a given flag |
1042
|
|
|
* @param string|array $flag |
1043
|
|
|
* |
1044
|
|
|
* @return bool |
1045
|
|
|
* @throws Exceptions\ConnectionFailedException |
1046
|
|
|
*/ |
1047
|
|
|
public function setFlag($flag) { |
1048
|
|
|
$this->client->openFolder($this->folder_path); |
1049
|
|
|
|
1050
|
|
|
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag); |
1051
|
|
|
$status = \imap_setflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID); |
|
|
|
|
1052
|
|
|
$this->parseFlags(); |
1053
|
|
|
|
1054
|
|
|
return $status; |
1055
|
|
|
} |
1056
|
|
|
|
1057
|
|
|
/** |
1058
|
|
|
* Unset a given flag |
1059
|
|
|
* @param string|array $flag |
1060
|
|
|
* |
1061
|
|
|
* @return bool |
1062
|
|
|
* @throws Exceptions\ConnectionFailedException |
1063
|
|
|
*/ |
1064
|
|
|
public function unsetFlag($flag) { |
1065
|
|
|
$this->client->openFolder($this->folder_path); |
1066
|
|
|
|
1067
|
|
|
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag); |
1068
|
|
|
$status = \imap_clearflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID); |
|
|
|
|
1069
|
|
|
$this->parseFlags(); |
1070
|
|
|
|
1071
|
|
|
return $status; |
1072
|
|
|
} |
1073
|
|
|
|
1074
|
|
|
/** |
1075
|
|
|
* @return null|object|string |
1076
|
|
|
* @throws Exceptions\ConnectionFailedException |
1077
|
|
|
*/ |
1078
|
|
|
public function getRawBody() { |
1079
|
|
|
if ($this->raw_body === null) { |
1080
|
|
|
$this->client->openFolder($this->folder_path); |
1081
|
|
|
|
1082
|
|
|
$this->raw_body = \imap_fetchbody($this->client->getConnection(), $this->getUid(), '', $this->fetch_options | IMAP::FT_UID); |
|
|
|
|
1083
|
|
|
} |
1084
|
|
|
|
1085
|
|
|
return $this->raw_body; |
1086
|
|
|
} |
1087
|
|
|
|
1088
|
|
|
/** |
1089
|
|
|
* Get an almost unique message token |
1090
|
|
|
* @return string |
1091
|
|
|
* @throws Exceptions\ConnectionFailedException |
1092
|
|
|
*/ |
1093
|
|
|
public function getToken(){ |
1094
|
|
|
return base64_encode(implode('-', [$this->message_id, $this->subject, strlen($this->getRawBody())])); |
1095
|
|
|
} |
1096
|
|
|
|
1097
|
|
|
/** |
1098
|
|
|
* @return string |
1099
|
|
|
*/ |
1100
|
|
|
public function getHeader() { |
1101
|
|
|
return $this->header; |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
|
|
/** |
1105
|
|
|
* @return Client |
1106
|
|
|
*/ |
1107
|
|
|
public function getClient() { |
1108
|
|
|
return $this->client; |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
|
|
/** |
1112
|
|
|
* @return integer |
1113
|
|
|
*/ |
1114
|
|
|
public function getFetchOptions() { |
1115
|
|
|
return $this->fetch_options; |
1116
|
|
|
} |
1117
|
|
|
|
1118
|
|
|
/** |
1119
|
|
|
* @return boolean |
1120
|
|
|
*/ |
1121
|
|
|
public function getFetchBodyOption() { |
1122
|
|
|
return $this->fetch_body; |
1123
|
|
|
} |
1124
|
|
|
|
1125
|
|
|
/** |
1126
|
|
|
* @return boolean |
1127
|
|
|
*/ |
1128
|
|
|
public function getFetchAttachmentOption() { |
1129
|
|
|
return $this->fetch_attachment; |
1130
|
|
|
} |
1131
|
|
|
|
1132
|
|
|
/** |
1133
|
|
|
* @return boolean |
1134
|
|
|
*/ |
1135
|
|
|
public function getFetchFlagsOption() { |
1136
|
|
|
return $this->fetch_flags; |
1137
|
|
|
} |
1138
|
|
|
|
1139
|
|
|
/** |
1140
|
|
|
* @return mixed |
1141
|
|
|
*/ |
1142
|
|
|
public function getBodies() { |
1143
|
|
|
return $this->bodies; |
1144
|
|
|
} |
1145
|
|
|
|
1146
|
|
|
/** |
1147
|
|
|
* @return FlagCollection |
1148
|
|
|
*/ |
1149
|
|
|
public function getFlags() { |
1150
|
|
|
return $this->flags; |
1151
|
|
|
} |
1152
|
|
|
|
1153
|
|
|
/** |
1154
|
|
|
* @return object|null |
1155
|
|
|
*/ |
1156
|
|
|
public function getStructure(){ |
1157
|
|
|
return $this->structure; |
1158
|
|
|
} |
1159
|
|
|
|
1160
|
|
|
/** |
1161
|
|
|
* Does this message match another one? |
1162
|
|
|
* |
1163
|
|
|
* A match means same uid, message id, subject and date/time. |
1164
|
|
|
* |
1165
|
|
|
* @param null|Message $message |
1166
|
|
|
* @return boolean |
1167
|
|
|
*/ |
1168
|
|
|
public function is(Message $message = null) { |
1169
|
|
|
if (is_null($message)) { |
1170
|
|
|
return false; |
1171
|
|
|
} |
1172
|
|
|
|
1173
|
|
|
return $this->uid == $message->uid |
1174
|
|
|
&& $this->message_id == $message->message_id |
1175
|
|
|
&& $this->subject == $message->subject |
1176
|
|
|
&& $this->date->eq($message->date); |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
/** |
1180
|
|
|
* @return array |
1181
|
|
|
*/ |
1182
|
|
|
public function getAttributes(){ |
1183
|
|
|
return $this->attributes; |
1184
|
|
|
} |
1185
|
|
|
|
1186
|
|
|
/** |
1187
|
|
|
* @param $mask |
1188
|
|
|
* @return $this |
1189
|
|
|
*/ |
1190
|
|
|
public function setMask($mask){ |
1191
|
|
|
if(class_exists($mask)){ |
1192
|
|
|
$this->mask = $mask; |
1193
|
|
|
} |
1194
|
|
|
|
1195
|
|
|
return $this; |
1196
|
|
|
} |
1197
|
|
|
|
1198
|
|
|
/** |
1199
|
|
|
* @return string |
1200
|
|
|
*/ |
1201
|
|
|
public function getMask(){ |
1202
|
|
|
return $this->mask; |
1203
|
|
|
} |
1204
|
|
|
|
1205
|
|
|
/** |
1206
|
|
|
* Get a masked instance by providing a mask name |
1207
|
|
|
* @param string|null $mask |
1208
|
|
|
* |
1209
|
|
|
* @return mixed |
1210
|
|
|
* @throws MaskNotFoundException |
1211
|
|
|
*/ |
1212
|
|
|
public function mask($mask = null){ |
1213
|
|
|
$mask = $mask !== null ? $mask : $this->mask; |
1214
|
|
|
if(class_exists($mask)){ |
1215
|
|
|
return new $mask($this); |
1216
|
|
|
} |
1217
|
|
|
|
1218
|
|
|
throw new MaskNotFoundException("Unknown mask provided: ".$mask); |
1219
|
|
|
} |
1220
|
|
|
} |
1221
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..