Problème 2 pieces jointes php

Résolu/Fermé
lucasc - 17 févr. 2009 à 16:22
Fallentree Messages postés 2309 Date d'inscription mercredi 25 février 2009 Statut Membre Dernière intervention 22 juillet 2019 - 11 déc. 2010 à 19:10
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...
A voir également:

14 réponses

lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
2 mars 2009 à 10:23
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
7
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
2 mars 2009 à 10:23
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
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
18 févr. 2009 à 20:12
personne pour m'aider ?
0
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
18 févr. 2009 à 20:45
Svp j'ai vraiment besoin d'aide !!!
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
18 févr. 2009 à 20:52
merci mathieu,

je vais essayer ca.
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
18 févr. 2009 à 21:16
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…
0
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
18 févr. 2009 à 21:18
merci beaucoup
0
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
27 févr. 2009 à 19:15
UP finalement je n'ai toujours pas trouvé !!

S'il vous plaît j'ai vraiment besoin de votre aide ... MERCI
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
28 févr. 2009 à 13:21
salut,

ton code ressemble à quoi maintenant ?
0
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
28 févr. 2009 à 13:33
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
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
2 mars 2009 à 09:07
	$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 ?
0
lucasc Messages postés 97 Date d'inscription mercredi 18 février 2009 Statut Membre Dernière intervention 12 juin 2009 14
2 mars 2009 à 10:22
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
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
2 mars 2009 à 11:10
nickel !

bon courage pour la suite !
-;o)
0
Bravo ça marche, merci !!
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
18 févr. 2009 à 20:49
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
Fallentree Messages postés 2309 Date d'inscription mercredi 25 février 2009 Statut Membre Dernière intervention 22 juillet 2019 208
Modifié par Fallentree le 12/12/2010 à 11:20
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???
0