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 Webklex\IMAP\Support\AttachmentCollection; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Message |
20
|
|
|
* |
21
|
|
|
* @package Webklex\IMAP |
22
|
|
|
*/ |
23
|
|
|
class Message { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Client instance |
27
|
|
|
* |
28
|
|
|
* @var Client |
29
|
|
|
*/ |
30
|
|
|
private $client = Client::class; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* U ID |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $uid = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Fetch body options |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $fetch_options = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Fetch attachments options |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
public $fetch_attachment = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int $msglist |
55
|
|
|
*/ |
56
|
|
|
public $msglist = 1; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string $header |
60
|
|
|
*/ |
61
|
|
|
public $header = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var null|object $header_info |
65
|
|
|
*/ |
66
|
|
|
public $header_info = null; |
67
|
|
|
|
68
|
|
|
/** @var null|object $raw_body */ |
69
|
|
|
public $raw_body = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Message header components |
73
|
|
|
* |
74
|
|
|
* @var string $message_id |
75
|
|
|
* @var mixed $message_no |
76
|
|
|
* @var string $subject |
77
|
|
|
* @var mixed $date |
78
|
|
|
* @var array $from |
79
|
|
|
* @var array $to |
80
|
|
|
* @var array $cc |
81
|
|
|
* @var array $bcc |
82
|
|
|
* @var array $reply_to |
83
|
|
|
* @var string $in_reply_to |
84
|
|
|
* @var array $sender |
85
|
|
|
*/ |
86
|
|
|
public $message_id = ''; |
87
|
|
|
public $message_no = null; |
88
|
|
|
public $subject = ''; |
89
|
|
|
public $date = null; |
90
|
|
|
public $from = []; |
91
|
|
|
public $to = []; |
92
|
|
|
public $cc = []; |
93
|
|
|
public $bcc = []; |
94
|
|
|
public $reply_to = []; |
95
|
|
|
public $in_reply_to = ''; |
96
|
|
|
public $sender = []; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Message body components |
100
|
|
|
* |
101
|
|
|
* @var array $bodies |
102
|
|
|
* @var AttachmentCollection|array $attachments |
103
|
|
|
*/ |
104
|
|
|
public $bodies = []; |
105
|
|
|
public $attachments = []; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Message const |
109
|
|
|
* |
110
|
|
|
* @const integer TYPE_TEXT |
111
|
|
|
* @const integer TYPE_MULTIPART |
112
|
|
|
* |
113
|
|
|
* @const integer ENC_7BIT |
114
|
|
|
* @const integer ENC_8BIT |
115
|
|
|
* @const integer ENC_BINARY |
116
|
|
|
* @const integer ENC_BASE64 |
117
|
|
|
* @const integer ENC_QUOTED_PRINTABLE |
118
|
|
|
* @const integer ENC_OTHER |
119
|
|
|
*/ |
120
|
|
|
const TYPE_TEXT = 0; |
121
|
|
|
const TYPE_MULTIPART = 1; |
122
|
|
|
|
123
|
|
|
const ENC_7BIT = 0; |
124
|
|
|
const ENC_8BIT = 1; |
125
|
|
|
const ENC_BINARY = 2; |
126
|
|
|
const ENC_BASE64 = 3; |
127
|
|
|
const ENC_QUOTED_PRINTABLE = 4; |
128
|
|
|
const ENC_OTHER = 5; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Message constructor. |
132
|
|
|
* |
133
|
|
|
* @param integer $uid |
134
|
|
|
* @param integer|null $msglist |
135
|
|
|
* @param Client $client |
136
|
|
|
* @param integer|null $fetch_options |
137
|
|
|
* @param boolean $parse_body |
138
|
|
|
* @param boolean $fetch_attachment |
139
|
|
|
*/ |
140
|
|
|
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $parse_body = false, $fetch_attachment = false) { |
141
|
|
|
$this->setFetchOption($fetch_options); |
142
|
|
|
$this->setFetchAttachment($fetch_attachment); |
143
|
|
|
|
144
|
|
|
$this->attachments = AttachmentCollection::make([]); |
145
|
|
|
|
146
|
|
|
$this->msglist = $msglist; |
147
|
|
|
$this->client = $client; |
148
|
|
|
$this->uid = ($this->fetch_options == FT_UID) ? $uid : imap_msgno($this->client->getConnection(), $uid); |
149
|
|
|
|
150
|
|
|
$this->parseHeader(); |
151
|
|
|
|
152
|
|
|
if ($parse_body !== false) { |
153
|
|
|
$this->parseBody(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Copy the current Messages to a mailbox |
159
|
|
|
* |
160
|
|
|
* @param $mailbox |
161
|
|
|
* @param int $options |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function copy($mailbox, $options = 0) { |
166
|
|
|
return imap_mail_copy($this->client->getConnection(), $this->msglist, $mailbox, $options); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Move the current Messages to a mailbox |
171
|
|
|
* |
172
|
|
|
* @param $mailbox |
173
|
|
|
* @param int $options |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function move($mailbox, $options = 0) { |
178
|
|
|
return imap_mail_move($this->client->getConnection(), $this->msglist, $mailbox, $options); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Check if the Message has a text body |
183
|
|
|
* |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
|
|
public function hasTextBody() { |
187
|
|
|
return isset($this->bodies['text']); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get the Message text body |
192
|
|
|
* |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
|
|
public function getTextBody() { |
196
|
|
|
if (!isset($this->bodies['text'])) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $this->bodies['text']->content; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Check if the Message has a html body |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function hasHTMLBody() { |
209
|
|
|
return isset($this->bodies['html']); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the Message html body |
214
|
|
|
* |
215
|
|
|
* @var bool $replaceImages |
216
|
|
|
* |
217
|
|
|
* @return mixed |
218
|
|
|
*/ |
219
|
|
|
public function getHTMLBody($replaceImages = false) { |
220
|
|
|
if (!isset($this->bodies['html'])) { |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$body = $this->bodies['html']->content; |
225
|
|
|
if ($replaceImages) { |
226
|
|
|
$this->attachments->each(function($oAttachment) use(&$body){ |
227
|
|
|
if ($oAttachment->id && isset($oAttachment->img_src)) { |
228
|
|
|
$body = str_replace('cid:'.$oAttachment->id, $oAttachment->img_src, $body); |
229
|
|
|
} |
230
|
|
|
}); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $body; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Parse all defined headers |
238
|
|
|
* |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
|
|
private function parseHeader() { |
242
|
|
|
$this->header = $header = imap_fetchheader($this->client->getConnection(), $this->uid, $this->fetch_options); |
|
|
|
|
243
|
|
|
if ($this->header) { |
244
|
|
|
$header = imap_rfc822_parse_headers($this->header); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (property_exists($header, 'subject')) { |
248
|
|
|
$this->subject = imap_utf8($header->subject); |
249
|
|
|
} |
250
|
|
|
if (property_exists($header, 'date')) { |
251
|
|
|
$date = $header->date; |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Exception handling for invalid dates |
255
|
|
|
* Will be extended in the future |
256
|
|
|
* |
257
|
|
|
* Currently known invalid formats: |
258
|
|
|
* ^ Datetime ^ Problem ^ Cause |
259
|
|
|
* | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature |
260
|
|
|
* | | and invalid timezone (max 6 char) | |
261
|
|
|
* | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown |
262
|
|
|
* |
263
|
|
|
* Please report any new invalid timestamps to [#45](https://github.com/Webklex/laravel-imap/issues/45) |
264
|
|
|
*/ |
265
|
|
|
try { |
266
|
|
|
$this->date = Carbon::parse($date); |
267
|
|
|
} catch(\Exception $e) { |
268
|
|
|
switch (true) { |
269
|
|
|
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}\ \([A-Z]{2,3}\+[0-9]{1,2}\:[0-9]{1,2})\)+$/i', $date) > 0: |
270
|
|
|
$array = explode('(', $date); |
271
|
|
|
$array = array_reverse($array); |
272
|
|
|
$date = trim(array_pop($array)); |
273
|
|
|
break; |
274
|
|
|
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: |
275
|
|
|
$date .= 'C'; |
276
|
|
|
break; |
277
|
|
|
} |
278
|
|
|
$this->date = Carbon::parse($date); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (property_exists($header, 'from')) { |
283
|
|
|
$this->from = $this->parseAddresses($header->from); |
284
|
|
|
} |
285
|
|
|
if (property_exists($header, 'to')) { |
286
|
|
|
$this->to = $this->parseAddresses($header->to); |
287
|
|
|
} |
288
|
|
|
if (property_exists($header, 'cc')) { |
289
|
|
|
$this->cc = $this->parseAddresses($header->cc); |
290
|
|
|
} |
291
|
|
|
if (property_exists($header, 'bcc')) { |
292
|
|
|
$this->bcc = $this->parseAddresses($header->bcc); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
if (property_exists($header, 'reply_to')) { |
296
|
|
|
$this->reply_to = $this->parseAddresses($header->reply_to); |
297
|
|
|
} |
298
|
|
|
if (property_exists($header, 'in_reply_to')) { |
299
|
|
|
$this->in_reply_to = str_replace(['<', '>'], '', $header->in_reply_to); |
300
|
|
|
} |
301
|
|
|
if (property_exists($header, 'sender')) { |
302
|
|
|
$this->sender = $this->parseAddresses($header->sender); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if (property_exists($header, 'message_id')) { |
306
|
|
|
$this->message_id = str_replace(['<', '>'], '', $header->message_id); |
307
|
|
|
} |
308
|
|
|
if (property_exists($header, 'Msgno')) { |
309
|
|
|
$this->message_no = ($this->fetch_options == FT_UID) ? trim($header->Msgno) : imap_msgno($this->client->getConnection(), trim($header->Msgno)); |
|
|
|
|
310
|
|
|
} else{ |
311
|
|
|
$this->message_no = imap_msgno($this->client->getConnection(), $this->getUid()); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Get the current Message header info |
317
|
|
|
* |
318
|
|
|
* @return object |
319
|
|
|
*/ |
320
|
|
|
public function getHeaderInfo() { |
321
|
|
|
if ($this->header_info == null) { |
322
|
|
|
$this->header_info = |
323
|
|
|
$this->header_info = imap_headerinfo($this->client->getConnection(), $this->getMessageNo()); ; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return $this->header_info; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Parse Addresses |
331
|
|
|
* |
332
|
|
|
* @param $list |
333
|
|
|
* |
334
|
|
|
* @return array |
335
|
|
|
*/ |
336
|
|
|
private function parseAddresses($list) { |
337
|
|
|
$addresses = []; |
338
|
|
|
|
339
|
|
|
foreach ($list as $item) { |
340
|
|
|
$address = (object) $item; |
341
|
|
|
|
342
|
|
|
if (!property_exists($address, 'mailbox')) { |
343
|
|
|
$address->mailbox = false; |
344
|
|
|
} |
345
|
|
|
if (!property_exists($address, 'host')) { |
346
|
|
|
$address->host = false; |
347
|
|
|
} |
348
|
|
|
if (!property_exists($address, 'personal')) { |
349
|
|
|
$address->personal = false; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
$address->personal = imap_utf8($address->personal); |
353
|
|
|
|
354
|
|
|
$address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false; |
355
|
|
|
$address->full = ($address->personal) ? $address->personal.' <'.$address->mail.'>' : $address->mail; |
|
|
|
|
356
|
|
|
|
357
|
|
|
$addresses[] = $address; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $addresses; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Parse the Message body |
365
|
|
|
* |
366
|
|
|
* @return $this |
367
|
|
|
*/ |
368
|
|
|
public function parseBody() { |
369
|
|
|
$structure = imap_fetchstructure($this->client->getConnection(), $this->uid, $this->fetch_options); |
|
|
|
|
370
|
|
|
|
371
|
|
|
$this->fetchStructure($structure); |
372
|
|
|
|
373
|
|
|
return $this; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Fetch the Message structure |
378
|
|
|
* |
379
|
|
|
* @param $structure |
380
|
|
|
* @param mixed $partNumber |
381
|
|
|
*/ |
382
|
|
|
private function fetchStructure($structure, $partNumber = null) { |
383
|
|
|
if ($structure->type == self::TYPE_TEXT && |
384
|
|
|
($structure->ifdisposition == 0 || |
385
|
|
|
($structure->ifdisposition == 1 && !isset($structure->parts) && $partNumber == null) |
386
|
|
|
) |
387
|
|
|
) { |
388
|
|
|
if ($structure->subtype == "PLAIN") { |
389
|
|
|
if (!$partNumber) { |
390
|
|
|
$partNumber = 1; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$encoding = $this->getEncoding($structure); |
394
|
|
|
|
395
|
|
|
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options); |
|
|
|
|
396
|
|
|
$content = $this->decodeString($content, $structure->encoding); |
397
|
|
|
$content = $this->convertEncoding($content, $encoding); |
398
|
|
|
|
399
|
|
|
$body = new \stdClass; |
400
|
|
|
$body->type = "text"; |
401
|
|
|
$body->content = $content; |
402
|
|
|
|
403
|
|
|
$this->bodies['text'] = $body; |
404
|
|
|
|
405
|
|
|
$this->fetchAttachment($structure, $partNumber); |
406
|
|
|
|
407
|
|
|
} elseif ($structure->subtype == "HTML") { |
408
|
|
|
if (!$partNumber) { |
409
|
|
|
$partNumber = 1; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
$encoding = $this->getEncoding($structure); |
413
|
|
|
|
414
|
|
|
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options); |
415
|
|
|
$content = $this->decodeString($content, $structure->encoding); |
416
|
|
|
$content = $this->convertEncoding($content, $encoding); |
417
|
|
|
|
418
|
|
|
$body = new \stdClass; |
419
|
|
|
$body->type = "html"; |
420
|
|
|
$body->content = $content; |
421
|
|
|
|
422
|
|
|
$this->bodies['html'] = $body; |
423
|
|
|
} |
424
|
|
|
} elseif ($structure->type == self::TYPE_MULTIPART) { |
425
|
|
|
foreach ($structure->parts as $index => $subStruct) { |
426
|
|
|
$prefix = ""; |
427
|
|
|
if ($partNumber) { |
428
|
|
|
$prefix = $partNumber."."; |
429
|
|
|
} |
430
|
|
|
$this->fetchStructure($subStruct, $prefix.($index + 1)); |
431
|
|
|
} |
432
|
|
|
} else { |
433
|
|
|
if ($this->fetch_attachment === true) { |
434
|
|
|
$this->fetchAttachment($structure, $partNumber); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* Fetch the Message attachment |
441
|
|
|
* |
442
|
|
|
* @param object $structure |
443
|
|
|
* @param mixed $partNumber |
444
|
|
|
*/ |
445
|
|
|
protected function fetchAttachment($structure, $partNumber) { |
446
|
|
|
|
447
|
|
|
$oAttachment = new Attachment($this, $structure, $partNumber); |
448
|
|
|
|
449
|
|
|
if ($oAttachment->getName() != null) { |
|
|
|
|
450
|
|
|
if ($oAttachment->getId() != null) { |
|
|
|
|
451
|
|
|
$this->attachments->put($oAttachment->getId(), $oAttachment); |
452
|
|
|
} else { |
453
|
|
|
$this->attachments->push($oAttachment); |
454
|
|
|
} |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Fail proof setter for $fetch_option |
460
|
|
|
* |
461
|
|
|
* @param $option |
462
|
|
|
* |
463
|
|
|
* @return $this |
464
|
|
|
*/ |
465
|
|
|
public function setFetchOption($option) { |
466
|
|
|
if (is_long($option) == true) { |
|
|
|
|
467
|
|
|
$this->fetch_options = $option; |
468
|
|
|
} elseif(is_null($option) == true) { |
|
|
|
|
469
|
|
|
$config = config('imap.options.fetch', FT_UID); |
470
|
|
|
$this->fetch_options = is_long($config) ? $config : 1; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
return $this; |
474
|
|
|
} |
475
|
|
|
/** |
476
|
|
|
* Fail proof setter for $fetch_attachment |
477
|
|
|
* |
478
|
|
|
* @param $option |
479
|
|
|
* |
480
|
|
|
* @return $this |
481
|
|
|
*/ |
482
|
|
|
public function setFetchAttachment($option) { |
483
|
|
|
if (is_bool($option) == true) { |
|
|
|
|
484
|
|
|
$this->fetch_attachment = $option; |
485
|
|
|
} elseif(is_null($option) == true) { |
|
|
|
|
486
|
|
|
$config = config('imap.options.attachments', false); |
487
|
|
|
$this->fetch_attachment = is_bool($config) ? $config : false; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
return $this; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Decode a given string |
495
|
|
|
* |
496
|
|
|
* @param $string |
497
|
|
|
* @param $encoding |
498
|
|
|
* |
499
|
|
|
* @return string |
500
|
|
|
*/ |
501
|
|
|
public function decodeString($string, $encoding) { |
502
|
|
|
switch ($encoding) { |
503
|
|
|
case self::ENC_7BIT: |
504
|
|
|
return $string; |
505
|
|
|
case self::ENC_8BIT: |
506
|
|
|
return quoted_printable_decode(imap_8bit($string)); |
507
|
|
|
case self::ENC_BINARY: |
508
|
|
|
return imap_binary($string); |
509
|
|
|
case self::ENC_BASE64: |
510
|
|
|
return imap_base64($string); |
511
|
|
|
case self::ENC_QUOTED_PRINTABLE: |
512
|
|
|
return quoted_printable_decode($string); |
513
|
|
|
case self::ENC_OTHER: |
514
|
|
|
return $string; |
515
|
|
|
default: |
516
|
|
|
return $string; |
517
|
|
|
} |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Convert the encoding |
522
|
|
|
* |
523
|
|
|
* @param $str |
524
|
|
|
* @param string $from |
525
|
|
|
* @param string $to |
526
|
|
|
* |
527
|
|
|
* @return mixed|string |
528
|
|
|
*/ |
529
|
|
|
private function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") { |
530
|
|
|
if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') { |
531
|
|
|
return iconv($from, $to.'//IGNORE', $str); |
532
|
|
|
} else { |
533
|
|
|
if (!$from) { |
534
|
|
|
return mb_convert_encoding($str, $to); |
535
|
|
|
} |
536
|
|
|
return mb_convert_encoding($str, $to, $from); |
537
|
|
|
} |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Get the encoding of a given abject |
542
|
|
|
* |
543
|
|
|
* @param object $structure |
544
|
|
|
* |
545
|
|
|
* @return null|string |
546
|
|
|
*/ |
547
|
|
|
private function getEncoding($structure) { |
548
|
|
|
if (property_exists($structure, 'parameters')) { |
549
|
|
|
foreach ($structure->parameters as $parameter) { |
550
|
|
|
if (strtolower($parameter->attribute) == "charset") { |
551
|
|
|
return strtoupper($parameter->value); |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
return null; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
/** |
559
|
|
|
* Move the Message into an other Folder |
560
|
|
|
* |
561
|
|
|
* @param string $mailbox |
562
|
|
|
* |
563
|
|
|
* @return bool |
564
|
|
|
*/ |
565
|
|
|
public function moveToFolder($mailbox = 'INBOX') { |
566
|
|
|
$this->client->createFolder($mailbox); |
567
|
|
|
|
568
|
|
|
if (imap_mail_move($this->client->getConnection(), $this->msglist, $mailbox) == true) { |
|
|
|
|
569
|
|
|
return true; |
570
|
|
|
} |
571
|
|
|
return false; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Delete the current Message |
576
|
|
|
* |
577
|
|
|
* @return bool |
578
|
|
|
*/ |
579
|
|
|
public function delete() { |
580
|
|
|
$status = imap_delete($this->client->getConnection(), $this->uid, $this->fetch_options); |
|
|
|
|
581
|
|
|
$this->client->expunge(); |
582
|
|
|
|
583
|
|
|
return $status; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Restore a deleted Message |
588
|
|
|
* |
589
|
|
|
* @return bool |
590
|
|
|
*/ |
591
|
|
|
public function restore() { |
592
|
|
|
return imap_undelete($this->client->getConnection(), $this->message_no); |
|
|
|
|
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Get all message attachments. |
597
|
|
|
* |
598
|
|
|
* @return AttachmentCollection |
599
|
|
|
*/ |
600
|
|
|
public function getAttachments() { |
601
|
|
|
return $this->attachments; |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Set a given flag |
606
|
|
|
* @param string|array $flag |
607
|
|
|
* |
608
|
|
|
* @return bool |
609
|
|
|
*/ |
610
|
|
|
public function setFlag($flag) { |
611
|
|
|
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag); |
612
|
|
|
return imap_setflag_full($this->client->getConnection(), $this->getUid(), $flag, SE_UID); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Unset a given flag |
617
|
|
|
* @param string|array $flag |
618
|
|
|
* |
619
|
|
|
* @return bool |
620
|
|
|
*/ |
621
|
|
|
public function unsetFlag($flag) { |
622
|
|
|
$flag = "\\".trim(is_array($flag) ? implode(" \\", $flag) : $flag); |
623
|
|
|
return imap_clearflag_full($this->client->getConnection(), $this->getUid(), "\\$flag", SE_UID); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* @return null|object|string |
628
|
|
|
*/ |
629
|
|
|
public function getRawBody() { |
630
|
|
|
if ($this->raw_body == null) { |
631
|
|
|
$this->raw_body = imap_fetchbody($this->client->getConnection(), $this->getUid(), ''); |
|
|
|
|
632
|
|
|
} |
633
|
|
|
|
634
|
|
|
return $this->raw_body; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
/** |
638
|
|
|
* @return string |
639
|
|
|
*/ |
640
|
|
|
public function getHeader() { |
641
|
|
|
return $this->header; |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* @return Client |
646
|
|
|
*/ |
647
|
|
|
public function getClient() { |
648
|
|
|
return $this->client; |
649
|
|
|
} |
650
|
|
|
|
651
|
|
|
/** |
652
|
|
|
* @return string |
653
|
|
|
*/ |
654
|
|
|
public function getUid() { |
655
|
|
|
return $this->uid; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* @return string |
660
|
|
|
*/ |
661
|
|
|
public function getFetchOptions() { |
662
|
|
|
return $this->fetch_options; |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* @return int |
667
|
|
|
*/ |
668
|
|
|
public function getMsglist() { |
669
|
|
|
return $this->msglist; |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
/** |
673
|
|
|
* @return mixed |
674
|
|
|
*/ |
675
|
|
|
public function getMessageId() { |
676
|
|
|
return $this->message_id; |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
/** |
680
|
|
|
* @return int |
681
|
|
|
*/ |
682
|
|
|
public function getMessageNo() { |
683
|
|
|
return $this->message_no; |
|
|
|
|
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
/** |
687
|
|
|
* @return string |
688
|
|
|
*/ |
689
|
|
|
public function getSubject() { |
690
|
|
|
return $this->subject; |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* @return null |
695
|
|
|
*/ |
696
|
|
|
public function getDate() { |
697
|
|
|
return $this->date; |
698
|
|
|
} |
699
|
|
|
|
700
|
|
|
/** |
701
|
|
|
* @return array |
702
|
|
|
*/ |
703
|
|
|
public function getFrom() { |
704
|
|
|
return $this->from; |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* @return array |
709
|
|
|
*/ |
710
|
|
|
public function getTo() { |
711
|
|
|
return $this->to; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
/** |
715
|
|
|
* @return array |
716
|
|
|
*/ |
717
|
|
|
public function getCc() { |
718
|
|
|
return $this->cc; |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* @return array |
723
|
|
|
*/ |
724
|
|
|
public function getBcc() { |
725
|
|
|
return $this->bcc; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* @return array |
730
|
|
|
*/ |
731
|
|
|
public function getReplyTo() { |
732
|
|
|
return $this->reply_to; |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* @return string |
737
|
|
|
*/ |
738
|
|
|
public function getInReplyTo() { |
739
|
|
|
return $this->in_reply_to; |
740
|
|
|
} |
741
|
|
|
|
742
|
|
|
/** |
743
|
|
|
* @return array |
744
|
|
|
*/ |
745
|
|
|
public function getSender() { |
746
|
|
|
return $this->sender; |
747
|
|
|
} |
748
|
|
|
|
749
|
|
|
/** |
750
|
|
|
* @return mixed |
751
|
|
|
*/ |
752
|
|
|
public function getBodies() { |
753
|
|
|
return $this->bodies; |
754
|
|
|
} |
755
|
|
|
} |
756
|
|
|
|