当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP imap_rfc822_parse_headers()用法及代码示例



PHP-IMAP 函数可帮助您访问电子邮件帐户,IMAP 代表I互联网M艾尔A访问权限Protocol 使用这些函数您还可以使用 NNTP、POP3 协议和本地邮箱访问方法。

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

用法

imap_rfc822_parse_headers($address [, $default_host]);

参数

Sr.No 参数及说明
1

address (Mandatory)

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

2

default_host (Optional)

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

返回值

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

PHP版本

这个函数最初是在 PHP 版本 4 中引入的,并且适用于所有后续版本。

示例

以下示例演示了 imap_rfc822_parse_headers() 函数的用法 -

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $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] => tutorialspoint.test@gmail.com [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 = "tutorialspoint.test@gmail.com";
         $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] => tutorialspoint.test@gmail.com [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 - imap_rfc822_parse_headers() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。