简体   繁体   English

如何确定通过PHP IMAP函数检索的电子邮件的部件号?

[英]How to determine part numbers of an email retrieved via PHP IMAP functions?

To fetch the particular section of the body of a message with imap_fetchbody() , you have to pass the section parameter which relates to the IMAP part number and is defined as follow in the PHP documentation : 要使用imap_fetchbody()获取消息正文的特定部分,您必须传递与IMAP部件号相关的section参数,并在PHP文档中定义如下

It is a string of integers delimited by period which index into a body part list as per the IMAP4 specification 它是一个由句点分隔的整数字符串,它根据IMAP4规范索引到正文部分列表中

The only clue I have is to use imap_fetchstructure() to read the structure of a particular message. 我唯一的线索是使用imap_fetchstructure()来读取特定消息的结构。 However, I don't know how to deduce the part numbers from it. 但是,我不知道如何从中推断出零件编号。

EDIT 编辑

For those interested in the IMAPv4 specification, here is the paragraph about fetching where part numbers are mentioned. 对于那些对IMAPv4规范感兴趣的人, 这里是关于提取部件号的部分的段落 Unfortunately, it does not clearly state how to get or compute them. 不幸的是,它没有明确说明如何获取或计算它们。

You are right, you have to use the result of the imap_fetchstructure() function. 你是对的,你必须使用imap_fetchstructure()函数的结果。

You can get the full listing of parts and subpart of a mail by using the following function : 您可以使用以下函数获取邮件的部分和子部分的完整列表:

 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
 );

EDIT: be aware that your server may not handle RFC822 subpart 编辑:请注意您的服务器可能无法处理RFC822子部分

For instance on a Dovecot v2.2 server, it returns an empty string 例如,在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 : it seems that subparts with only one part does not count... See modified code EDIT2:似乎只有一个部分的子部分不计算...参见修改后的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM