Problème 2 pieces jointes php

Résolu
Bonjour,

Voilà mon problème : j'arrive à envoyer une pièce jointe mais lorsque je veux en envoyer une deuxième dans le même mail ca ne marche pas.

Ca ne joint qu'un seul fichier ! Comment faire ?

Voici le bout de mon code traitant l'envoi du mail avec les pièces jointes :

$boundary = "-----=".md5(uniqid(rand()));

	$header = "MIME-Version: 1.0\r\n";
	$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
	$header .= "\r\n";

	$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";

	$msg .= "--$boundary\r\n";

	$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
	$msg .= "Content-Transfer-Encoding:8bit\r\n";
	$msg .= "\r\n";
	$msg .= "Un client vient d'effectuer une réservation.";
	$msg .= "\r\n";


	$file0 = "contrat.pdf";
	$file1 = "facture.pdf";
	
	$fp0 = fopen($file0, "rb");
	$fp1 = fopen($file1, "rb");
	
	$attachment0 = fread($fp0, filesize($file0));
	$attachment1 = fread($fp1, filesize($file1));

	fclose($fp0);
	fclose($fp1);


	$attachment0 = chunk_split(base64_encode($attachment0));
	$attachment1 = chunk_split(base64_encode($attachment1));

	$msg .= "--$boundary\r\n";
	
	$msg .= "Content-Type: pdf; name=\"$file0\"\r\n";
	$msg .= "Content-Type: pdf; name=\"$file1\"\r\n";
	
	$msg .= "Content-Transfer-Encoding: base64\r\n";
	
	$msg .= "Content-Disposition: attachement; filename=\"$file0\"\r\n";
	$msg .= "Content-Disposition: attachement; filename=\"$file1\"\r\n";
	
	$msg .= "\r\n";

	$msg .= $attachment0."\r\n";
	$msg .= $attachment1."\r\n";
	
	$msg .= "\r\n\r\n";

	$msg .= "--$boundary--\r\n";

	$destinataire = "crivelli.lucas@gmail.com";
	$expediteur   = "info@easy-park.ch";
	$reponse      = $expediteur;
	$sujet = "Rapport de réservation";

	mail($destinataire, $sujet, $msg,"Reply-to: $reponse\r\nFrom: ".$expediteur."\r\n".$header);<code>
</code>

mais ca ne marche pas.

merci pour votre aide...
Configuration: Mac OS X
Safari 525.27.1

14 réponses

  1. J'ai moi-même trouvé la réponse :

    Voici le code de base pour envoyer deux pièces jointes :

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/plain; charset=iso-8859-1" />
    <title>Document sans titre</title>
    </head>
    
    <body>
    
    <?php
    
    	mysql_connect('...','...','...');
    	mysql_select_db('...');
    
    	
    	//******************mail	
    	$boundary = "-----=".md5(uniqid(rand()));
    
    	$header = "MIME-Version: 1.0\r\n";
    	$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
    	$header .= "\r\n";
    
    	$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
    
    	$msg .= "--$boundary\r\n";
    
    	$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
    	$msg .= "Content-Transfer-Encoding:8bit\r\n";
    	$msg .= "\r\n";
    	
    	$msg .= "BLABLABLA.";
    	$msg .= "\r\n";
    	
    	$file = "fichier1.pdf";
    	$fp = fopen($file, "rb");
    	$attachment = fread($fp, filesize($file));
    	fclose($fp);
    	$attachment = chunk_split(base64_encode($attachment));
    
    	//piece jointe fichier1
    	$msg .= "--$boundary\r\n";
    
    	$msg .= "Content-Type: pdf; name=\"$file\"\r\n";
    	$msg .= "Content-Transfer-Encoding: base64\r\n";
    	$msg .= "Content-Disposition: inline; filename=\"$file\"\r\n";
    
    	$msg .= "\r\n";
    
    	$msg .= $attachment."\r\n";
    	$msg .= "\r\n\r\n";
    
    	$file = "fichier2.pdf";
    	$fp = fopen($file, "rb");
    	$attachment = fread($fp, filesize($file));
    	fclose($fp);
    	$attachment = chunk_split(base64_encode($attachment));
    
    	//piece jointe fichier2
    	$msg .= "--$boundary\r\n";
    
    	$msg .= "Content-Type: pdf; name=\"$file\"\r\n";
    	$msg .= "Content-Transfer-Encoding: base64\r\n";
    	$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
    
    	$msg .= "\r\n";
    
    	$msg .= $attachment."\r\n";
    	$msg .= "\r\n\r\n";
    	
    	$msg .= "--$boundary--\r\n";
    
    	$destinataire = "monadresse@gmail.com";
    	$expediteur   = "adresse@expediteur";
    	$reponse      = $expediteur;
    	$sujet = "Sujet";
    
    	mail($destinataire, $sujet, $msg,"Reply-to: $reponse\r\nFrom: ".$expediteur."\r\n".$header);
    	//***********************mail
    	
    	
    	mysql_close();
    	
    ?>
    </body>
    </html>


    Voila ce qui à résolu mon problème. Comme l'a dit Mathieu (presque juste) il y avait
    $msg .= "--$boundary--\r\n";
    	
    	//piece jointe FACTURE
    	$msg .= "--$boundary\r\n";


    en trop. Comme l'a dit également Mathieu il faut traiter chaque pj comme si il n'y en avait qu'une. et dèrnière astuce. Il faut mettre la première en inline et la deuxième en attachment.

    Bonne continuation à tous. Lucas

    PS : merci mathieu
    1. J'ai moi-même trouvé la réponse :

      Voici le code de base pour envoyer deux pièces jointes :

      
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <meta http-equiv="Content-Type" content="text/plain; charset=iso-8859-1" />
      <title>Document sans titre</title>
      </head>
      
      <body>
      
      <?php
      
      	mysql_connect('...','...','...');
      	mysql_select_db('...');
      
      	
      	//******************mail	
      	$boundary = "-----=".md5(uniqid(rand()));
      
      	$header = "MIME-Version: 1.0\r\n";
      	$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
      	$header .= "\r\n";
      
      	$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
      
      	$msg .= "--$boundary\r\n";
      
      	$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
      	$msg .= "Content-Transfer-Encoding:8bit\r\n";
      	$msg .= "\r\n";
      	
      	$msg .= "BLABLABLA.";
      	$msg .= "\r\n";
      	
      	$file = "fichier1.pdf";
      	$fp = fopen($file, "rb");
      	$attachment = fread($fp, filesize($file));
      	fclose($fp);
      	$attachment = chunk_split(base64_encode($attachment));
      
      	//piece jointe fichier1
      	$msg .= "--$boundary\r\n";
      
      	$msg .= "Content-Type: pdf; name=\"$file\"\r\n";
      	$msg .= "Content-Transfer-Encoding: base64\r\n";
      	$msg .= "Content-Disposition: inline; filename=\"$file\"\r\n";
      
      	$msg .= "\r\n";
      
      	$msg .= $attachment."\r\n";
      	$msg .= "\r\n\r\n";
      
      	$file = "fichier2.pdf";
      	$fp = fopen($file, "rb");
      	$attachment = fread($fp, filesize($file));
      	fclose($fp);
      	$attachment = chunk_split(base64_encode($attachment));
      
      	//piece jointe fichier2
      	$msg .= "--$boundary\r\n";
      
      	$msg .= "Content-Type: pdf; name=\"$file\"\r\n";
      	$msg .= "Content-Transfer-Encoding: base64\r\n";
      	$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
      
      	$msg .= "\r\n";
      
      	$msg .= $attachment."\r\n";
      	$msg .= "\r\n\r\n";
      	
      	$msg .= "--$boundary--\r\n";
      
      	$destinataire = "monadresse@gmail.com";
      	$expediteur   = "adresse@expediteur";
      	$reponse      = $expediteur;
      	$sujet = "Sujet";
      
      	mail($destinataire, $sujet, $msg,"Reply-to: $reponse\r\nFrom: ".$expediteur."\r\n".$header);
      	//***********************mail
      	
      	
      	mysql_close();
      	
      ?>
      </body>
      </html>


      Voila ce qui à résolu mon problème. Comme l'a dit Mathieu (presque juste) il y avait
      $msg .= "--$boundary--\r\n";
      	
      	//piece jointe FACTURE
      	$msg .= "--$boundary\r\n";


      en trop. Comme l'a dit également Mathieu il faut traiter chaque pj comme si il n'y en avait qu'une. et dèrnière astuce. Il faut mettre la première en inline et la deuxième en attachment.

      Bonne continuation à tous. Lucas

      PS : merci mathieu
      1. merci mathieu,

        je vais essayer ca.
        1. Contributeur
          au passage et après un petit tour dans le manuel, ça a bien l'air d'être ça !
          -:o)

          regarde cet exemple, il boucle sur les attachments avec boundary, content type et compagnie…
      2. UP finalement je n'ai toujours pas trouvé !!

        S'il vous plaît j'ai vraiment besoin de votre aide ... MERCI
        1. Salut Mathieu merci de ton aide,

          Voila mon code :
          <?php
          
          	mysql_connect('...','...','...');
          	mysql_select_db('...');
           
          	$n_client_login = $_SESSION['n_client_login'];
          	
          	$email = $donnees['email'];
          	
          	//******************mail	
          	$boundary = "-----=".md5(uniqid(rand()));
          
          	$header = "MIME-Version: 1.0\r\n";
          	$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
          	$header .= "\r\n";
          
          	$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
          
          	$msg .= "--$boundary\r\n";
          
          	$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
          	$msg .= "Content-Transfer-Encoding:8bit\r\n";
          	$msg .= "\r\n";
          	
          	
          	$msg .= "Ici le message.";
          	$msg .= "\r\n";
          	
          	$file1 = "contrat.pdf";
                  $file2 = "facture.pdf";
          
          	$fp = fopen($file1, "rb");
                  $fp2 = fopen($file2, "rb");
          
          	$attachment = fread($fp, filesize($file1));
          	fclose($fp);
          	$attachment = chunk_split(base64_encode($attachment));
          	
          	$attachment2 = fread($fp2, filesize($file2));
          	fclose($fp2);
          	$attachment2 = chunk_split(base64_encode($attachment2));
          
          	//piece jointe CONTRAT
          	$msg .= "--$boundary\r\n";
          
          	$msg .= "Content-Type: pdf; name=\"$file1\"\r\n";
          	$msg .= "Content-Transfer-Encoding: base64\r\n";
          	$msg .= "Content-Disposition: attachment; filename=\"$file1\"\r\n";
          
          	$msg .= "\r\n";
          
          	$msg .= $attachment."\r\n";
          	$msg .= "\r\n\r\n";
          
          	$msg .= "--$boundary--\r\n";
          	
          	//piece jointe FACTURE
          	$msg .= "--$boundary\r\n";
          	
          	$msg .= "Content-Type: pdf; name=\"$file2\"\r\n";
          	$msg .= "Content-Transfer-Encoding: base64\r\n";
          	$msg .= "Content-Disposition: attachment; filename=\"$file2\"\r\n";
          
          	$msg .= "\r\n";
          
          	$msg .= $attachment2."\r\n";
          	$msg .= "\r\n\r\n";
          
          	$msg .= "--$boundary--\r\n";
          
          	$destinataire = "blablabla.com";
          	$expediteur   = "blablabla.ch";
          	$reponse      = $expediteur;
          	$sujet = "Rapport de réservation";
          
          	mail($destinataire, $sujet, $msg,"Reply-to: $reponse\r\nFrom: ".$expediteur."\r\n".$header);
          	//***********************mail
          	
          	
          	mysql_close();
          	
          	
          
          	
          ?>


          Malheureusement ça ne marche pas. Je reçois seulement le fichier contrat.pdf.

          Merci de ton aide. Lucas
          1. Contributeur
            	$msg .= "--$boundary--\r\n";
            	
            	//piece jointe FACTURE
            	$msg .= "--$boundary\r\n";
            

            le boundary du haut n'est pas en trop ?
            normalement quand tu ajoutes les deux tirets après c'est celui qui finit le message non ?
            1. J'ai moi-même trouvé la réponse :

              Voici le code de base pour envoyer deux pièces jointes :

              <?php
              	session_start();
              ?>
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <meta http-equiv="Content-Type" content="text/plain; charset=iso-8859-1" />
              <title>Document sans titre</title>
              </head>
              
              <body>
              
              <?php
              
              	mysql_connect('...','...','...');
              	mysql_select_db('...');
              	
              	$email = $donnees['email'];
              	
              	//******************mail	
              	$boundary = "-----=".md5(uniqid(rand()));
              
              	$header = "MIME-Version: 1.0\r\n";
              	$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
              	$header .= "\r\n";
              
              	$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
              
              	$msg .= "--$boundary\r\n";
              
              	$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
              	$msg .= "Content-Transfer-Encoding:8bit\r\n";
              	$msg .= "\r\n";
              	
              	$msg .= "BLABLABLA.";
              	$msg .= "\r\n";
              	
              	$file = "fichier1.pdf";
              	$fp = fopen($file, "rb");
              	$attachment = fread($fp, filesize($file));
              	fclose($fp);
              	$attachment = chunk_split(base64_encode($attachment));
              
              	//piece jointe fichier1
              	$msg .= "--$boundary\r\n";
              
              	$msg .= "Content-Type: pdf; name=\"$file\"\r\n";
              	$msg .= "Content-Transfer-Encoding: base64\r\n";
              	$msg .= "Content-Disposition: inline; filename=\"$file\"\r\n";
              
              	$msg .= "\r\n";
              
              	$msg .= $attachment."\r\n";
              	$msg .= "\r\n\r\n";
              
              	$file = "fichier2.pdf";
              	$fp = fopen($file, "rb");
              	$attachment = fread($fp, filesize($file));
              	fclose($fp);
              	$attachment = chunk_split(base64_encode($attachment));
              
              	//piece jointe fichier2
              	$msg .= "--$boundary\r\n";
              
              	$msg .= "Content-Type: pdf; name=\"$file\"\r\n";
              	$msg .= "Content-Transfer-Encoding: base64\r\n";
              	$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
              
              	$msg .= "\r\n";
              
              	$msg .= $attachment."\r\n";
              	$msg .= "\r\n\r\n";
              	
              	$msg .= "--$boundary--\r\n";
              
              	$destinataire = "monadresse@gmail.com";
              	$expediteur   = "adresse@expediteur";
              	$reponse      = $expediteur;
              	$sujet = "Sujet";
              
              	mail($destinataire, $sujet, $msg,"Reply-to: $reponse\r\nFrom: ".$expediteur."\r\n".$header);
              	//***********************mail
              	
              	
              	mysql_close();
              	
              ?>
              </body>
              </html>


              Voila ce qui à résolu mon problème. Comme l'a dit Mathieu (presque juste) il y avait
              $msg .= "--$boundary--\r\n";
              	
              	//piece jointe FACTURE
              	$msg .= "--$boundary\r\n";


              en trop. Comme l'a dit également Mathieu il faut traiter chaque pj comme si il n'y en avait qu'une. et dèrnière astuce. Il faut mettre la première en inline et la deuxième en attachment.

              Bonne continuation à tous. Lucas

              PS : merci mathieu
              1. Contributeur
                salut,

                il ne faut pas séparer chaque PJ avec le boundary ?
                en fait traiter l'une après l'autre comme si il n'y en avait qu'une ?

                et demande aux modos de gommer les adresses mail du premier post, ça peut passer chez Google.
                tu t'en fous peut être mais le client…
                -;o)
                1. Pour ma part je passe un tableau de fichier a transmettre et sans cette separation j'en ai qu une seule piece jointe...
                  Quelle histoire ces pieces jointes???