@@ -83,14 +83,7 @@ class Message
83
83
*
84
84
* @var string
85
85
*/
86
- protected static $ flagTypes = array (
87
- self ::FLAG_RECENT ,
88
- self ::FLAG_FLAGGED ,
89
- self ::FLAG_ANSWERED ,
90
- self ::FLAG_DELETED ,
91
- self ::FLAG_SEEN ,
92
- self ::FLAG_DRAFT
93
- );
86
+ protected static $ flagTypes = array (self ::FLAG_RECENT , self ::FLAG_FLAGGED , self ::FLAG_ANSWERED , self ::FLAG_DELETED , self ::FLAG_SEEN , self ::FLAG_DRAFT );
94
87
95
88
/**
96
89
* This holds the plantext email message.
@@ -213,18 +206,17 @@ class Message
213
206
* message should be opened from. This constructor should generally not be called directly, but rather retrieved
214
207
* through the apprioriate Imap functions.
215
208
*
216
- * @param int $messageUniqueId
217
- * @param Server $connection
209
+ * @param int $messageUniqueId
210
+ * @param Server $mailbox
218
211
*/
219
212
public function __construct ($ messageUniqueId , Server $ connection )
220
213
{
221
214
$ this ->imapConnection = $ connection ;
222
- $ this ->mailbox = $ connection ->getMailBox ();
223
- $ this ->uid = $ messageUniqueId ;
224
- $ this ->imapStream = $ this ->imapConnection ->getImapStream ();
225
- if ($ this ->loadMessage () !== true ) {
215
+ $ this ->mailbox = $ connection ->getMailBox ();
216
+ $ this ->uid = $ messageUniqueId ;
217
+ $ this ->imapStream = $ this ->imapConnection ->getImapStream ();
218
+ if ($ this ->loadMessage () !== true )
226
219
throw new \RuntimeException ('Message with ID ' . $ messageUniqueId . ' not found. ' );
227
- }
228
220
}
229
221
230
222
/**
@@ -237,39 +229,34 @@ protected function loadMessage()
237
229
238
230
/* First load the message overview information */
239
231
240
- if (!is_object ($ messageOverview = $ this ->getOverview ())) {
232
+ if (!is_object ($ messageOverview = $ this ->getOverview ()))
233
+
241
234
return false ;
242
- }
243
235
244
236
$ this ->subject = MIME ::decode ($ messageOverview ->subject , self ::$ charset );
245
- $ this ->date = strtotime ($ messageOverview ->date );
246
- $ this ->size = $ messageOverview ->size ;
237
+ $ this ->date = strtotime ($ messageOverview ->date );
238
+ $ this ->size = $ messageOverview ->size ;
247
239
248
- foreach (self ::$ flagTypes as $ flag ) {
240
+ foreach (self ::$ flagTypes as $ flag )
249
241
$ this ->status [$ flag ] = ($ messageOverview ->$ flag == 1 );
250
- }
251
242
252
243
/* Next load in all of the header information */
253
244
254
245
$ headers = $ this ->getHeaders ();
255
246
256
- if (isset ($ headers ->to )) {
247
+ if (isset ($ headers ->to ))
257
248
$ this ->to = $ this ->processAddressObject ($ headers ->to );
258
- }
259
249
260
- if (isset ($ headers ->cc )) {
250
+ if (isset ($ headers ->cc ))
261
251
$ this ->cc = $ this ->processAddressObject ($ headers ->cc );
262
- }
263
252
264
- if (isset ($ headers ->bcc )) {
253
+ if (isset ($ headers ->bcc ))
265
254
$ this ->bcc = $ this ->processAddressObject ($ headers ->bcc );
266
- }
267
255
268
- if (isset ($ headers ->sender )) {
256
+ if (isset ($ headers ->sender ))
269
257
$ this ->sender = $ this ->processAddressObject ($ headers ->sender );
270
- }
271
258
272
- $ this ->from = isset ($ headers ->from ) ? $ this ->processAddressObject ($ headers ->from ) : array ('' );
259
+ $ this ->from = isset ($ headers ->from ) ? $ this ->processAddressObject ($ headers ->from ) : array ('' );
273
260
$ this ->replyTo = isset ($ headers ->reply_to ) ? $ this ->processAddressObject ($ headers ->reply_to ) : $ this ->from ;
274
261
275
262
/* Finally load the structure itself */
@@ -281,9 +268,8 @@ protected function loadMessage()
281
268
$ this ->processStructure ($ structure );
282
269
} else {
283
270
// multipart
284
- foreach ($ structure ->parts as $ id => $ part ) {
271
+ foreach ($ structure ->parts as $ id => $ part )
285
272
$ this ->processStructure ($ part , $ id + 1 );
286
- }
287
273
}
288
274
289
275
return true ;
@@ -294,19 +280,19 @@ protected function loadMessage()
294
280
* imap_fetch_overview function, only instead of an array of message overviews only a single result is returned. The
295
281
* results are only retrieved from the server once unless passed true as a parameter.
296
282
*
297
- * @param bool $forceReload
283
+ * @param bool $forceReload
298
284
* @return \stdClass
299
285
*/
300
286
public function getOverview ($ forceReload = false )
301
287
{
302
288
if ($ forceReload || !isset ($ this ->messageOverview )) {
303
289
// returns an array, and since we just want one message we can grab the only result
304
- $ results = imap_fetch_overview ($ this ->imapStream , $ this ->uid , FT_UID );
305
- if (sizeof ($ results ) == 0 ) {
290
+ $ results = imap_fetch_overview ($ this ->imapStream , $ this ->uid , FT_UID );
291
+ if ( sizeof ($ results ) == 0 ) {
306
292
throw new \RuntimeException ('Error fetching overview ' );
307
293
}
308
294
$ this ->messageOverview = array_shift ($ results );
309
- if (! isset ($ this ->messageOverview ->date )) {
295
+ if ( ! isset ($ this ->messageOverview ->date )) {
310
296
$ this ->messageOverview ->date = null ;
311
297
}
312
298
}
@@ -317,7 +303,7 @@ public function getOverview($forceReload = false)
317
303
/**
318
304
* This function returns an object containing the raw headers of the message.
319
305
*
320
- * @param bool $forceReload
306
+ * @param bool $forceReload
321
307
* @return string
322
308
*/
323
309
public function getRawHeaders ($ forceReload = false )
@@ -335,7 +321,7 @@ public function getRawHeaders($forceReload = false)
335
321
* and running them through the imap_rfc822_parse_headers function. The results are only retrieved from the server
336
322
* once unless passed true as a parameter.
337
323
*
338
- * @param bool $forceReload
324
+ * @param bool $forceReload
339
325
* @return \stdClass
340
326
*/
341
327
public function getHeaders ($ forceReload = false )
@@ -366,7 +352,7 @@ public function getHeaders($forceReload = false)
366
352
* returned by imap_fetchstructure. The results are only retrieved from the server once unless passed true as a
367
353
* parameter.
368
354
*
369
- * @param bool $forceReload
355
+ * @param bool $forceReload
370
356
* @return \stdClass
371
357
*/
372
358
public function getStructure ($ forceReload = false )
@@ -384,7 +370,7 @@ public function getStructure($forceReload = false)
384
370
* the plaintext version is given some html formatting and returned. If neither are present the return value will be
385
371
* false.
386
372
*
387
- * @param bool $html Pass true to receive an html response.
373
+ * @param bool $html Pass true to receive an html response.
388
374
* @return string|bool Returns false if no body is present.
389
375
*/
390
376
public function getMessageBody ($ html = false )
@@ -400,7 +386,7 @@ public function getMessageBody($html = false)
400
386
}
401
387
} else {
402
388
if (!isset ($ this ->plaintextMessage ) && isset ($ this ->htmlMessage )) {
403
- $ output = preg_replace ('/\s*\<br\s*\/?\>/i ' , PHP_EOL , trim ($ this ->htmlMessage ));
389
+ $ output = preg_replace ('/\s*\<br\s*\/?\>/i ' , PHP_EOL , trim ($ this ->htmlMessage ) );
404
390
$ output = strip_tags ($ output );
405
391
406
392
return $ output ;
@@ -434,36 +420,32 @@ public function getHtmlBody()
434
420
* This function returns either an array of email addresses and names or, optionally, a string that can be used in
435
421
* mail headers.
436
422
*
437
- * @param string $type Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.
438
- * @param bool $asString
423
+ * @param string $type Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.
424
+ * @param bool $asString
439
425
* @return array|string|bool
440
426
*/
441
427
public function getAddresses ($ type , $ asString = false )
442
428
{
443
- $ type = ($ type == 'reply-to ' ) ? 'replyTo ' : $ type ;
429
+ $ type = ( $ type == 'reply-to ' ) ? 'replyTo ' : $ type ;
444
430
$ addressTypes = array ('to ' , 'cc ' , 'bcc ' , 'from ' , 'sender ' , 'replyTo ' );
445
431
446
- if (!in_array ($ type , $ addressTypes ) || !isset ($ this ->$ type ) || count ($ this ->$ type ) < 1 ) {
432
+ if (!in_array ($ type , $ addressTypes ) || !isset ($ this ->$ type ) || count ($ this ->$ type ) < 1 )
447
433
return false ;
448
- }
449
434
450
435
if (!$ asString ) {
451
- if ($ type == 'from ' ) {
436
+ if ($ type == 'from ' )
452
437
return $ this ->from [0 ];
453
- } elseif ($ type == 'sender ' ) {
438
+ elseif ($ type == 'sender ' )
454
439
return $ this ->sender [0 ];
455
- }
456
440
457
441
return $ this ->$ type ;
458
442
} else {
459
443
$ outputString = '' ;
460
444
foreach ($ this ->$ type as $ address ) {
461
- if (isset ($ set )) {
445
+ if (isset ($ set ))
462
446
$ outputString .= ', ' ;
463
- }
464
- if (!isset ($ set )) {
447
+ if (!isset ($ set ))
465
448
$ set = true ;
466
- }
467
449
468
450
$ outputString .= isset ($ address ['name ' ]) ?
469
451
$ address ['name ' ] . ' < ' . $ address ['address ' ] . '> '
@@ -520,7 +502,7 @@ public function getImapBox()
520
502
* message has its own subparts, those are recursively processed using this function.
521
503
*
522
504
* @param \stdClass $structure
523
- * @param string $partIdentifier
505
+ * @param string $partIdentifier
524
506
*/
525
507
protected function processStructure ($ structure , $ partIdentifier = null )
526
508
{
@@ -529,7 +511,7 @@ protected function processStructure($structure, $partIdentifier = null)
529
511
if ((isset ($ parameters ['name ' ]) || isset ($ parameters ['filename ' ]))
530
512
|| (isset ($ structure ->subtype ) && strtolower ($ structure ->subtype ) == 'rfc822 ' )
531
513
) {
532
- $ attachment = new Attachment ($ this , $ structure , $ partIdentifier );
514
+ $ attachment = new Attachment ($ this , $ structure , $ partIdentifier );
533
515
$ this ->attachments [] = $ attachment ;
534
516
} elseif ($ structure ->type == 0 || $ structure ->type == 1 ) {
535
517
$ messageBody = isset ($ partIdentifier ) ?
@@ -541,23 +523,21 @@ protected function processStructure($structure, $partIdentifier = null)
541
523
if (!empty ($ parameters ['charset ' ]) && $ parameters ['charset ' ] !== self ::$ charset ) {
542
524
$ mb_converted = false ;
543
525
if (function_exists ('mb_convert_encoding ' )) {
544
- $ mb_encoding = false ;
545
- foreach (mb_list_encodings () as $ encoding ) {
546
- if (strtolower ($ encoding ) === strtolower ($ parameters ['charset ' ])) {
547
- $ mb_encoding = true ;
548
- }
549
- }
550
-
551
- if ($ mb_encoding === false ) {
526
+ $ encodings = mb_list_encodings ();
527
+ if ($ key = array_search (
528
+ strtolower ($ parameters ['charset ' ]),
529
+ array_map (function ($ val ){return mb_strtolower ($ val );}, $ encodings )
530
+ ) !== false ) {
531
+ $ parameters ['charset ' ] = $ encodings [$ key ];
532
+ }else {
552
533
$ parameters ['charset ' ] = ($ structure ->encoding === 0 ? 'US-ASCII ' : 'UTF-8 ' );
553
534
}
554
535
555
536
$ messageBody = @mb_convert_encoding ($ messageBody , self ::$ charset , $ parameters ['charset ' ]);
556
537
$ mb_converted = true ;
557
538
}
558
539
if (!$ mb_converted ) {
559
- $ messageBodyConv = @iconv ($ parameters ['charset ' ], self ::$ charset . self ::$ charsetFlag ,
560
- $ messageBody );
540
+ $ messageBodyConv = @iconv ($ parameters ['charset ' ], self ::$ charset . self ::$ charsetFlag , $ messageBody );
561
541
562
542
if ($ messageBodyConv !== false ) {
563
543
$ messageBody = $ messageBodyConv ;
@@ -589,9 +569,8 @@ protected function processStructure($structure, $partIdentifier = null)
589
569
foreach ($ structure ->parts as $ partIndex => $ part ) {
590
570
$ partId = $ partIndex + 1 ;
591
571
592
- if (isset ($ partIdentifier )) {
572
+ if (isset ($ partIdentifier ))
593
573
$ partId = $ partIdentifier . '. ' . $ partId ;
594
- }
595
574
596
575
$ this ->processStructure ($ part , $ partId );
597
576
}
@@ -601,7 +580,7 @@ protected function processStructure($structure, $partIdentifier = null)
601
580
/**
602
581
* This function takes in the message data and encoding type and returns the decoded data.
603
582
*
604
- * @param string $data
583
+ * @param string $data
605
584
* @param int|string $encoding
606
585
* @return string
607
586
*/
@@ -628,7 +607,7 @@ public static function decode($data, $encoding)
628
607
/**
629
608
* This function returns the body type that an imap integer maps to.
630
609
*
631
- * @param int $id
610
+ * @param int $id
632
611
* @return string
633
612
*/
634
613
public static function typeIdToString ($ id )
@@ -670,17 +649,13 @@ public static function typeIdToString($id)
670
649
public static function getParametersFromStructure ($ structure )
671
650
{
672
651
$ parameters = array ();
673
- if (isset ($ structure ->parameters )) {
674
- foreach ($ structure ->parameters as $ parameter ) {
652
+ if (isset ($ structure ->parameters ))
653
+ foreach ($ structure ->parameters as $ parameter )
675
654
$ parameters [strtolower ($ parameter ->attribute )] = $ parameter ->value ;
676
- }
677
- }
678
655
679
- if (isset ($ structure ->dparameters )) {
680
- foreach ($ structure ->dparameters as $ parameter ) {
656
+ if (isset ($ structure ->dparameters ))
657
+ foreach ($ structure ->dparameters as $ parameter )
681
658
$ parameters [strtolower ($ parameter ->attribute )] = $ parameter ->value ;
682
- }
683
- }
684
659
685
660
return $ parameters ;
686
661
}
@@ -695,7 +670,7 @@ public static function getParametersFromStructure($structure)
695
670
protected function processAddressObject ($ addresses )
696
671
{
697
672
$ outputAddresses = array ();
698
- if (is_array ($ addresses )) {
673
+ if (is_array ($ addresses ))
699
674
foreach ($ addresses as $ address ) {
700
675
if (property_exists ($ address , 'mailbox ' ) && $ address ->mailbox != 'undisclosed-recipients ' ) {
701
676
$ currentAddress = array ();
@@ -706,7 +681,6 @@ protected function processAddressObject($addresses)
706
681
$ outputAddresses [] = $ currentAddress ;
707
682
}
708
683
}
709
- }
710
684
711
685
return $ outputAddresses ;
712
686
}
@@ -725,24 +699,21 @@ public function getUid()
725
699
* This function returns the attachments a message contains. If a filename is passed then just that ImapAttachment
726
700
* is returned, unless
727
701
*
728
- * @param null|string $filename
702
+ * @param null|string $filename
729
703
* @return array|bool|Attachment[]
730
704
*/
731
705
public function getAttachments ($ filename = null )
732
706
{
733
- if (!isset ($ this ->attachments ) || count ($ this ->attachments ) < 1 ) {
707
+ if (!isset ($ this ->attachments ) || count ($ this ->attachments ) < 1 )
734
708
return false ;
735
- }
736
709
737
- if (!isset ($ filename )) {
710
+ if (!isset ($ filename ))
738
711
return $ this ->attachments ;
739
- }
740
712
741
713
$ results = array ();
742
714
foreach ($ this ->attachments as $ attachment ) {
743
- if ($ attachment ->getFileName () == $ filename ) {
715
+ if ($ attachment ->getFileName () == $ filename )
744
716
$ results [] = $ attachment ;
745
- }
746
717
}
747
718
748
719
switch (count ($ results )) {
@@ -772,8 +743,8 @@ public function checkFlag($flag = self::FLAG_FLAGGED)
772
743
/**
773
744
* This function is used to enable or disable one or more flags on the imap message.
774
745
*
775
- * @param string|array $flag Flagged, Answered, Deleted, Seen, Draft
776
- * @param bool $enable
746
+ * @param string|array $flag Flagged, Answered, Deleted, Seen, Draft
747
+ * @param bool $enable
777
748
* @throws \InvalidArgumentException
778
749
* @return bool
779
750
*/
@@ -783,9 +754,8 @@ public function setFlag($flag, $enable = true)
783
754
784
755
foreach ($ flags as $ i => $ flag ) {
785
756
$ flag = ltrim (strtolower ($ flag ), '\\' );
786
- if (!in_array ($ flag , self ::$ flagTypes ) || $ flag == self ::FLAG_RECENT ) {
757
+ if (!in_array ($ flag , self ::$ flagTypes ) || $ flag == self ::FLAG_RECENT )
787
758
throw new \InvalidArgumentException ('Unable to set invalid flag " ' . $ flag . '" ' );
788
- }
789
759
790
760
if ($ enable ) {
791
761
$ this ->status [$ flag ] = true ;
@@ -796,7 +766,7 @@ public function setFlag($flag, $enable = true)
796
766
$ flags [$ i ] = $ flag ;
797
767
}
798
768
799
- $ imapifiedFlag = '\\' . implode (' \\' , array_map ('ucfirst ' , $ flags ));
769
+ $ imapifiedFlag = '\\' . implode (' \\' , array_map ('ucfirst ' , $ flags ));
800
770
801
771
if ($ enable === true ) {
802
772
return imap_setflag_full ($ this ->imapStream , $ this ->uid , $ imapifiedFlag , ST_UID );
0 commit comments