查詢

mailparse_msg_extract_part()函式—用法及示例

「 從郵件訊息中提取指定的部分,並返回其內容 」


函式名:mailparse_msg_extract_part()

函式描述:mailparse_msg_extract_part() 函式從郵件訊息中提取指定的部分,並返回其內容。

適用版本:該函式在PHP版本5.0.0及以上可用。

語法:string mailparse_msg_extract_part(resource $mimemail, string $msgbody, string $callbackfunc = ?)

引數:

  • $mimemail:郵件訊息的資源控制代碼,透過 mailparse_msg_create() 函式建立。
  • $msgbody:要提取的部分識別符號,可以是部分的內容型別(例如"text/html")或部分的序號(例如"2")。
  • $callbackfunc(可選):一個回撥函式,用於處理提取的部分內容。如果不提供該引數,則函式將返回提取的部分內容。

返回值:如果提取成功,則返回提取的部分內容;如果提取失敗,則返回 FALSE。

示例:

// 建立郵件訊息資源控制代碼
$mimemail = mailparse_msg_create();

// 解析郵件訊息
mailparse_msg_parse($mimemail, $email_content);

// 提取文字部分
$text_part = mailparse_msg_extract_part($mimemail, "text/plain");

if ($text_part) {
    // 處理提取的文字內容,例如儲存到檔案
    file_put_contents("text_part.txt", $text_part);
} else {
    echo "無法提取文字部分";
}

// 關閉郵件訊息資源控制代碼
mailparse_msg_free($mimemail);

在上面的示例中,我們首先建立了一個郵件訊息資源控制代碼 $mimemail,然後使用 mailparse_msg_parse() 函式解析了郵件訊息的內容。接下來,我們使用 mailparse_msg_extract_part() 函式提取了郵件訊息中的文字部分,並將其儲存到檔案 "text_part.txt" 中。最後,我們使用 mailparse_msg_free() 函式關閉了郵件訊息資源控制代碼。

請注意,示例中的 $email_content 是一個代表郵件訊息內容的字串變數,你需要根據實際情況將其替換為有效的郵件訊息內容。

補充糾錯
熱門PHP函式
分享連結