PHP - Fonction imap_fetchheader ()

Les fonctions PHP-IMAP vous aident à accéder aux comptes de messagerie, IMAP signifie IInternet Mail Aaccès Protocol en utilisant ces fonctions, vous pouvez également travailler avec les protocoles NNTP, POP3 et les méthodes d'accès aux boîtes aux lettres locales.

le imap_fetchheader() La fonction accepte une valeur de ressource représentant un flux IMAP, une valeur entière représentant un message dans la boîte aux lettres comme paramètres et, récupère l'en-tête du message spécifié.

Syntaxe

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

Paramètres

Sr. Non Paramètre et description
1

imap_stream (Mandatory)

Il s'agit d'une valeur de chaîne représentant un flux IMAP, valeur de retour du imap_open() fonction.

2

msg (Mandatory)

Il s'agit d'une valeur entière représentant le numéro du message / e-mail, à marquer pour suppression.

3

options(Optional)

Il s'agit d'un paramètre facultatif qui peut être un ou plusieurs des éléments suivants -

  • FT_UID

  • FT_PREFETCHTEXT

  • FT_INTERNAL

Valeurs de retour

Cette fonction renvoie une valeur de chaîne représentant l'en-tête du courrier / message spécifié.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 4 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

Voici un exemple de la fonction 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>

Production

Cela générera la sortie suivante -

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 +  . . . . 
. . . . . . . . . . .

Exemple

Ceci est un autre exemple de cette fonction -

<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>

Production

Cela générera la sortie suivante -

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

Exemple

Voici un exemple de la fonction ci-dessus avec des paramètres optionnels -

<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>

Cela générera la sortie suivante -

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 +  . . . . . . . . . . . . . . .