comment ajouter un fichier joint a un formulaire pour envoi
Posté : 20 mars 2008, 15:57
bonjour!!
J'ai un formulaire a mettre sur un site. Les données remplies par les internautes me seront envoyées par mail.
Je voudrais y ajouter la possibilité de m'envoyer des fichiers joint (images). Mais je suis novice en php et je n'arrive pas...
Voila le code de mon formulaire et du fichier php, :
formulaire HTML
traitement php:
Voila,
Je vous remercie d'avance.
Cordialement
J'ai un formulaire a mettre sur un site. Les données remplies par les internautes me seront envoyées par mail.
Je voudrais y ajouter la possibilité de m'envoyer des fichiers joint (images). Mais je suis novice en php et je n'arrive pas...
Voila le code de mon formulaire et du fichier php, :
formulaire HTML
<!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/html; charset=utf-8" />
<title>Formulaire</title>
</head>
<body>
<center>
<form action="traitement.php" method="post" enctype="multipart/form-data">
<p class="titre"><strong>Coordonnées</strong></p>
<fieldset id="coordonnees">
<p id="civilite"><label><strong>Civilité </strong>: </label>
<input type="radio" name="civilite" value="M." />M.
<input type="radio" name="civilite" value="Mlle" />Mlle
<input type="radio" name="civilite" value="Mme" />Mme
<br />
</p>
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="130" height="25"><div align="left"><strong>Nom</strong></div></td>
<td width="20" height="25"><div align="center"><strong>:</strong></div></td>
<td width="150" height="25"><div align="center">
<input type="text" name="nom" size="20" />
</div></td>
</tr>
<tr>
<td width="130" height="25"><strong>Prénom</strong></td>
<td width="20" height="25"><div align="center"><strong>:</strong></div></td>
<td width="150" height="25"><div align="center">
<input type="text" name="prenom" size="20" />
</div></td>
</tr>
<tr>
<td width="130" height="25"><strong>E-mail</strong></td>
<td width="20" height="25"><div align="center"><strong>:</strong></div></td>
<td width="150" height="25"><div align="center">
<input type="text" name="Email" size="20" />
</div></td>
</tr>
<tr>
<td width="130" height="25"><strong>Adresse</strong></td>
<td width="20" height="25"><div align="center"><strong>:</strong></div></td>
<td width="150" height="25"><div align="center">
<input type="text" name="adresse" size="20" />
</div></td>
</tr>
<tr>
<td width="130" height="25"><strong>Code postal</strong></td>
<td width="20" height="25"><div align="center"><strong>:</strong></div></td>
<td width="150" height="25"><div align="center">
<input type="text" name="codepostal" size="20" />
</div></td>
</tr>
<tr>
<td width="130" height="25"><strong>Ville</strong></td>
<td width="20" height="25"><div align="center"><strong>:</strong></div></td>
<td width="150" height="25"><div align="center">
<input type="text" name="ville" size="20" />
</div></td>
</tr>
</table>
<label></label>
<br />
<label><strong>Pays</strong> : </label>
<select name="pays">
<option value="Belgique">Belgique</option>
<option value="france">France</option>
<option value="Suisse">Suisse</option>
<option value="Tunisie">Tunisie</option>
<option value="Autre">Autre</option>
</select>
<p id="interets">
<label></label>
</p>
</fieldset>
<p class="titre"><strong>Ecrivez votre annonce ici :</strong></p>
<fieldset id="message">
<p>
<textarea name="comments" cols="50" rows="5" wrap="physical"></textarea>
<br />
<br />
<strong>Insérez vos images ici :</strong></p>
<p>
<label>
<input type="file" name="inserez_une_image" id="insérez une image" />
</label>
<br />
<input type="file" name="inserez une image2" id="insérez une image2" />
<br />
<input type="file" name="inserez une image3" id="insérez une image3" />
</p>
</fieldset>
<p id="buttons">
<input type="submit" value="Envoyer" />
<input type="reset" value="Recommencer" />
</p>
</form>
</center>
</body>
</html>
traitement php:
<!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/html; charset=utf-8" />
<title>envoi formulaire</title>
</head>
<body>
<?php
$msg_erreur = "Erreur. Les champs suivants doivent être obligatoirement remplis :<br/><br/>";
$msg_ok = "Votre demande a bien été prise en compte.";
$message = $msg_erreur;
define('MAIL_DESTINATAIRE','[email protected]'); // remplacer par votre email
define('MAIL_SUJET','Message du formulaire');
// vérification des champs
if (empty($_POST['civilite']))
$message .= "Votre civilité<br/>";
if (empty($_POST['nom']))
$message .= "Votre nom<br/>";
if (empty($_POST['prenom']))
$message .= "Votre prénom<br/>";
if (empty($_POST['Email']))
$message .= "E-mail<br/>";
if (empty($_POST['adresse']))
$message .= "Votre adresse<br/>";
if (empty($_POST['codepostal']))
$message .= "Votre code postal<br/>";
if (empty($_POST['ville']))
$message .= "Votre ville<br/>";
if (empty($_POST['comments']))
$message .= "Votre message<br/>";
// si un champ est vide, on affiche le message d'erreur
if (strlen($message) > strlen($msg_erreur)) {
echo $message;
// sinon c'est ok
} else {
foreach($_POST as $index => $valeur) {
$$index = stripslashes(trim($valeur));
}
$interets = $_POST['interets'];
$sqlinterets = '';
for ($i=0; $i<count($interets); $i++)
{
$sqlinterets .= $interets[$i];
$sqlinterets .= ', ';
}
//Préparation de l'entête du mail:
$mail_entete = "MIME-Version: 1.0\r\n";
$mail_entete .= "From: {$_POST['nom']} "
."<{$_POST['email']}>\r\n";
$mail_entete .= 'Reply-To: '.$_POST['email']."\r\n";
$mail_entete .= 'Content-Type: text/plain; charset="iso-8859-1"';
$mail_entete .= "\r\nContent-Transfer-Encoding: 8bit\r\n";
$mail_entete .= 'X-Mailer:PHP/' . phpversion()."\r\n";
// préparation du corps du mail
$mail_corps = "Message de : $civilite $nom $prenom\n";
$mail_corps .= "Adresse : $adresse, $codepostal $ville, $pays\n";
$mail_corps .= "E-mail : $Email\n";
$mail_corps .= "La rubrique qui l'intéresse : $sqlrubrique\n\n\n";
$mail_corps .= $comments;
// envoi du mail
if (mail(MAIL_DESTINATAIRE,MAIL_SUJET,$mail_corps,$mail_entete)) {
//Le mail est bien expédié
echo $msg_ok;
} else {
//Le mail n'a pas été expédié
echo 'Une erreur est survenue lors de l\'envoi du formulaire par email';
}
}
?>
</body>
</html>
Voila,
Je vous remercie d'avance.
Cordialement