Salut à tous et à toutes !
Voila j'ai tenté un petit formulaire de contact, qui m'envois les données par mail, ainsi que la pièce jointe.
Mon seul soucis, c'est que quand le mail est reçu, j'ai bien les infos et la pièce jointe, mais entre les deux il y à des millers de Lettres et de Chiffres incomprehensibles...
Une Solution ?
Le HTML :
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
<title> Formulaire de Contact </title>
</head>
<body>
<div id="cadreformulaire">
<form method="post" action="traitement.php" enctype="multipart/form-data" accept-charset="UTF-8">
<h2 class="titres" for="titre"> Contact </h2>
<input type="text" class="exemple" name="prenom" placeholder="Ex: Hugo" required>
<input type="text" class="exemple" name="nom" placeholder="Ex: Maury" required/>
<input type="email" class="exemple" name="email" placeholder="Email" required/>
<h2 class="titres">Message</h2>
<textarea class="exemple" name="texte" placeholder="Ex: Votre formulaire fonctionne à merveille !" required></textarea>
<div>
<label for="piecejointe">Ajouter une pièce jointe</label>
<input type="file" name="piecejointe"/>
</div>
<button type="submit" name="envoyer" class="button">Envoyer</button>
</form>
</div>
</body>
</html>
Le PHP :
<?php
$destinataire = "[email protected]";
$nom= $_POST['nom'];
$prenom = $_POST['prenom'];
$email = $_POST['email'];
$texte = $_POST['texte'];
$data = chunk_split(base64_encode($data));
/* GET File Variables */
$tmpName = $_FILES['piecejointe']['tmp_name'];
$fileType = $_FILES['piecejointe']['type'];
$fileName = $_FILES['piecejointe']['name'];
/* Start of headers */
$headers = "From: $email";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File piecejointe */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "Prenom: $prenom\n\n" . "Nom: $nom\n\n" . "Email: $email\n\n" . "Annonce: $texte\n\n" .
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
mail ("$destinataire","$subject","$message","$headers");
?>
Salut à tous et à toutes !
Voila j'ai tenté un petit formulaire de contact, qui m'envois les données par mail, ainsi que la pièce jointe.
Mon seul soucis, c'est que quand le mail est reçu, j'ai bien les infos et la pièce jointe, mais entre les deux il y à des millers de Lettres et de Chiffres incomprehensibles...
Une Solution ?
Le HTML :
[html]<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
<title> Formulaire de Contact </title>
</head>
<body>
<div id="cadreformulaire">
<form method="post" action="traitement.php" enctype="multipart/form-data" accept-charset="UTF-8">
<h2 class="titres" for="titre"> Contact </h2>
<input type="text" class="exemple" name="prenom" placeholder="Ex: Hugo" required>
<input type="text" class="exemple" name="nom" placeholder="Ex: Maury" required/>
<input type="email" class="exemple" name="email" placeholder="Email" required/>
<h2 class="titres">Message</h2>
<textarea class="exemple" name="texte" placeholder="Ex: Votre formulaire fonctionne à merveille !" required></textarea>
<div>
<label for="piecejointe">Ajouter une pièce jointe</label>
<input type="file" name="piecejointe"/>
</div>
<button type="submit" name="envoyer" class="button">Envoyer</button>
</form>
</div>
</body>
</html>[/html]
Le PHP :
[php]<?php
$destinataire = "
[email protected]";
$nom= $_POST['nom'];
$prenom = $_POST['prenom'];
$email = $_POST['email'];
$texte = $_POST['texte'];
$data = chunk_split(base64_encode($data));
/* GET File Variables */
$tmpName = $_FILES['piecejointe']['tmp_name'];
$fileType = $_FILES['piecejointe']['type'];
$fileName = $_FILES['piecejointe']['name'];
/* Start of headers */
$headers = "From: $email";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File piecejointe */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "Prenom: $prenom\n\n" . "Nom: $nom\n\n" . "Email: $email\n\n" . "Annonce: $texte\n\n" .
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
mail ("$destinataire","$subject","$message","$headers");
?>[/php]