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

标签 php imap

要使用 imap_fetchbody() 获取消息正文的特定部分,您必须传递与 IMAP 部件号相关且为 defined as follow in the PHP documentationsection 参数:

It is a string of integers delimited by period which index into a body part list as per the IMAP4 specification

我唯一的线索是使用 imap_fetchstructure() 读取特定消息的结构。但是,我不知道如何从中推断出部件号。

编辑

对于那些对 IMAPv4 规范感兴趣的人,here is the paragraph about fetching 其中提到了部件号。不幸的是,它没有明确说明如何获取或计算它们。

最佳答案

你是对的,你必须使用 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:似乎只有一个部分的子部分不算数...查看修改后的代码

关于php - 如何确定通过 PHP IMAP 函数检索到的电子邮件的部件号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442522/

相关文章:

php - HTTP 请求被视为超时的默认时间是多少?

php - Laravel 5.3 抛出错误表单请求

node.js - node-imap模块获取gmail列表文件夹(标签)

python - IMAP电子邮件IDLE停止等待超时持续时间,而是在Python中立即循环

java imap 从某个日期开始获取消息

java - 在地址数组中循环?

php - PHP/PDO 下的 MySQL 槽

php - 如何使用 PHP 填充输入字段

php - 尝试仅检索具有特定列 php mysql 的表

PHPUnit 电子邮件测试