PHP - imap_fetchheader() 函式



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

imap_fetchheader() 函式接受表示 IMAP 流的資源值、表示郵箱中郵件的整數值作為引數,並檢索指定郵件的報頭。

語法

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

引數

序號 引數和描述
1

imap_stream (必填)

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

2

msg (必填)

這是一個表示郵件/郵件編號的整數值,即將被標記為刪除。

3

options(可選)

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

  • FT_UID

  • FT_PREFETCHTEXT

  • FT_INTERNAL

返回值

此函式返回一個表示指定郵件/郵件報頭的字串值。

PHP 版本

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

示例

以下是 imap_fetchheader() 函式的示例:−

<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_fetchheader($imap, 3);
         print_r($body);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:−

Connection established ....
Contents of the first message:
Delivered-To: tutorialspoint.test@gmail.com Received: by 2002: a05: 6214: 1625: 
0: 0: 0: 0 with SMTP id e5csp4406831qvw; Mon, 26 Oct 2020 00:01:56 −0700 
(PDT) X-Received: by 2002: a05: 6830: 1308 :: with SMTP id 
p8mr1957837otq.330.1603695716288; Mon, 26 Oct 2020 00:01:56 −0700 (PDT) ARC
−Seal: i = 1; a = rsa−sha256; t = 1603695716; cv = none; d = 
google.com; s = arc-20160816; b = 
Xm8jmMAo9BHvDPfEAHphx6Siu3z1V6Kp9EBFAlZ8pq5efBjIEA / zBXvKTzf4XVct + v +  . . . . 
. . . . . . . . . . . 

示例

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

<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>");
		 
         $emailData = imap_search($imap, '');
         foreach ($emailData as $msg) {
            $header = imap_fetchheader($imap, $msg);
            print($header);
            print("<br>");              
         }    	     
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:−

Connection established....
Contents of the first message:
Delivered-To: tutorialspoint.test@gmail.com Received: by 
2002:a05:6214:1625:0:0:0:0 with SMTP id e5csp1568707qvw; Thu, 22 Oct 2020 
07:40:56 -0700 (PDT) X-Received: by 2002:aca:750b:: with SMTP id 
q11mr1798627oic.163.1603377656521; Thu, 22 Oct 2020 07:40:56 -0700 (PDT) 
ARC-Seal: i=1; a=rsa-sha256; t=1603377656; cv=none; d=google.com; s=arc-20160816; 
b=zSBoZxFjMeKTU6s4IfuwImWLA+EHPl+rbEcemnErz+th7yY5+qDdO3bqR7R14ZOQwf

示例

以下是上述函式使用可選引數的示例:−

<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_fetchheader($imap, imap_uid($imap, 7), FT_UID);
         print_r($body);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

這將生成以下輸出:−

Connection established ....
Contents of the first message:
Delivered-To: tutorialspoint.test@gmail.com Received: by 2002: a05: 6214: 1625: 
0: 0: 0: 0 with SMTP id e5csp4406831qvw; Mon, 26 Oct 2020 00:01:56 −0700 
(PDT) X−Received: by 2002: a05: 6830: 1308 :: with SMTP id 
p8mr1957837otq.330.1603695716288; Mon, 26 Oct 2020 00:01:56 −0700 (PDT) 
ARC-Seal: i = 1; a = rsa-sha256; t = 1603695716; cv = none; d = google.com; s = 
arc-20160816; b = Xm8jmMAo9BHvDPfEAHphx6Siu3z1V6Kp9EBFAlZ8pq5efBjIEA / 
zBXvKTzf4XVct + v +  . . . . . . . . . . . . . . . 
php_function_reference.htm
廣告