Bonjour, je souhaiterais envoyer un mail avec une pièce jointe via un formulaire html. Le but est de récupérer un cv et une lettre de motivation. J'ai bien les deux pieces jointes mais elles sont vides.
voici le code du formulaire :
<FORM name="form" id="monForm" method="GET" action="envoi_candidature.php" enctype="multipart/form-data" onSubmit="return check();" >
<table>
<tr>
<td> Nom * : </td>
<td> E-Mail * : </td>
</tr>
<tr>
<td> <INPUT name="nom" type="text" size="20" onKeyUp="javascript:couleur(this);"> </td>
<td> <INPUT name="mail" type="text" size="30" onKeyUp="javascript:couleur(this);"> </td>
</tr>
<tr>
<td> Prénom * : </td>
<td> Téléphone * : </td>
</tr>
<tr>
<td> <INPUT name="prenom" type="text" size="20" onKeyUp="javascript:couleur(this);"> </td>
<td> <INPUT name="telephone" type="text" size="10" onKeyUp="javascript:couleur(this);"> </td>
</tr>
<tr>
<td> Adresse * : </td>
<td> Poste sollicité * : </td>
</tr>
<tr>
<td> <INPUT name="adresse" type="text" size="30" onKeyUp="javascript:couleur(this);"> </td>
<td> <INPUT name="poste_sollicite" type="text" size="20" onKeyUp="javascript:couleur(this);"> </td>
</tr>
<tr>
<td> Code postal * :</td>
<td> Lettre de motivation * :</td>
</tr>
<tr>
<td> <INPUT name="cp" type="text" size="5" onKeyUp="javascript:couleur(this);"> </td>
<td> <input name="lettre_motiv" type="file" > </td>
</tr>
<tr>
<td> Ville * : </td>
<td> CV : *</td>
</tr>
<tr>
<td> <INPUT name="ville" type="text" size="15" onKeyUp="javascript:couleur(this);"> </td>
<td> <input name="cv" type="file" > </td>
</tr>
</table>
<fieldset>
<legend>Vérification anti-spam</legend>
<p><font size="2">Veuillez s'il vous plait répondre à cette question : *
<br>Combien font 2 + 6 ?</font>
<input name="protection" type="text" size="3" onKeyUp="javascript:couleur(this);">
</p>
</fieldset>
<p>
<font size="1">Les champs marqués d'un * sont obligatoires</font>
</p>
<input type="submit" value="Envoyer" name="bouton" >
</form>
Voici le code qui effectue le traitement :
<?php
$nom = $_GET["nom"];
$prenom = $_GET["prenom"];
$adresse = $_GET["adresse"];
$cp = $_GET["cp"];
$ville = $_GET["ville"];
$mail = $_GET["mail"];
$telephone = $_GET["telephone"];
$poste_sollicite = $_GET["poste_sollicite"];
$lettre_motiv = $_GET["lettre_motiv"];
$cv = $_GET["cv"];
echo $lettre_motiv;
//----------------------------------
// Construction de l'entête
//----------------------------------
$boundary = "-----=".md5(uniqid(rand()));
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$header .= "\r\n";
//--------------------------------------------------
// Construction du message proprement dit
//--------------------------------------------------
$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";
//---------------------------------
// 1ère partie du message
// Le texte
//---------------------------------
$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 .= "Ceci est un mail avec 2 fichiers joints\r\n";
$msg .= "\r\n";
//---------------------------------
// 2nde partie du message
// Le 1er fichier (inline)
//---------------------------------
$file = $lettre_motiv;
$fp = fopen($file, "rb"); // le b c'est pour les windowsiens
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));
$msg .= "--$boundary\r\n";
$msg .= "Content-Type: multipart/mixed; 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";
//---------------------------------
// 3ème partie du message
// Le 2ème fichier (attachment)
//---------------------------------
$file = $lettre_motiv;
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));
$msg .= "--$boundary\r\n";
$msg .= "Content-Type: image/gif; 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@hotmail.com";
$expediteur = "monAdresse@hotmail.com";
$reponse = $expediteur;
echo "Ce script envoie un mail avec 2 fichiers joints à $destinataire";
mail($destinataire,
"Email avec 2 fichiers joints (dont 1 inline)",
$msg,
"Reply-to: $reponse\r\nFrom: $destinataire\r\n".$header);