@@ -368,7 +368,7 @@ public function decodeStringFromUtf7ImapToUtf8($str)
368368 public function switchMailbox ($ imapPath )
369369 {
370370 $ this ->imapPath = $ imapPath ;
371- $ this ->imap ('reopen ' , $ this ->imapPath );
371+ $ this ->imap ('reopen ' , $ this ->getCombinedPath ( $ imapPath, true ) );
372372 }
373373
374374 protected function initImapStreamWithRetry ()
@@ -439,7 +439,7 @@ public function checkMailbox()
439439 */
440440 public function createMailbox ($ name )
441441 {
442- $ this ->imap ('createmailbox ' , $ this ->imapPath . $ this -> getPathDelimiter (). $ name );
442+ $ this ->imap ('createmailbox ' , $ this ->getCombinedPath ( $ name) );
443443 }
444444
445445 /**
@@ -449,7 +449,7 @@ public function createMailbox($name)
449449 */
450450 public function deleteMailbox ($ name )
451451 {
452- $ this ->imap ('deletemailbox ' , $ this ->imapPath . $ this -> getPathDelimiter (). $ name );
452+ $ this ->imap ('deletemailbox ' , $ this ->getCombinedPath ( $ name) );
453453 }
454454
455455 /**
@@ -460,7 +460,7 @@ public function deleteMailbox($name)
460460 */
461461 public function renameMailbox ($ oldName , $ newName )
462462 {
463- $ this ->imap ('renamemailbox ' , [$ this ->imapPath . $ this -> getPathDelimiter (). $ oldName , $ this ->imapPath . $ this -> getPathDelimiter (). $ newName ]);
463+ $ this ->imap ('renamemailbox ' , [$ this ->getCombinedPath ( $ oldName) , $ this ->getCombinedPath ( $ newName) ]);
464464 }
465465
466466 /**
@@ -622,17 +622,19 @@ public function markMailsAsImportant(array $mailId)
622622 /**
623623 * Causes a store to add the specified flag to the flags set for the mails in the specified sequence.
624624 *
625- * @param string $flag which you can set are \Seen, \Answered, \Flagged, \Deleted, and \Draft as defined by RFC2060
625+ * @param array $mailsIds Array of mail IDs
626+ * @param string $flag Which you can set are \Seen, \Answered, \Flagged, \Deleted, and \Draft as defined by RFC2060
626627 */
627628 public function setFlag (array $ mailsIds , $ flag )
628629 {
629630 $ this ->imap ('setflag_full ' , [implode (', ' , $ mailsIds ), $ flag , ST_UID ]);
630631 }
631632
632633 /**
633- * Cause a store to delete the specified flag to the flags set for the mails in the specified sequence.
634+ * Causes a store to delete the specified flag to the flags set for the mails in the specified sequence.
634635 *
635- * @param string $flag which you can set are \Seen, \Answered, \Flagged, \Deleted, and \Draft as defined by RFC2060
636+ * @param array $mailsIds Array of mail IDs
637+ * @param string $flag Which you can delete are \Seen, \Answered, \Flagged, \Deleted, and \Draft as defined by RFC2060
636638 */
637639 public function clearFlag (array $ mailsIds , $ flag )
638640 {
@@ -661,14 +663,14 @@ public function clearFlag(array $mailsIds, $flag)
661663 * seen - this mail is flagged as already read
662664 * draft - this mail is flagged as being a draft
663665 *
664- * @return array
666+ * @return array $mailsIds Array of mail IDs
665667 */
666668 public function getMailsInfo (array $ mailsIds )
667669 {
668670 $ mails = $ this ->imap ('fetch_overview ' , [implode (', ' , $ mailsIds ), (SE_UID == $ this ->imapSearchOption ) ? FT_UID : 0 ]);
669671 if (\is_array ($ mails ) && \count ($ mails )) {
670672 foreach ($ mails as &$ mail ) {
671- if (isset ($ mail ->subject )) {
673+ if (isset ($ mail ->subject ) and ! empty ( $ mail -> subject ) ) {
672674 $ mail ->subject = $ this ->decodeMimeStr ($ mail ->subject , $ this ->getServerEncoding ());
673675 }
674676 if (isset ($ mail ->from ) and !empty ($ mail ->from )) {
@@ -730,8 +732,8 @@ public function getMailboxInfo()
730732 * SORTCC - mailbox in first cc address
731733 * SORTSIZE - size of mail in octets
732734 *
733- * @param int $criteria
734- * @param bool $reverse
735+ * @param int $criteria Sorting criteria (eg. SORTARRIVAL)
736+ * @param bool $reverse Sort reverse or not
735737 * @param string $searchCriteria See http://php.net/imap_search for a complete list of available criteria
736738 *
737739 * @return array Mails ids
@@ -754,7 +756,7 @@ public function countMails()
754756 /**
755757 * Retrieve the quota settings per user.
756758 *
757- * @param string Should normally be in the form of which mailbox (i.e. INBOX)
759+ * @param string $quota_root Should normally be in the form of which mailbox (i.e. INBOX)
758760 *
759761 * @return array
760762 */
@@ -766,7 +768,7 @@ protected function getQuota($quota_root = 'INBOX')
766768 /**
767769 * Return quota limit in KB.
768770 *
769- * @param string Should normally be in the form of which mailbox (i.e. INBOX)
771+ * @param string $quota_root Should normally be in the form of which mailbox (i.e. INBOX)
770772 *
771773 * @return int
772774 */
@@ -780,7 +782,7 @@ public function getQuotaLimit($quota_root = 'INBOX')
780782 /**
781783 * Return quota usage in KB.
782784 *
783- * @param string Should normally be in the form of which mailbox (i.e. INBOX)
785+ * @param string $quota_root Should normally be in the form of which mailbox (i.e. INBOX)
784786 *
785787 * @return int FALSE in the case of call failure
786788 */
@@ -794,8 +796,10 @@ public function getQuotaUsage($quota_root = 'INBOX')
794796 /**
795797 * Get raw mail data.
796798 *
797- * @param $msgId
799+ * @param int $msgId ID of the message
798800 * @param bool $markAsSeen Mark the email as seen, when set to true
801+ *
802+ * @return string Message of the fetched body
799803 */
800804 public function getRawMail ($ msgId , $ markAsSeen = true )
801805 {
@@ -810,7 +814,7 @@ public function getRawMail($msgId, $markAsSeen = true)
810814 /**
811815 * Get mail header.
812816 *
813- * @param $mailId
817+ * @param int $mailId ID of the message
814818 *
815819 * @return IncomingMailHeader
816820 *
@@ -865,7 +869,7 @@ public function getMailHeader($mailId)
865869 foreach ($ head ->to as $ to ) {
866870 if (!empty ($ to ->mailbox ) && !empty ($ to ->host )) {
867871 $ toEmail = strtolower ($ to ->mailbox .'@ ' .$ to ->host );
868- $ toName = (isset ($ to ->personal ) and !empty ($ to ->personal )) ? $ this ->decodeMimeStr ($ to ->personal , $ this ->getServerEncoding ()) : null ;
872+ $ toName = (isset ($ to ->personal ) and !empty (trim ( $ to ->personal ) )) ? $ this ->decodeMimeStr ($ to ->personal , $ this ->getServerEncoding ()) : null ;
869873 $ toStrings [] = $ toName ? "$ toName < $ toEmail> " : $ toEmail ;
870874 $ header ->to [$ toEmail ] = $ toName ;
871875 }
@@ -876,22 +880,33 @@ public function getMailHeader($mailId)
876880 if (isset ($ head ->cc )) {
877881 foreach ($ head ->cc as $ cc ) {
878882 if (!empty ($ cc ->mailbox ) && !empty ($ cc ->host )) {
879- $ header ->cc [strtolower ($ cc ->mailbox .'@ ' .$ cc ->host )] = (isset ($ cc ->personal ) and !empty ($ cc ->personal )) ? $ this ->decodeMimeStr ($ cc ->personal , $ this ->getServerEncoding ()) : null ;
883+ $ ccEmail = strtolower ($ cc ->mailbox .'@ ' .$ cc ->host );
884+ $ ccName = (isset ($ cc ->personal ) and !empty (trim ($ cc ->personal ))) ? $ this ->decodeMimeStr ($ cc ->personal , $ this ->getServerEncoding ()) : null ;
885+ $ ccStrings [] = $ ccName ? "$ ccName < $ ccEmail> " : $ ccEmail ;
886+ $ header ->cc [$ ccEmail ] = $ ccName ;
880887 }
881888 }
882889 }
883890
884891 if (isset ($ head ->bcc )) {
885892 foreach ($ head ->bcc as $ bcc ) {
886893 if (!empty ($ bcc ->mailbox ) && !empty ($ bcc ->host )) {
887- $ header ->bcc [strtolower ($ bcc ->mailbox .'@ ' .$ bcc ->host )] = (isset ($ bcc ->personal ) and !empty ($ bcc ->personal )) ? $ this ->decodeMimeStr ($ bcc ->personal , $ this ->getServerEncoding ()) : null ;
894+ $ bccEmail = strtolower ($ bcc ->mailbox .'@ ' .$ bcc ->host );
895+ $ bccName = (isset ($ bcc ->personal ) and !empty (trim ($ bcc ->personal ))) ? $ this ->decodeMimeStr ($ bcc ->personal , $ this ->getServerEncoding ()) : null ;
896+ $ bccStrings [] = $ bccName ? "$ bccName < $ bccEmail> " : $ bccEmail ;
897+ $ header ->bcc [$ bccEmail ] = $ bccName ;
888898 }
889899 }
890900 }
891901
892902 if (isset ($ head ->reply_to )) {
893903 foreach ($ head ->reply_to as $ replyTo ) {
894- $ header ->replyTo [strtolower ($ replyTo ->mailbox .'@ ' .$ replyTo ->host )] = (isset ($ replyTo ->personal ) and !empty ($ replyTo ->personal )) ? $ this ->decodeMimeStr ($ replyTo ->personal , $ this ->getServerEncoding ()) : null ;
904+ if (!empty ($ replyTo ->mailbox ) && !empty ($ replyTo ->host )) {
905+ $ replyToEmail = strtolower ($ replyTo ->mailbox .'@ ' .$ replyTo ->host );
906+ $ replyToName = (isset ($ replyTo ->personal ) and !empty (trim ($ replyTo ->personal ))) ? $ this ->decodeMimeStr ($ replyTo ->personal , $ this ->getServerEncoding ()) : null ;
907+ $ replyToStrings [] = $ replyToName ? "$ replyToName < $ replyToEmail> " : $ replyToEmail ;
908+ $ header ->replyTo [$ replyToEmail ] = $ replyToName ;
909+ }
895910 }
896911 }
897912
@@ -902,10 +917,33 @@ public function getMailHeader($mailId)
902917 return $ header ;
903918 }
904919
920+ /**
921+ * taken from https://www.electrictoolbox.com/php-imap-message-parts/.
922+ */
923+ public function flattenParts ($ messageParts , $ flattenedParts = [], $ prefix = '' , $ index = 1 , $ fullPrefix = true )
924+ {
925+ foreach ($ messageParts as $ part ) {
926+ $ flattenedParts [$ prefix .$ index ] = $ part ;
927+ if (isset ($ part ->parts )) {
928+ if (2 == $ part ->type ) {
929+ $ flattenedParts = $ this ->flattenParts ($ part ->parts , $ flattenedParts , $ prefix .$ index .'. ' , 0 , false );
930+ } elseif ($ fullPrefix ) {
931+ $ flattenedParts = $ this ->flattenParts ($ part ->parts , $ flattenedParts , $ prefix .$ index .'. ' );
932+ } else {
933+ $ flattenedParts = $ this ->flattenParts ($ part ->parts , $ flattenedParts , $ prefix );
934+ }
935+ unset($ flattenedParts [$ prefix .$ index ]->parts );
936+ }
937+ ++$ index ;
938+ }
939+
940+ return $ flattenedParts ;
941+ }
942+
905943 /**
906944 * Get mail data.
907945 *
908- * @param $mailId
946+ * @param int $mailId ID of the mail
909947 * @param bool $markAsSeen Mark the email as seen, when set to true
910948 *
911949 * @return IncomingMail
@@ -920,8 +958,8 @@ public function getMail($mailId, $markAsSeen = true)
920958 if (empty ($ mailStructure ->parts )) {
921959 $ this ->initMailPart ($ mail , $ mailStructure , 0 , $ markAsSeen );
922960 } else {
923- foreach ($ mailStructure ->parts as $ partNum => $ partStructure ) {
924- $ this ->initMailPart ($ mail , $ partStructure , $ partNum + 1 , $ markAsSeen );
961+ foreach ($ this -> flattenParts ( $ mailStructure ->parts ) as $ partNum => $ partStructure ) {
962+ $ this ->initMailPart ($ mail , $ partStructure , $ partNum , $ markAsSeen );
925963 }
926964 }
927965
@@ -979,9 +1017,10 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $m
9791017 }
9801018
9811019 // Do NOT parse attachments, when getAttachmentsIgnore() is true
982- if ($ this ->getAttachmentsIgnore () &&
983- (TYPEMULTIPART !== $ partStructure ->type &&
984- (TYPETEXT !== $ partStructure ->type || !\in_array (strtolower ($ partStructure ->subtype ), ['plain ' , 'html ' ])))) {
1020+ if ($ this ->getAttachmentsIgnore ()
1021+ && (TYPEMULTIPART !== $ partStructure ->type
1022+ && (TYPETEXT !== $ partStructure ->type || !\in_array (strtolower ($ partStructure ->subtype ), ['plain ' , 'html ' ])))
1023+ ) {
9851024 return false ;
9861025 }
9871026
@@ -1282,7 +1321,7 @@ public function getSubscribedMailboxes($search = '*')
12821321 */
12831322 public function subscribeMailbox ($ mailbox )
12841323 {
1285- $ this ->imap ('subscribe ' , $ this ->imapPath . $ this -> getPathDelimiter (). $ mailbox );
1324+ $ this ->imap ('subscribe ' , $ this ->getCombinedPath ( $ mailbox) );
12861325 }
12871326
12881327 /**
@@ -1294,7 +1333,7 @@ public function subscribeMailbox($mailbox)
12941333 */
12951334 public function unsubscribeMailbox ($ mailbox )
12961335 {
1297- $ this ->imap ('unsubscribe ' , $ this ->imapPath . $ this -> getPathDelimiter (). $ mailbox );
1336+ $ this ->imap ('unsubscribe ' , $ this ->getCombinedPath ( $ mailbox) );
12981337 }
12991338
13001339 /**
@@ -1352,4 +1391,29 @@ public function imap($methodShortName, $args = [], $prependConnectionAsFirstArg
13521391
13531392 return $ result ;
13541393 }
1394+
1395+ /**
1396+ * Combine Subfolder or Folder to the connection.
1397+ * Have the imapPath a folder added to the connection info, then will the $folder added as subfolder.
1398+ * If the parameter $absolute TRUE, then will the connection new builded only with this folder as root element.
1399+ *
1400+ * @param string $folder Folder, the will added to the path
1401+ * @param bool $absolute Add folder as root element to the connection and remove all other from this
1402+ *
1403+ * @return string Return the new path
1404+ */
1405+ protected function getCombinedPath (string $ folder , bool $ absolute = false )
1406+ {
1407+ if (!empty ($ folder )) {
1408+ if ('} ' === substr ($ this ->imapPath , -1 ) || true === $ absolute ) {
1409+ $ posConnectionDefinitionEnd = strpos ($ this ->imapPath , '} ' );
1410+
1411+ return substr ($ this ->imapPath , 0 , $ posConnectionDefinitionEnd + 1 ).$ folder ;
1412+ } else {
1413+ return $ this ->imapPath .$ this ->getPathDelimiter ().$ folder ;
1414+ }
1415+ }
1416+
1417+ return $ this ->imapPath ;
1418+ }
13551419}
0 commit comments