簡體   English   中英

如何使用PHP imap將電子郵件從一個文件夾移動到另一個文件夾

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

我正在嘗試從電子郵件中讀取消息...根據主題的內容,我想將其移動到“進程”或“未授權”文件夾中

將消息保存在數組中,然后將消息從“收件箱”移動到“繼續”文件夾

這是我所做的

// 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.');
}

我沒有任何問題地閱讀該消息,但是在嘗試移動該消息時出現錯誤。

.[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

我認為問題在於如何將$ id傳遞給imap_mail_move函數。

我還嘗試將消息順序號轉換為UID號,例如$f['id'] = imap_uid($this->conn , $id ) ,但這沒有用。

我也嘗試過

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

我什至嘗試復制然后刪除該消息,但這沒有用。

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

我無法傳達信息。

如何正確移動郵件?

我發現了問題。

問題是“已Processed文件夾不是INBOX文件夾的子文件夾。 這是INBOX旁邊的文件夾設置。

要點是使用imap_mail_move()函數時,您需要傳遞序列號或一系列序列號

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

每個收到的消息都有一個序列號1,2,3,n ,其中n是在給定框中收到的最新消息。

這是$id變量的示例

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

第一個示例意味着將消息1從當前文件夾移動到$box定義的新文件夾。

第二個示例意味着將消息1,2,3,4,5從當前文件夾移動到$box定義的新文件夾。

第三個示例意味着將消息1,2,5,6,7從當前文件夾移動到$box定義的新文件夾。

此外,以下是$ box變量的一些示例

'INBOX/Processed'
'Unauthorized'

第一個示例表示位於INBOX文件夾下的“已Processed文件夾。

第二個示例表示位於INBOX文件夾“相同位置”旁邊的Unauthorized文件夾

要了解每個文件夾在電子郵件中的位置,可以使用imap_list函數。

我希望這對其他人有所幫助,因為我花了一段時間才找到了這個愚蠢的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM