PHP - imap_fetchbody() 函式



PHP 的 IMAP 函式幫助您訪問電子郵件帳戶,IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol,使用這些函式,您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

imap_fetchbody() 函式接受表示 IMAP 流的資源值、表示郵箱中郵件的整數值、包含郵件部分號(用“.”分隔)的字串值作為引數,並檢索郵件正文的指定部分。

語法

imap_fetchbody($imap_stream, $msg, $section [, $options]);

引數

序號 引數及描述
1

imap_stream (必填)

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

2

msg (必填)

這是一個整數值,表示要標記為刪除的訊息/郵件編號。

3

section (必填)

這是一個字串值,表示郵件編號(用“.”分隔)。

4

options (可選)

這是一個可選引數,可以是以下一個或多個:

  • FT_UID

  • FT_PEEK

  • FT_INTERNAL

  • FT_INTERNAL

返回值

此函式返回一個字串值,表示檢索到的郵件/訊息部分。

PHP 版本

此函式首次引入於 PHP 4 版本,並在所有後續版本中均有效。

示例

以下示例演示了 imap_fetchbody() 函式的用法:

<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("Contents of the first message: "."<br>");
         $body = imap_fetchbody($imap, 1, 1);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Contents of the first message:
#sample_mail1

示例

以下示例獲取當前流中郵件的 UID:

<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("Contents of the first message: "."<br>");
         $body = imap_fetchbody($imap, imap_uid($imap, 1), 1, FT_UID);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Contents of the first message:
#sample_mail1

示例

以下是此函式的另一個示例,在這個示例中,我們已建立到特定郵箱的連線並檢索其中郵件的內容。

<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) {
               $msg = imap_fetchbody($imap, $msg, "1");
               print(quoted_printable_decode($msg)."<br>");                
            }    
         } 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

此程式生成以下輸出:

Connection established....
#sample_mail1
#sample_mail2
#sample_mail3
#sample_mail4
#sample_mail5
#sample_mail6
php_function_reference.htm
廣告