函数名称:imap_rfc822_parse_headers()
适用版本:该函数适用于PHP 4.3.0及以上版本。
函数描述:imap_rfc822_parse_headers()函数解析RFC 822格式的邮件头,并返回一个关联数组,包含邮件头字段的名称和值。
用法:
array imap_rfc822_parse_headers ( string $headers [, string $defaulthost = "UNKNOWN" ] )
参数:
- headers:要解析的RFC 822格式的邮件头字符串。
- defaulthost(可选):如果邮件头中没有提供主机名,则使用此参数作为默认主机名。
返回值:返回一个关联数组,包含邮件头字段的名称和值。如果解析失败,则返回false。
示例:
// 邮件头字符串
$headers = "From: John Doe <johndoe@example.com>\r\n" .
"To: Jane Smith <janesmith@example.com>\r\n" .
"Subject: Hello World\r\n" .
"Date: Fri, 10 Sep 2021 12:00:00 +0000\r\n" .
"Message-ID: <1234567890@example.com>\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"\r\n";
// 解析邮件头
$parsedHeaders = imap_rfc822_parse_headers($headers);
// 打印解析结果
print_r($parsedHeaders);
输出:
Array
(
[From] => John Doe <johndoe@example.com>
[To] => Jane Smith <janesmith@example.com>
[Subject] => Hello World
[Date] => Fri, 10 Sep 2021 12:00:00 +0000
[Message-ID] => <1234567890@example.com>
[MIME-Version] => 1.0
[Content-Type] => text/plain; charset=UTF-8
)
以上示例中,我们使用imap_rfc822_parse_headers()函数解析了一个邮件头字符串,并将解析结果存储在一个关联数组中。然后,我们使用print_r()函数打印出解析结果,以便查看各个邮件头字段的名称和值。