<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.code-web.org/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Code-Web.org - messagerie</title>
  <link>http://www.code-web.org/</link>
  <description>Développement Web en toute liberté!</description>
  <language>fr</language>
  <pubDate>Thu, 21 Aug 2008 18:05:39 +0200</pubDate>
  <copyright></copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>L'envoit d'e-mail avec PHP c'est simple comme PHPMailer</title>
    <link>http://www.code-web.org/post/2007/11/20/27-l-envoit-d-e-mail-avec-php-c-est-simple-comme-phpmailer</link>
    <guid isPermaLink="false">urn:md5:0913f16906b73e097a051c3b566f517b</guid>
    <pubDate>Tue, 20 Nov 2007 13:15:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>PHP</category>
        <category>automatisation</category><category>messagerie</category><category>php</category><category>ressources</category>    
    <description>&lt;p&gt;Voyant beaucoup de personnes rencontrer des problèmes pour l'envoi d'e-mail en PHP, je vous propose dans cet article de découvrir la classe PHPMailler rendant la tâche beaucoup plus simple. Bien sur, envoyer un simple e-mail au format texte n'a rien de compliqué. Toutefois, lorsqu'il s'agit de l'envoyer au format HTML, ou encore d'y joindre un fichier, certains si perdent. PHPMailer apporte une solution simple et efficace.&lt;/p&gt;    &lt;h3&gt;Pour commencer&lt;/h3&gt;

&lt;p&gt;Avant toute chose, vous allez devoir &lt;a href=&quot;http://sourceforge.net/project/showfiles.php?group_id=26031&quot; hreflang=&quot;fr&quot;&gt;télécharger PHPMailer&lt;/a&gt;. Une fois l'archive décompressée, vous trouverez plusieurs fichiers dont class.phpmailer.php. C'est celui-ci qui nous intéressera. Il contient toute la définition de la classe PHPMailer. Tout code faisant appel à cette classe devra donc commencer par les instructions suivantes&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
   &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'class.phpmailer.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;h3&gt;Un premier mail&lt;/h3&gt;

&lt;p&gt;Nous allons tenter d'envoyer un premier e-mail grâce à cette classe. Vous allez tout de suite comprendre à quel point il est simple de l'utiliser.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On va chercher la définition de la classe&lt;/span&gt;
   &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'class.phpmailer.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On créé une nouvelle instance de la classe&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; PHPMailer&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// De qui vient le message, e-mail puis nom&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;From&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;moi@vous.la&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;FromName&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Nom Prénom&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Définition du sujet/objet&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;Subject&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Un mail envoyé avec PHPMailer!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit le corps du message&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;Body&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Bonjour!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Il reste encore à ajouter au moins un destinataire&lt;/span&gt;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// (ou plus, par plusieurs appel à cette méthode)&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;AddAddress&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;hey@ho.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;HeyHo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Pour finir, on envoi l'e-mail&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;send&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Dans cette exemple, nous avons envoyé un message simple en format texte. Comme vous pouvez le voir, l'envoi est tout de suite plus clair. Il n'est pas nécessaire de créer les entêtes du message. De plus, les noms d'attributs et méthodes sont très clairs.&lt;/p&gt;


&lt;h3&gt;Un mail au format HTML&lt;/h3&gt;

&lt;p&gt;On va maintenant voir à quel point cette classe est intéressante. Avec un exemple d'envoi d'e-mail au format HTML. Là encore, rien de bien compliqué, et c'est justement tout l'intérêt de cette classe!&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On va chercher la définition de la classe&lt;/span&gt;
   &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'class.phpmailer.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On créé une nouvelle instance de la classe&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; PHPMailer&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// De qui vient le message, e-mail puis nom&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;From&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;moi@vous.la&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;FromName&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Nom Prénom&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Définition du sujet/objet&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;Subject&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Un mail envoyé avec PHPMailer!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On lit le contenu d'une page html&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$body&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;file_get_contents&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'page_mail.html'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit le contenu de cette page comme message&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;MsgHTML&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$body&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On pourra définir un message alternatif pour les boîtes de&lt;/span&gt;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// messagerie n'acceptant pas le html&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;AltBody&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Ce message est au format HTML, votre messagerie n'accepte pas ce format.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Il reste encore à ajouter au moins un destinataire&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;AddAddress&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;hey@ho.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;HeyHo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Pour finir, on envoi l'e-mail&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;send&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Notez qu'on ne se pose toujours pas la question de la formation des entêtes. On pourra tout de même se poser la question du jeu de caractères (charset). Par défaut, il est définit à ISO-8859-1. Mais il est tout à fait possible de le changer avec l'instruction suivante&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Pour le passer en UTF-8 par exemple :&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;CharSet&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'UTF-8'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;h3&gt;Un mail avec pièce jointe&lt;/h3&gt;

&lt;p&gt;Voilà une question très fréquente dans les forums de programmation PHP, comment envoyer un e-mail avec une pièce jointe? PHPMailer propose là encore une solution simple et efficace. Imaginons que l'on souhaite envoyer notre CV par e-mail&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On va chercher la définition de la classe&lt;/span&gt;
   &lt;span style=&quot;color: #b1b100;&quot;&gt;require&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'class.phpmailer.php'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On créé une nouvelle instance de la classe&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; PHPMailer&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// De qui vient le message, e-mail puis nom&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;From&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;moi@vous.la&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;FromName&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Nom Prénom&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Définition du sujet/objet&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;Subject&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Un mail envoyé avec PHPMailer!&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On définit le corps du message&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;Body&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Bonjour!
Veuillez trouver ci-joint mon CV.
&amp;nbsp;
Cordialement,
Machin truc.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Il reste encore à ajouter au moins un destinataire&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;AddAddress&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;recrutement@ho.com&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;HeyHo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On met notre CV en pièce jointe&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;AddAttachment&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;'./CV.pdf'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Pour finir, on envoi l'e-mail&lt;/span&gt;
   &lt;span style=&quot;color: #ff0000&quot;&gt;$mail&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;send&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Vous remarquerez encore une fois que l'envoi de pièce jointe est considérablement simplifié en utilisant PHPMailer.&lt;/p&gt;


&lt;h3&gt;Le mot de la fin&lt;/h3&gt;

&lt;p&gt;Pour aller plus loin, rien de mieux que de vous rendre sur le &lt;a href=&quot;http://phpmailer.codeworxtech.com/&quot; hreflang=&quot;en&quot;&gt;site officiel de PHPMailer&lt;/a&gt;, en anglais. Vous y trouverez une documentation très complète sur l'utilisation de cette classe et les différentes possibilités qui vous sont offertes.&lt;/p&gt;


&lt;p&gt;Ce qui est certain, c'est qu'il ne faut pas se priver de l'utiliser. Vous gagnerez du temps dans vos développements.&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2007/11/20/27-l-envoit-d-e-mail-avec-php-c-est-simple-comme-phpmailer#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2007/11/20/27-l-envoit-d-e-mail-avec-php-c-est-simple-comme-phpmailer#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/12</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=12">
								<!-- # If the subscriber is logged in -->
																<!-- # If the subscriber is not logged in -->
																	S'abonner pour recevoir les commentaires suivants par email															</a>
						</p>
					  </item>
    
  <item>
    <title>Introduction aux fonctions IMAP</title>
    <link>http://www.code-web.org/post/2007/11/11/21-introduction-aux-fonctions-imap</link>
    <guid isPermaLink="false">urn:md5:beab0a40b76621e1535bc2306a654203</guid>
    <pubDate>Sun, 11 Nov 2007 00:02:00 +0000</pubDate>
    <dc:creator>Thierry Geindre</dc:creator>
        <category>PHP</category>
        <category>messagerie</category><category>php</category>    
    <description>&lt;p&gt;Il y à bien peu de clients de messagerie en PHP qui valent le détour. C'est pourquoi je vous propose ici une introduction, et seulement un introduction, aux fonctions IMAP proposées par PHP et permettant la gestion complète d'un serveur de messagerie IMAP ou POP3.&lt;/p&gt;    &lt;h3&gt;Introduction&lt;/h3&gt;

&lt;p&gt;Nous allons étudier, dans cette article, les fonctions IMAP de PHP. Avant d'aller plus loin, notez que de bonnes connaissances sur l'architecture client/serveur vous serons très utiles si vous souhaitez utiliser ces fonctions. Comme indiqué dans le titre de l'article, nous nous limiterons ici à une simple introduction. Ainsi, nous verrons comment établir la connexion au serveur puis comment récupérer la liste des messages disponibles sur celui-ci.&lt;/p&gt;


&lt;h3&gt;Quelques bases sur les serveurs de messageries&lt;/h3&gt;

&lt;p&gt;Comme tout serveur qui se respecte, les communications avec les serveurs de messageries se font selon un protocole bien définit. Dans notre cas, il existe deux protocoles&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;IMAP&lt;/li&gt;
&lt;li&gt;POP3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avant d'établir une connexion à un serveur de messagerie, vous devez impérativement connaître le protocole que celui-ci utilise sans quoi la connexion en question échouerait.&lt;/p&gt;


&lt;p&gt;Lors de l'établissement d'une connexion, vous pouvez sélectionner un dossier. Il s'agit du dossier dans lequel vous souhaitez lire les mails. Il est bien sur possible d'en changer au cours de la connexion. Au moment de la première connexion, il est impossible de connaître la liste exacte des dossiers disponibles sur le serveur puisque l'utilisateur du compte que nous allons utiliser peut en avoir créer comme bon lui semble. Toutefois, il existe toujours le dossier INBOX, qui correspond au dossier où les messages entrant arrivent par défaut. Nous sélectionnerons donc toujours ce dossier lors d'une première connexion.&lt;/p&gt;


&lt;h3&gt;Première connexion&lt;/h3&gt;

&lt;h4&gt;La fonction imap_open&lt;/h4&gt;

&lt;p&gt;Avant d'entamer la connexion à proprement parlé, nous allons étudier la fonction imap_open. Cette fonction admet des paramètres bien particuliers que nous allons étudier ici. Vous pouvez consulter la &lt;a href=&quot;http://fr.php.net/imap_open&quot; hreflang=&quot;fr&quot;&gt;documentation officielle de cette fonction&lt;/a&gt; si vous le souhaitez.&lt;/p&gt;


&lt;p&gt;resource imap_open ( string $mailbox, string $username, string $password [, int $options [, int $n_retries]] )&lt;/p&gt;


&lt;p&gt;Comme le montre la signature de cette fonction, trois paramètres sont obligatoires. Nous nous limiterons à ces paramètres, pour les autres je vous invite une nouvelle fois à vous reporter à la &lt;a href=&quot;http://fr.php.net/imap_open&quot; hreflang=&quot;fr&quot;&gt;documentation officielle&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;$mailbox&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;Il s'agit de la boîte mail que nous souhaitons consulter. C'est une chaîne de caractères constituée de&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;L'adresse du serveur, exemple&amp;nbsp;: pop3.free.fr&lt;/li&gt;
&lt;li&gt;Du port utilisé, exemple&amp;nbsp;: 143&lt;/li&gt;
&lt;li&gt;Du protocole, exemple&amp;nbsp;: POP3&lt;/li&gt;
&lt;li&gt;Du dossier à utiliser par défaut, généralement INBOX&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cette chaîne de caractère doit être construire ainsi&amp;nbsp;: {adresseServeur:port/protocole}Dossier&lt;/p&gt;


&lt;p&gt;Un exemple sera certainement plus parlant. Imaginons que nous souhaitions établir une connexion sur le serveur pop3.free.fr sur le port 143 en utilisant le protocole POP3 (et en utilisant INBOX comme dossier par défaut), on passera en paramètre la chaîne suivante&amp;nbsp;: {pop3.free.fr:143/pop3}INBOX&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;$username&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;Il s'agit du nom d'utilisateur utilisé pour s'identifier sur le serveur. Classiquement, on utilise l'adresse e-mail de l'utilisateur. Il peut également s'agir du nom seul (sans @domaine.ext). Exemple&amp;nbsp;: mon.nom@free.fr&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;$password&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;Il s'agit du mot de passe à utiliser pour l'identification, une chaîne de caractère correspondant au mot de passe du l'utilisateur indiquée dans le paramètre précédent.&lt;/p&gt;


&lt;h4&gt;Première connexion, c'est parti!&lt;/h4&gt;

&lt;p&gt;Nous allons pouvoir établir notre première connexion au serveur pop3.free.fr (par exemple) avec le code suivant&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
imap_open&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;{pop3.free.fr:143/pop3}INBOX&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;mon.nom@free.fr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;monMDP&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ce code est incomplet. Il ne permet pas d'une part de savoir si la connexion a bien été établie et d'autre part il ne ferme pas la connexion. Nous allons donc utiliser la valeur renvoyée par imap_open() pour vérifier que la connexion a bien été établie. Pour cela il faut savoir que deux types de valeur sont possibles&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Un booléen à FALSE indiquant que la connexion a échouée.&lt;/li&gt;
&lt;li&gt;Un &lt;a href=&quot;http://www.php.net/manual/fr/language.types.resource.php&quot; hreflang=&quot;fr&quot;&gt;type ressource&lt;/a&gt; contenant le lien vers la connexion au serveur. Il faudra conserver cette ressource pour la passer en paramètre à d'autres fonctions imap_*.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Tentative de connexion&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; imap_open&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;{pop3.free.fr:143/pop3}INBOX&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;mon.nom@free.fr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;monMDP&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Test sur le retour de la fonction imap_open()&lt;/span&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Echec&lt;/span&gt;
      &lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;La connexion a échouée.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Connexion établie&lt;/span&gt;
      &lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Connexion établie, fermeture de la connexion.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On utilise imap_close() pour fermer la connexion.&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On lui passe en paramètre la ressource retournée par imap_open()&lt;/span&gt;
      imap_close&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Pour tester ce code, pensez à remplacer les paramètres indiqués ici par ceux correspondant au serveur de votre choix et au compte correspondant. Si vous voyez enfin apparaître le texte &quot;Connexion établie, fermeture de la connexion.&quot;, vous avez établie votre connexion à un serveur de messagerie avec succès.&lt;/p&gt;


&lt;h3&gt;Récupérer les informations concernant la boîte de messagerie&lt;/h3&gt;

&lt;p&gt;Une fois la connexion établie, nous allons récupérer quelques informations concernant la boîte de messagerie. Vous comprendrez pourquoi par la suite. Pour celà, nous utiliserons la fonction imap_check().&lt;/p&gt;


&lt;p&gt;object imap_check ( resource $imap_stream )&lt;/p&gt;


&lt;p&gt;Comme vous pouvez le voir, cette fonction n'accepte qu'un seul paramètre (obligatoire) qui correspond à la ressource retournée par imap_open(). imap_check() retourne également un objet ayant pour attributs les différentes informations recherchées.&lt;/p&gt;


&lt;p&gt;Grâce au code suivant, nous allons récupérer les informations concernant les boîte de messagerie située sur le serveur pop3.free.fr.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Tentative de connexion&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; imap_open&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;{pop3.free.fr:143/pop3}INBOX&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;mon.nom@free.fr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;monMDP&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Test sur le retour de la fonction imap_open()&lt;/span&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Echec&lt;/span&gt;
      &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;La connexion a échouée.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Fin du script&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Connexion établie&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Connexion établie, lecture des informations :&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On récupére les informations&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$mailBoxInfos&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; imap_check&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On affiche ces informations&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;lt;pre&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;print_r&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$mailBoxInfos&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;lt;/pre&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On utilise imap_close() pour fermer la connexion.&lt;/span&gt;
imap_close&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Une nouvelle, fois, n'oubliez pas de remplacer les paramètres pour vos tests.&lt;/p&gt;


&lt;p&gt;Si tout a bien fonctionné, vous devriez vous apparaître quelque chose comme ceci&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt;Connexion établie, lecture des informations :
stdClass Object
(
    [Date] =&amp;gt; Sat, 10 Nov 2007 21:29:32 +0100 (CET)
    [Driver] =&amp;gt; pop3
    [Mailbox] =&amp;gt; {pop3.free.fr:143/pop3/user=&quot;mon.nom@free.fr&quot;}INBOX
    [Nmsgs] =&amp;gt; 9
    [Recent] =&amp;gt; 9
)&lt;/pre&gt;


&lt;p&gt;L'objet retourné contient donc 5 attributs&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Date, qui correspond à la date de dernière modification du contenu de la boîte de messagerie.&lt;/li&gt;
&lt;li&gt;Driver, qui correspond au protocole utilisé.&lt;/li&gt;
&lt;li&gt;Mailbox, la boîte de messagerie courante.&lt;/li&gt;
&lt;li&gt;Nmsgs, qui correspond au nombre de message dans le dossier courant (INBOX) dans la boîte courante.&lt;/li&gt;
&lt;li&gt;Recent, le nombre de nouveaux messages, non lus.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;C'est l'attribut Nmsgs qui nous intéressera pour obtenir la liste des messages présents dans le dossier INBOX. Vous allez comprendre tout de suite.&lt;/p&gt;


&lt;h3&gt;Récupération de la liste des messages&lt;/h3&gt;

&lt;p&gt;Nous y voilà! Avec tout ce nous avons vu et les informations que nous sommes en mesure de récupérer sur le serveur, nous allons enfin pouvoir récupérer la liste des messages présents sur le serveur dans le dossier courant, INBOX.&lt;/p&gt;


&lt;p&gt;Pour cela, nous allons utiliser la fonction&amp;nbsp;:&lt;/p&gt;


&lt;p&gt;array imap_fetch_overview ( resource $imap_stream, string $sequence [, int $options] )&lt;/p&gt;


&lt;p&gt;Là encore nous ne parlerons que des paramètres obligatoires.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;$imap_stream&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;Il s'agit toujours de la ressource contenant le lien vers la connexion au serveur de messagerie.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;$sequence&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;Vous devrez indiquer dans ce paramètre, sous la forme d'une chaîne de caractère, le numéro du message de départ et le numéro du message de fin que vous souhaitez obtenir. Les messages, sur le serveur, sont numérotés de 0 à NombreDeMessageTotal. Pour obtenir la liste complète des messages présents dans le dossier courant, il nous faut donc connaître à l'avance le nombre de messages qu'il contient. C'est précisément ce que nous avons fait avec la fonction imap_check au paragraphe précédent.&lt;/p&gt;


&lt;p&gt;En somme nous devons indiquer l'interval de messages que nous souhaitons obtenir. L'intérêt de ce système n'est peut être pas évident au premier abord mais imaginons que nous ayons mis en place un système de pagination dans notre liste de messages. Il serait totalement inutile de télé charger les informations concernant tout les message alors que seul ceux qui seront affichés sur la page courante nous intéresse.&lt;/p&gt;


&lt;p&gt;La syntaxe de ce paramètre est la suivante&amp;nbsp;: NumDepart:NumFin&lt;/p&gt;


&lt;p&gt;Pour obtenir les messages du numéro 0 au numéro 10 on passera donc en paramètre la chaîne suivante&amp;nbsp;: 0:10&lt;/p&gt;


&lt;p&gt;Voici maintenant le dernier code que nous verrons dans cet article, il affiche la liste des messages présents sur le serveur. Pour en savoir plus sur ces informations, reportez-vous à la &lt;a href=&quot;http://fr.php.net/imap_fetch_overview&quot; hreflang=&quot;fr&quot;&gt;documentation officielle&lt;/a&gt;.&lt;/p&gt;

&lt;pre class=&quot;php&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Tentative de connexion&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; imap_open&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;{pop3.free.fr:143/pop3}INBOX&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;mon.nom@free.fr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;monMDP&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Test sur le retour de la fonction imap_open()&lt;/span&gt;
&lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
      &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Echec&lt;/span&gt;
      &lt;span style=&quot;color: #000066;&quot;&gt;die&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;La connexion a échouée.&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt; &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Fin du script&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Connexion établie&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Connexion établie, lecture des messages :&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On récupère les informations&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$mailBoxInfos&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; imap_check&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On récupère la liste des messages&lt;/span&gt;
&lt;span style=&quot;color: #ff0000&quot;&gt;$mailList&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt; imap_fetch_overview&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0:&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$mailBoxInfos&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #006600;&quot;&gt;Nmsgs&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On affiche la liste des messages&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;lt;pre&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;print_r&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$mailList&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;echo&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;'&amp;lt;/pre&amp;gt;'&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On utilise imap_close() pour fermer la connexion.&lt;/span&gt;
imap_close&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000&quot;&gt;$imapLink&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/pre&gt;


&lt;h3&gt;Pour aller plus loin&lt;/h3&gt;

&lt;p&gt;Comme très souvent, rien ne vaut la &lt;a href=&quot;http://fr.php.net/imap&quot; hreflang=&quot;fr&quot;&gt;documentation officielle de PHP&lt;/a&gt;. Malheureusement, il existe très peu de tutoriaux sur le sujet et il sera probablement difficile d'en trouver de bien complets. Cela-dit, rien ne vous empêche de faire vos propres test ou encore de regarder dans les sources de clients de messagerie tels que &lt;a href=&quot;http://www.horde.org/&quot; hreflang=&quot;en&quot;&gt;Horde&lt;/a&gt;.&lt;/p&gt;


&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;J'espère simplement que cette petite introduction, si légère soit-elle, vous aura donné l'envie de développer un bon p'tit client de messagerie en PHP. Si c'est le cas, n'hésitez pas à m'en parler par mail, je consacrerait avec plaisir un article à vos développements. Mais, et je le rappel une nouvelle fois, les commentaires ne sont pas un espace publicitaire!&lt;/p&gt;</description>
    
    
    
          <comments>http://www.code-web.org/post/2007/11/11/21-introduction-aux-fonctions-imap#comment-form</comments>
      <wfw:comment>http://www.code-web.org/post/2007/11/11/21-introduction-aux-fonctions-imap#comment-form</wfw:comment>
      <wfw:commentRss>http://www.code-web.org/feed/rss2/comments/6</wfw:commentRss>
    						<h3>Abonnement aux commentaires</h3>
						<p>
							<a href="http://www.code-web.org/subscribetocomments?post_id=6">
								<!-- # If the subscriber is logged in -->
																<!-- # If the subscriber is not logged in -->
																	S'abonner pour recevoir les commentaires suivants par email															</a>
						</p>
					  </item>
    
</channel>
</rss>