简体   繁体   中英

How can I move an email message from one folder to another using PHP imap

I am trying to read messages from an email... Depending on the subject's content I want to move it to either a "Processes" or "unauthorized" folder

save the messages in an array and then move the message from the INBOX to the Proceeded folder

Here is what I have done

// Checks the inbox
if ($messages = imap_search($this->conn,'ALL'))
{
    // Sorts the messages newest first
    rsort($messages);
    // Loops through the messages
    foreach ($messages as $id)
    {
        $header = imap_headerinfo($this->conn, $id);
        $message = imap_fetchbody($this->conn, $id, 1);

        if(    !isset($header->from[0]->mailbox) || empty($header->from[0]->mailbox)
            || !isset($header->from[0]->host) || empty($header->from[0]->host)
            || !isset($header->subject) || empty($header->from[0]->host)
        ) {
            continue;
        }

        $from = $header->from[0]->mailbox . '@' . $header->from[0]->host;
        $subject = $header->subject;    

        $outlook = $this->_parseReplyExchange($message);

        if($outlook !== false){
            $newReply = $outlook;
        } else {
            $newReply = $this->_parseReplySystem($message);
        }

        $ticketID = $this->_parseTicketID($subject);
        if($ticketID !== false){
            $f = array();
            $f['id'] = $id;
            $f['from'] = $from;
            $f['subject'] = $subject;
            $f['ticketID'] = $ticketID;
            $f['message'] = $newReply;
            $this->replyList[] = $f;

            $imapresult = imap_mail_move($this->conn, $id, $box, CP_UID);

            if($imapresult == false){
                echo imap_last_error();
            }
        }
    }
}
else
{
    exit('No messages on the IMAP server.');
}

I read the message with no issues, but when trying to moving the message I get an error.

.[TRYCREATE] The requested item could not be found.
Notice: Unknown: [TRYCREATE] The requested item could not be found. (errflg=2) in Unknown on line 0

I think the issue is the way how I am passing the $id to the imap_mail_move function.

I also tried to convert the message sequance number to a UID number like so $f['id'] = imap_uid($this->conn , $id ) and that did not work..

I also tried this

$imapresult = imap_mail_move($this->conn, '1:' . $id, $box);
$imapresult = imap_mail_move($this->conn, '1:' . $id, $box, CP_UID);

I even tried to copy and then delete the message and that did not work.

$imapresult = imap_mail_copy($c, '1', 'INBOX/Processed', CP_MOVE);

I can't get the message to move.

How can I correctly move the message?

I found the issue.

The issue was is that the Processed folder was not a sub folder of the INBOX folder. It was a folder setting next to INBOX.

The take away here is when using the imap_mail_move() function you will need to pass either a sequence number or a range of sequence numbers

$imapresult = imap_mail_move($this->conn, $id, $box);

Each message received have a sequence number 1,2,3,n where n is the newest message received in a giving box.

Here is an examples of the $id variable

1
1:5
1,2,5,6,7

The first example means move message 1 from current folder to a new folder defined in $box .

The second example means move messages 1,2,3,4,5 from current folder to a new folder defined in $box .

The third example means move messages 1,2,5,6,7 from current folder to a new folder defined in $box .

In addition, here are some examples of the $box variable

'INBOX/Processed'
'Unauthorized'

The first example means the Processed folder that is located under the INBOX folder.

The second example means the Unauthorized folder that is located next "same location" to the INBOX folder

To know where each folder is located in your email, you can use imap_list function.

I hope this helps other as it took me a while to find this silly issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM