Exemple imap pour lecture de mail

Contenu du snippet

Salut,

Je me lance dans ma première publication. Ce code permet de consulter les mails d'un compte par les primitives IMAP de PHP.

C'est un exemple tout simple pour comprendre le fonctionnement, ca n'a pas pour ambition d'etre un client webmail. Mais c'est realisable... Une idée pour le LABO si certains sont interressés pour la realisation d'un webmail, je suis partant !

@+
Boris

//Un peu de personalisation avant de l'executer :
// IPHOST = IP du serveur ou nom
// LOGIN = profil de connexion
// PASS = mot de passe

Source / Exemple :


<html>
<head><title>Exemple IMAP Webmail</title></head>
<body>
<?php

function get_mime_type(&$structure)
{
   $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
   if($structure->subtype) {
   	return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
   }
   	return "TEXT/PLAIN";
}

function get_part($stream, $msg_number, $mime_type, $structure = false,$part_number = false)
{

// CETTE FONCTION PERMET DE RECUPERER UNIQUEMENT LE TEXTE DU MAIL (PAS LES PIECES JOINTES)

   	if(!$structure) {
   		$structure = imap_fetchstructure($stream, $msg_number);
   	}
   	if($structure) {
   		if($mime_type == get_mime_type($structure)) {
   			if(!$part_number) {
   				$part_number = "1";
   			}
   			$text = imap_fetchbody($stream, $msg_number, $part_number);
   			if($structure->encoding == 3) {
   				return imap_base64($text);
   			} else if($structure->encoding == 4) {
   				return imap_qprint($text);
   			} else {
   			return $text;
   		}
   	}

		if($structure->type == 1) /* multipart */ {
   		while(list($index, $sub_structure) = each($structure->parts)) {
   			if($part_number) {
   				$prefix = $part_number . '.';
   			}
   			$data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix .    ($index + 1));
   			if($data) {
   				return $data;
   			}
   		} // END WHILE
   		} // END MULTIPART
   	} // END STRUTURE
   	return false;
} // END FUNCTION

echo "<table width='750' border='0' rules='none' height='16'>
<HR>";

$colorFond = "#CCCCCC";				

echo "<tr bgcolor='#FFFFFF'>";
echo "<td width='10' bgcolor=$colorFond></td>";
echo "<td width='80' bgcolor=$colorFond colspan ='2'>Date</td>";
echo "<td width='50' bgcolor=$colorFond>Email</td>";
echo "<td width='60' bgcolor=$colorFond>Objet</td>";
echo "<td width='190' bgcolor=$colorFond>Mail</td>";
echo "</tr>";

  $mbox = imap_open ("{IPHOST:110/pop3}", "LOGIN", "PASS");	// CONNEXION AU SERVEUR POP 
    echo "<p><h1>Gestion des email</h1>\n";	
    $headers = imap_headers ($mbox);				// RECUPERATION DES ENTETES
    if ($headers == false) {
      echo "Pas de mail en cours !\n";				// PAS DE MAILS :-(
    } else {
      while (list ($key,$val) = each ($headers)) {		// ON PASSE TOUS LES MAILS EN REVUE

	  $header = imap_headerinfo($mbox, $key+1, 100, 100);
          $dateMail = date("d/m/Y", $header->udate);
          $from = $header->from;
	  $objetMail = $header->fetchsubject;
	  $objetMail = imap_utf8($objetMail);			// PERMET DE NE PAS AVOIR ISO=... pour les caracteres accentues
	  $objetMail = utf8_decode($objetMail);			// PERMET DE NE PAS AVOIR ISO=... pour les caracteres accentues
	  $emailExp = $from[0]->mailbox."@".$from[0]->host;
          $corpsMail = get_part ($mbox, $key+1, "TEXT/PLAIN");
          $corpsMail = imap_utf8($corpsMail);			// PERMET DE NE PAS AVOIR ISO=... pour les caracteres accentues
	  $corpsMail = utf8_decode($corpsMail);			// PERMET DE NE PAS AVOIR ISO=... pour les caracteres accentues

          $idtMail= $key+1;
		
	  // AFFICHAGE DES MAILS DANS UN TABLEAU

          echo "<tr bgcolor='#FFFFFF'>";
          echo "<td width='10' bgcolor=$colorFond><img src='".$rep."image/lettre.gif' border='0'></td>";
	  echo "<td width='90' bgcolor=$colorFond><font face='Arial' size='2'>$dateMail</font></td>";
	  echo "<td width='50' bgcolor=$colorFond><font face='Arial' size='2'>$emailExp</font></td>";
	  echo "<td width='60' bgcolor=$colorFond><font face='Arial' size='2'>$objetMail</font></td>";
	  echo "<td width='190' bgcolor=$colorFond><font face='Arial' size='2'>$corpsMail</font></td>";
  	  echo "</tr>";

      }
    }

  imap_close($mbox);

?>

</body>
</html>

Conclusion :


Amélioration à venir : fonction de suppression d'un mail, gestion des pieces jointes.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.