PHP - imap_rfc822_parse_headers() 函数



PHP−IMAP 函数可帮助您访问电子邮件帐户,IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol,使用这些函数您还可以使用 NNTP、POP3 协议和本地邮箱访问方法。

imap_rfc822_parse_headers() 函数接受两个字符串值(表示地址和默认主机名)作为参数,并解析并返回给定字符串中的电子邮件标题。

语法

imap_rfc822_parse_headers($address [, $default_host]);

参数

序号 参数和描述
1

address(必填)

这是一个表示地址的字符串值。

2

default_host(可选)

这是一个表示默认主机名的字符串值。

返回值

此函数返回一个包含标题的对象。

PHP 版本

此函数首次在 PHP 4 版本中引入,并在所有后续版本中可用。

示例

以下示例演示了 imap_rfc822_parse_headers() 函数的使用:

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "[email protected]";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
		  
         //Parsing a mail address	 
         print("Parsing mail address: "."<br>");
         $header = imap_fetchheader($imap, 1);
         $res = imap_rfc822_parse_headers($header);
         print_r($res);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

输出

这将生成以下输出:

Connection established....
Parsing mail address:
stdClass Object ( 
   [date] => Thu, 22 Oct 2020 20:10:17 +0530 
   [Date] => Thu, 22 Oct 2020 20:10:17 +0530 
   [subject] => [Subject] => [message_id] => 
   [toaddress] => [email protected] [to] => 
   Array ( 
      [0] => stdClass Object ( 
         [mailbox] => tutorialspoint.test [host] => gmail.com 
      ) 
   ) 
   [fromaddress] => Sender[from] => 
   Array ( 
      [0] => stdClass Object ( 
         [personal] => Sender[mailbox] => 
         sample.test[host] => gmail.com 
      ) 
   ) 
   [reply_toaddress] => Sender[reply_to] => 
   Array ( 
      [0] => stdClass Object ( 
         [personal] => Sender[mailbox] => 
         sample.test[host] => gmail.com 
      ) 
   ) 
   [senderaddress] => Sender[sender] => 
   Array ( 
      [0] => stdClass Object ( 
         [personal] => Sender[mailbox] => sample.test[host] => gmail.com 
      ) 
   ) 
)

示例

以下是上述函数使用可选参数的示例:

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "[email protected]";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
		  
         //Parsing a mail address	 
         print("Parsing mail address: "."<br>");
         $header = imap_fetchheader($imap, 1);
         $res = imap_rfc822_parse_headers($header, "default_header");
         print_r($res);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

输出

这将生成以下输出:

Connection established....
Parsing mail address:
stdClass Object ( [date] => Thu, 22 Oct 2020 20:10:17 +0530 [Date] => Thu, 
22 Oct 2020 20:10:17 +0530 [subject] => [Subject] => [message_id] => 
[toaddress] => [email protected] [to] => Array ( [0] => 
stdClass Object ( [mailbox] => tutorialspoint.test [host] => gmail.com ) ) 
[fromaddress] => Sender [from] => Array ( [0] => stdClass Object ( 
[personal] => Sender [mailbox] => sample.test[host] => gmail.com ) ) 
[reply_toaddress] => Sender [reply_to] => Array ( [0] => stdClass Object 
( [personal] => Sender [mailbox] => sample.test[host] => gmail.com ) ) 
[senderaddress] => Sender [sender] => Array ( [0] => stdClass Object ( 
[personal] => Sender [mailbox] => sample.test[host] => gmail.com ) ) )
php_function_reference.htm
广告