[英]How to determine part numbers of an email retrieved via PHP IMAP functions?
要使用imap_fetchbody()
獲取消息正文的特定部分,您必須傳遞與IMAP部件號相關的section
參數,並在PHP文檔中定義如下 :
它是一個由句點分隔的整數字符串,它根據IMAP4規范索引到正文部分列表中
我唯一的線索是使用imap_fetchstructure()
來讀取特定消息的結構。 但是,我不知道如何從中推斷出零件編號。
編輯
對於那些對IMAPv4規范感興趣的人, 這里是關於提取部件號的部分的段落 。 不幸的是,它沒有明確說明如何獲取或計算它們。
你是對的,你必須使用imap_fetchstructure()
函數的結果。
您可以使用以下函數獲取郵件的部分和子部分的完整列表:
function getPartList($struct, $base="") {
$res=Array();
if (!property_exists($struct,"parts")) {
return [$base?:"0"];
} else {
$num=1;
if (count($struct->parts)==1) return getPartList($struct->parts[0], $base);
foreach ($struct->parts as $p=>$part) {
foreach (getPartList($part, $p+1) as $subpart) {
$res[]=($base?"$base.":"").$subpart;
}
}
}
return $res;
}
$struct=imap_fetchstructure($mbox, $i);
$res=getPartList($struct);
print_r($res);
Result:
Array(
[0] => 1
[1] => 2
[2] => 3.1
[3] => 3.2
[4] => 4.1
[5] => 4.2
);
編輯:請注意您的服務器可能無法處理RFC822子部分
例如,在Dovecot v2.2服務器上,它返回一個空字符串
telnet imapserver.com 143
a1 LOGIN user@example.com password
a2 SELECT "Inbox"
a3 FETCH 522 (BODY[3.1.2])
// result:
* 522 FETCH (BODY[3.1.2] {0}
)
a3 OK Fetch completed.
a4 logout
EDIT2:似乎只有一個部分的子部分不計算...參見修改后的代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.