函式名:imap_fetchstructure()
適用版本:PHP 4, PHP 5, PHP 7
用法:imap_fetchstructure() 函式用於獲取郵件的結構資訊。它返回一個物件,包含了郵件的各個部分(如文字、附件等)的詳細資訊。
語法:mixed imap_fetchstructure ( resource $imap_stream , int $msg_number [, int $options = 0 ] )
引數:
- $imap_stream:必需。一個有效的 IMAP 連線資源。
- $msg_number:必需。要獲取結構資訊的郵件的訊息號。
- $options:可選。用於指定選項的整數值。預設為0。
返回值:成功時返回一個物件,包含郵件的結構資訊。失敗時返回 FALSE。
示例:
// 建立 IMAP 連線
$imap_stream = imap_open("{imap.example.com:993/imap/ssl}INBOX", "username", "password");
// 獲取郵件結構資訊
$msg_number = 1;
$structure = imap_fetchstructure($imap_stream, $msg_number);
// 列印郵件結構資訊
print_r($structure);
// 關閉 IMAP 連線
imap_close($imap_stream);
輸出示例:
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => HTML
[ifdescription] => 0
[ifid] => 0
[bytes] => 1234
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => CHARSET
[value] => UTF-8
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 3
[ifsubtype] => 1
[subtype] => PLAIN
[ifdescription] => 0
[ifid] => 0
[bytes] => 567
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => CHARSET
[value] => UTF-8
)
)
[parts] => Array
(
)
)
[1] => stdClass Object
(
[type] => 0
[encoding] => 4
[ifsubtype] => 1
[subtype] => JPEG
[ifdescription] => 0
[ifid] => 0
[bytes] => 123456
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => NAME
[value] => image.jpg
)
)
[parts] => Array
(
)
)
)
)
上述示例中,我們首先建立了一個 IMAP 連線,然後使用 imap_fetchstructure() 函式獲取第一封郵件的結構資訊,並將其儲存在變數 $structure 中。最後,我們列印出了該結構資訊。
結構資訊是一個包含各個部分的物件,其中包含了每個部分的型別、編碼、子型別、大小等詳細資訊。在示例輸出中,可以看到郵件的主體部分是一個 HTML 型別的子部分,其編碼為 UTF-8,大小為 1234 位元組。郵件還包含了一個純文字型別的子部分和一個 JPEG 型別的附件。
最後,我們關閉了 IMAP 連線以釋放資源。