@@ -110,6 +110,13 @@ class Message {
110110 'priority ' => 0 ,
111111 ];
112112
113+ /**
114+ * The message folder path
115+ *
116+ * @var string $folder_path
117+ */
118+ protected $ folder_path ;
119+
113120 /**
114121 * Fetch body options
115122 *
@@ -193,6 +200,8 @@ public function __construct($uid, $msglist, Client $client, $fetch_options = nul
193200 $ this ->mask = $ default_mask ;
194201 }
195202
203+ $ this ->folder_path = $ client ->getFolderPath ();
204+
196205 $ this ->config = config ('imap.options ' );
197206
198207 $ this ->setFetchOption ($ fetch_options );
@@ -285,6 +294,7 @@ public function __get($name) {
285294 * @throws Exceptions\ConnectionFailedException
286295 */
287296 public function copy ($ mailbox , $ options = 0 ) {
297+ $ this ->client ->openFolder ($ this ->folder_path );
288298 return imap_mail_copy ($ this ->client ->getConnection (), $ this ->msglist , $ mailbox , $ options );
289299 }
290300
@@ -298,6 +308,7 @@ public function copy($mailbox, $options = 0) {
298308 * @throws Exceptions\ConnectionFailedException
299309 */
300310 public function move ($ mailbox , $ options = 0 ) {
311+ $ this ->client ->openFolder ($ this ->folder_path );
301312 return imap_mail_move ($ this ->client ->getConnection (), $ this ->msglist , $ mailbox , $ options );
302313 }
303314
@@ -375,6 +386,7 @@ public function getHTMLBody($replaceImages = false) {
375386 * @throws InvalidMessageDateException
376387 */
377388 private function parseHeader () {
389+ $ this ->client ->openFolder ($ this ->folder_path );
378390 $ this ->header = $ header = imap_fetchheader ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
379391
380392 $ this ->priority = $ this ->extractPriority ($ this ->header );
@@ -515,6 +527,7 @@ private function parseDate($header) {
515527 private function parseFlags () {
516528 $ this ->flags = FlagCollection::make ([]);
517529
530+ $ this ->client ->openFolder ($ this ->folder_path );
518531 $ flags = imap_fetch_overview ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
519532 if (is_array ($ flags ) && isset ($ flags [0 ])) {
520533 foreach ($ this ->available_flags as $ flag ) {
@@ -548,7 +561,7 @@ private function parseFlag($flags, $flag) {
548561 */
549562 public function getHeaderInfo () {
550563 if ($ this ->header_info == null ) {
551- $ this ->header_info =
564+ $ this ->client -> openFolder ( $ this -> folder_path );
552565 $ this ->header_info = imap_headerinfo ($ this ->client ->getConnection (), $ this ->getMessageNo ());
553566 }
554567
@@ -568,7 +581,6 @@ private function extractHeaderAddressPart($header, $part) {
568581
569582 /**
570583 * Parse Addresses
571- *
572584 * @param $list
573585 *
574586 * @return array
@@ -612,6 +624,7 @@ private function parseAddresses($list) {
612624 * @throws Exceptions\ConnectionFailedException
613625 */
614626 public function parseBody () {
627+ $ this ->client ->openFolder ($ this ->folder_path );
615628 $ this ->structure = imap_fetchstructure ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
616629
617630 if (property_exists ($ this ->structure , 'parts ' )){
@@ -645,6 +658,8 @@ public function parseBody() {
645658 * @throws Exceptions\ConnectionFailedException
646659 */
647660 private function fetchStructure ($ structure , $ partNumber = null ) {
661+ $ this ->client ->openFolder ($ this ->folder_path );
662+
648663 if ($ structure ->type == IMAP ::MESSAGE_TYPE_TEXT &&
649664 ($ structure ->ifdisposition == 0 ||
650665 ($ structure ->ifdisposition == 1 && !isset ($ structure ->parts ) && $ partNumber == null )
@@ -917,7 +932,7 @@ public function getContainingFolder(Folder $folder = null) {
917932
918933 // Try finding the message by uid in the current folder
919934 $ client = new Client ;
920- $ client ->openFolder ($ folder );
935+ $ client ->openFolder ($ folder-> path );
921936 $ uidMatches = imap_fetch_overview ($ client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
922937 $ uidMatch = count ($ uidMatches )
923938 ? new Message ($ uidMatches [0 ]->uid , $ uidMatches [0 ]->msgno , $ client )
@@ -961,10 +976,12 @@ public function moveToFolder($mailbox = 'INBOX', $expunge = false, $create_folde
961976 $ target_folder = $ this ->client ->getFolder ($ mailbox );
962977 $ target_status = $ target_folder ->getStatus (IMAP ::SA_ALL );
963978
979+ $ this ->client ->openFolder ($ this ->folder_path );
964980 $ status = imap_mail_move ($ this ->client ->getConnection (), $ this ->uid , $ mailbox , IMAP ::CP_UID );
981+
965982 if ($ status === true ){
966983 if ($ expunge ) $ this ->client ->expunge ();
967- $ this ->client ->openFolder ($ target_folder );
984+ $ this ->client ->openFolder ($ target_folder-> path );
968985
969986 return $ target_folder ->getMessage ($ target_status ->uidnext );
970987 }
@@ -980,6 +997,8 @@ public function moveToFolder($mailbox = 'INBOX', $expunge = false, $create_folde
980997 * @throws Exceptions\ConnectionFailedException
981998 */
982999 public function delete ($ expunge = true ) {
1000+ $ this ->client ->openFolder ($ this ->folder_path );
1001+
9831002 $ status = imap_delete ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
9841003 if ($ expunge ) $ this ->client ->expunge ();
9851004
@@ -994,6 +1013,8 @@ public function delete($expunge = true) {
9941013 * @throws Exceptions\ConnectionFailedException
9951014 */
9961015 public function restore ($ expunge = true ) {
1016+ $ this ->client ->openFolder ($ this ->folder_path );
1017+
9971018 $ status = imap_undelete ($ this ->client ->getConnection (), $ this ->uid , IMAP ::FT_UID );
9981019 if ($ expunge ) $ this ->client ->expunge ();
9991020
@@ -1026,6 +1047,8 @@ public function hasAttachments() {
10261047 * @throws Exceptions\ConnectionFailedException
10271048 */
10281049 public function setFlag ($ flag ) {
1050+ $ this ->client ->openFolder ($ this ->folder_path );
1051+
10291052 $ flag = "\\" .trim (is_array ($ flag ) ? implode (" \\" , $ flag ) : $ flag );
10301053 $ status = imap_setflag_full ($ this ->client ->getConnection (), $ this ->getUid (), $ flag , SE_UID );
10311054 $ this ->parseFlags ();
@@ -1041,6 +1064,8 @@ public function setFlag($flag) {
10411064 * @throws Exceptions\ConnectionFailedException
10421065 */
10431066 public function unsetFlag ($ flag ) {
1067+ $ this ->client ->openFolder ($ this ->folder_path );
1068+
10441069 $ flag = "\\" .trim (is_array ($ flag ) ? implode (" \\" , $ flag ) : $ flag );
10451070 $ status = imap_clearflag_full ($ this ->client ->getConnection (), $ this ->getUid (), $ flag , SE_UID );
10461071 $ this ->parseFlags ();
@@ -1054,6 +1079,8 @@ public function unsetFlag($flag) {
10541079 */
10551080 public function getRawBody () {
10561081 if ($ this ->raw_body === null ) {
1082+ $ this ->client ->openFolder ($ this ->folder_path );
1083+
10571084 $ this ->raw_body = imap_fetchbody ($ this ->client ->getConnection (), $ this ->getUid (), '' , $ this ->fetch_options | IMAP ::FT_UID );
10581085 }
10591086
0 commit comments