PHP - imap_fetchmime() 函式



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

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

語法

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

引數

選項(可選)

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

  • FT_UID

  • FT_PEEK

  • FT_INTERNAL

序號 引數及說明
1

imap_stream(必填)

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

2

msg(必填)

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

3

section(必填)

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

返回值

此函式返回一個包含檢索到的 MIME 頭的字串值。

PHP 版本

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

示例

以下示例演示了 imap_fetchmime() 函式的使用:

<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("Mime Headers: "."<br>");
         $body = imap_fetchmime($imap, 1, 1);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

以下是一個函式示例:

Connection established....
Mime Headers:
Content−Type: text/plain; charset="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) {
               $msg = imap_fetchmime($imap, $msg, "1");
               print($msg."<br>");      			   
            }    
         } 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/plain; charset="UTF-8"
Content-Type: text/plain; charset="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>");
		 
         //Fetching the contents of a message
         print("Mime Headers: "."<br>");
         $body = imap_fetchmime($imap, imap_uid($imap, 1), 1, FT_UID);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Mime Headers:
Content-Type: text/plain; charset="UTF-8"
php_function_reference.htm
廣告