PHP - imap_body() 函式



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

**imap_body()** 函式接受表示 IMAP 流的資源值和表示特定郵件的整數值作為引數,並以字串的形式讀取指定郵件/訊息的主體。

語法

imap_body($imap_stream ,$msg [,options]);

引數

序號 引數及描述
1

imap_stream (必填)

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

2

msg (必填)

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

3

Options (可選)

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

  • FT_UID

  • FT_PEEK

  • FT_INTERNAL

返回值

此函式返回一個字串值,表示給定郵箱中指定訊息的主體。

PHP 版本

此函式最早在 PHP 4 版本中引入,並在所有後續版本中有效。

示例

這是一個演示 **imap_fetchtext()** 函式用法的示例:-

<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_body($imap, 1);
         print($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:-

Connection established....
Contents of the first message:
--000000000000a0d34e05b24373f4 Content-Type: text/plain; charset="UTF-8" #sample_mail1 --000000000000a0d34e05b24373f4 Content-Type: text/html; charset="UTF-8"
#sample_mail1
--000000000000a0d34e05b24373f4--

示例

這是此函式的另一個示例:-

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

輸出

這將生成以下輸出:-

Connection established....
−−000000000000a0d34e05b24373f4 Content-Type: text/plain; charset="UTF-8" #sample_mail1 −−000000000000a0d34e05b24373f4 Content-Type: text/html; charset="UTF−8"
#sample_mail1
−−000000000000a0d34e05b24373f4−−
−−000000000000bb1b8205b24375b9 Content-Type: text/plain; charset="UTF−8" #sample_mail2 −−000000000000bb1b8205b24375b9 Content-Type: text/html; charset="UTF−8"
#sample_mail2
−−000000000000bb1b8205b24375b9−−
−−000000000000dceebf05b27c7601 Content-Type: text/plain; charset="UTF−8" #sample_mail3 −−000000000000dceebf05b27c7601 Content-Type: text/html; charset="UTF-8"
#sample_mail3
−−000000000000dceebf05b27c7601−−
−−000000000000e7e7c705b27d7527 Content-Type: text/plain; charset="UTF-8" #sample_mail4 −−000000000000e7e7c705b27d7527 Content−Type: text/html; charset="UTF−8"
php_function_reference.htm
廣告