當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。