查詢

imap_headers()函式—用法及示例

「 獲取郵件信箱中所有郵件的頭資訊 」


函式名:imap_headers()

適用版本:PHP 4, PHP 5, PHP 7

用法:imap_headers() 函式用於獲取郵件信箱中所有郵件的頭資訊。

語法:array imap_headers ( resource $imap_stream )

引數:

  • imap_stream:必需。IMAP 郵箱的連線資源。

返回值:返回一個包含郵件頭資訊的陣列,如果出錯則返回 FALSE。

示例:

// 連線到 IMAP 郵箱
$imap_stream = imap_open("{imap.example.com:993/imap/ssl}", "username", "password");

// 獲取郵件信箱中所有郵件的頭資訊
$headers = imap_headers($imap_stream);

// 列印每封郵件的主題
foreach ($headers as $header) {
    $mail = imap_fetchheader($imap_stream, $header);
    $subject = imap_qprint(imap_utf8(imap_fetchstructure($imap_stream, $header))->subject);
    echo "郵件主題:$subject" . PHP_EOL;
}

// 關閉 IMAP 郵箱連線
imap_close($imap_stream);

以上示例首先使用 imap_open() 函式連線到 IMAP 郵箱,然後使用 imap_headers() 函式獲取所有郵件的頭資訊。接著使用 imap_fetchheader() 函式和 imap_fetchstructure() 函式獲取每封郵件的頭資訊和主題,並使用 imap_qprint() 函式和 imap_utf8() 函式對主題進行解碼和轉換。最後列印出每封郵件的主題。最後使用 imap_close() 函式關閉 IMAP 郵箱連線。

補充糾錯
上一個函式: imap_is_open()函式
下一個函式: imap_headerinfo()函式
熱門PHP函式
分享連結