在郵箱中查詢郵件

你可以使用 imap_headers 返回郵箱中所有郵件的列表。

<?php
$headers = imap_headers($mailbox);

結果是具有以下模式的字串陣列:

[FLAG] [MESSAGE-ID])[DD-MM-YYY] [FROM ADDRESS] [SUBJECT TRUNCATED TO 25 CHAR] ([SIZE] chars)

以下是每行的外觀示例:

A     1)19-Aug-2016 someone@example.com Message Subject (1728 chars)
D     2)19-Aug-2016 someone@example.com RE: Message Subject (22840 chars)
U     3)19-Aug-2016 someone@example.com RE: RE: Message Subject (1876 chars)
N     4)19-Aug-2016 someone@example.com RE: RE: RE: Message Subje (1741 chars)
符號 標誌 含義
一個 回答 訊息已被回覆
d 刪除 郵件已刪除(但未刪除)
F 標記 訊息被標記/盯著注意
ñ 訊息是新的,尚未見過
[R 最近 訊息是新的,已被看到
U 未讀 訊息尚未閱讀
X 草案 訊息是草稿

請注意,此呼叫可能需要相當長的時間才能執行,並且可能會返回一個非常大的列表

另一種方法是根據需要載入單個郵件。你的每封電子郵件都會分配一個 ID,從 1(最舊)到 imap_num_msg($mailbox) 的值。

有許多函式可以直接訪問電子郵件,但最簡單的方法是使用 imap_header 返回結構化的頭資訊:

<?php
$header = imap_headerinfo($mailbox , 1);

stdClass Object
(
    [date] => Wed, 19 Oct 2011 17:34:52 +0000
    [subject] => Message Subject
    [message_id] => <04b80ceedac8e74$51a8d50dd$0206600a@user1687763490>
    [references] => <ec129beef8a113c941ad68bdaae9@example.com>
    [toaddress] => Some One Else <someoneelse@example.com>
    [to] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Some One Else
                    [mailbox] => someonelse
                    [host] => example.com
                )
        )
    [fromaddress] => Some One <someone@example.com>
    [from] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Some One
                    [mailbox] => someone
                    [host] => example.com
                )
        )
    [reply_toaddress] => Some One <someone@example.com>
    [reply_to] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Some One
                    [mailbox] => someone
                    [host] => example.com
                )
        )
    [senderaddress] => Some One <someone@example.com>
    [sender] => Array
        (
            [0] => stdClass Object
                (
                    [personal] => Some One
                    [mailbox] => someone
                    [host] => example.com
                )
        )
    [Recent] =>  
    [Unseen] =>  
    [Flagged] =>  
    [Answered] =>  
    [Deleted] =>  
    [Draft] =>  
    [Msgno] =>    1
    [MailDate] => 19-Oct-2011 17:34:48 +0000
    [Size] => 1728
    [udate] => 1319038488
)