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 = "[email protected]";
         $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: [email protected] 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 = "[email protected]";
         $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: [email protected] 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 = "[email protected]";
         $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: [email protected] 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
广告