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