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


PHP imap_bodystruct()用法及代碼示例



PHP-IMAP 函數可幫助您訪問電子郵件帳戶,IMAP 代表I互聯網M艾爾A訪問權限Protocol 使用這些函數您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

這個imap_bodystruct()函數接受表示 IMAP 流的資源值、表示特定消息的整數值和表示正文部分的字符串值作為參數,並讀取(由整數表示)消息的指定正文部分的結構。

用法

imap_bodystruct($imap_stream ,$msg, $section);

參數

Sr.No 參數及說明
1

imap_stream (Mandatory)

這是一個表示 IMAP 流的字符串值,返回值imap_open()函數。

2

msg(Mandatory)

這是一個表示消息/郵件編號的整數值。

3

section(Mandatory)

這是表示要讀取的正文部分的字符串值。

返回值

此函數返回一個對象,其中包含有關指定主體部分的結構信息。

PHP版本

這個函數最初是在 PHP 版本 4 中引入的,並且適用於所有後續版本。

示例

下麵的例子演示了使用imap_bodystruct()函數 -

<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>");
		 
         //Fetching the contents of a message
         print("Message Structure:"."<br>");
         $struct = imap_bodystruct($imap, 1, 1);
         print_r($struct);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出 -

Connection established....
Message Structure:
stdClass Object ( 
   [type] => 0 [encoding] => 0 
   [ifsubtype] => 1 
   [subtype] => PLAIN 
   [ifdescription] => 0 
   [ifid] => 0 
   [lines] => 1 
   [bytes] => 15 
   [ifdisposition] => 0 
   [ifdparameters] => 0 
   [ifparameters] => 1 
   [parameters] => Array ( 
      [0] => stdClass Object ( 
         [attribute] => CHARSET [value] => UTF-8 
      ) 
   ) 
)

示例

以下是此函數的另一個示例 -

<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>");
         //Searching emails
         $emailData = imap_search($imap, '');
        
         if (! empty($emailData)) {  
            foreach ($emailData as $msg) {
               $struct = imap_bodystruct($imap, $msg, 1);
               print_r($struct);  
               print("<br>");			
               print("<br>");			   
			   
            }    
         } 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出 -

Connection established....
stdClass Object ( 
   [type] => 0 
   [encoding] => 0 
   [ifsubtype] => 1 
   [subtype] => PLAIN 
   [ifdescription] => 0 
   [ifid] => 0 
   [lines] => 1 
   [bytes] => 15 
   [ifdisposition] => 0 
   [ifdparameters] => 0 
   [ifparameters] => 1 
   [parameters] => Array ( 
      [0] => stdClass Object ( 
         [attribute] => CHARSET [value] => UTF-8 
      ) 
   ) 
)
stdClass Object ( 
   [type] => 0 
   [encoding] => 0 
   [ifsubtype] => 1 
   [subtype] => PLAIN 
   [ifdescription] => 0 
   [ifid] => 0 
   [lines] => 1 
   [bytes] => 16 
   [ifdisposition] => 0 
   [ifdparameters] => 0 
   [ifparameters] => 1 
   [parameters] => Array ( 
      [0] => stdClass Object ( 
         [attribute] => CHARSET 
         [value] => UTF-8 
      ) 
   )
)
stdClass Object ( 
   [type] => 0 
   [encoding] => 0 
   [ifsubtype] => 1 
   [subtype] => PLAIN 
   [ifdescription] => 0 
   [ifid] => 0 
   [lines] => 1 
   [bytes] => 16 
   [ifdisposition] => 0 
   [ifdparameters] => 0 
   [ifparameters] => 1 
   [parameters] => Array ( 
      [0] => stdClass Object ([attribute] => CHARSET [value] => UTF-8 ) 
   ) 
)
stdClass Object ( 
   [type] => 0 
   [encoding] => 0 
   [ifsubtype] => 1 
   [subtype] => PLAIN 
   [ifdescription] => 0 
   [ifid] => 0 
   [lines] => 1 
   [bytes] => 16 
   [ifdisposition] => 0 
   [ifdparameters] => 0 
   [ifparameters] => 1 
   [parameters] => Array ( 
      [0] => stdClass Object ( [attribute] => CHARSET [value] => UTF-8 ) 
   ) 
)

相關用法


注:本文由純淨天空篩選整理自 PHP - imap_bodystruct() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。