查询

imap_fetchstructure()函数—用法及示例

「 获取邮件的结构信息 」


函数名:imap_fetchstructure()

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

用法:imap_fetchstructure() 函数用于获取邮件的结构信息。它返回一个对象,包含了邮件的各个部分(如文本、附件等)的详细信息。

语法:mixed imap_fetchstructure ( resource $imap_stream , int $msg_number [, int $options = 0 ] )

参数:

  • $imap_stream:必需。一个有效的 IMAP 连接资源。
  • $msg_number:必需。要获取结构信息的邮件的消息号。
  • $options:可选。用于指定选项的整数值。默认为0。

返回值:成功时返回一个对象,包含邮件的结构信息。失败时返回 FALSE。

示例:

// 建立 IMAP 连接
$imap_stream = imap_open("{imap.example.com:993/imap/ssl}INBOX", "username", "password");

// 获取邮件结构信息
$msg_number = 1;
$structure = imap_fetchstructure($imap_stream, $msg_number);

// 打印邮件结构信息
print_r($structure);

// 关闭 IMAP 连接
imap_close($imap_stream);

输出示例:

stdClass Object
(
    [type] => 1
    [encoding] => 0
    [ifsubtype] => 1
    [subtype] => HTML
    [ifdescription] => 0
    [ifid] => 0
    [bytes] => 1234
    [ifdisposition] => 0
    [ifdparameters] => 0
    [ifparameters] => 1
    [parameters] => Array
        (
            [0] => stdClass Object
                (
                    [attribute] => CHARSET
                    [value] => UTF-8
                )

        )

    [parts] => Array
        (
            [0] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 3
                    [ifsubtype] => 1
                    [subtype] => PLAIN
                    [ifdescription] => 0
                    [ifid] => 0
                    [bytes] => 567
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => CHARSET
                                    [value] => UTF-8
                                )

                        )

                    [parts] => Array
                        (
                        )

                )

            [1] => stdClass Object
                (
                    [type] => 0
                    [encoding] => 4
                    [ifsubtype] => 1
                    [subtype] => JPEG
                    [ifdescription] => 0
                    [ifid] => 0
                    [bytes] => 123456
                    [ifdisposition] => 0
                    [ifdparameters] => 0
                    [ifparameters] => 1
                    [parameters] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [attribute] => NAME
                                    [value] => image.jpg
                                )

                        )

                    [parts] => Array
                        (
                        )

                )

        )

)

上述示例中,我们首先建立了一个 IMAP 连接,然后使用 imap_fetchstructure() 函数获取第一封邮件的结构信息,并将其存储在变量 $structure 中。最后,我们打印出了该结构信息。

结构信息是一个包含各个部分的对象,其中包含了每个部分的类型、编码、子类型、大小等详细信息。在示例输出中,可以看到邮件的主体部分是一个 HTML 类型的子部分,其编码为 UTF-8,大小为 1234 字节。邮件还包含了一个纯文本类型的子部分和一个 JPEG 类型的附件。

最后,我们关闭了 IMAP 连接以释放资源。

补充纠错
上一个函数: imap_fetchtext()函数
下一个函数: imap_fetchmime()函数
热门PHP函数
分享链接