Bonjour à tous,
Je suis débutant et j'ai fais avec l'assistance d'un ami un formulaire d'envoi email avec captcha récupéré sur le net (le captcha).
Mon formulaire fonctionne bien mais par momment je recois des emails vide pourtant j'ai fais en sorte qu'ils ne le soient pas je vous mets ci-dessous mon code
Code formulaire :
Code PHP :
<?PHP
session_start();
$txt_erreur="";
if (isset($_GET['txt_erreur']))
{
$txt_erreur=stripslashes(urldecode($_GET['txt_erreur']));
}
$txt_nom="";
if (isset($_GET['nom']))
{
$txt_nom=$_GET['nom'];
}
$txt_prenom="";
if (isset($_GET['prenom']))
{
$txt_prenom=$_GET['prenom'];
}
$txt_mail="";
if (isset($_GET['email']))
{
$txt_mail=$_GET['email'];
}
$txt_titre="";
if (isset($_GET['titre']))
{
$txt_titre=$_GET['titre'];
}
$txt_message="";
if (isset($_GET['message']))
{
$txt_message=$_GET['message'];
}
?>
<!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>Contactez-nous !</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function validform(){
err=0;
if(document.form_contact.txt_nom.value==""){
err=1;
document.form_contact.txt_nom.style.backgroundColor='#FFCCCC';
}else{
document.form_contact.txt_nom.style.backgroundColor='#FFFFFF';
}
if(document.form_contact.txt_prenom.value==""){
err=1;
document.form_contact.txt_prenom.style.backgroundColor='#FFCCCC';
}else{
document.form_contact.txt_prenom.style.backgroundColor='#FFFFFF';
}
if(document.form_contact.txt_mail.value==""){
err=1;
document.form_contact.txt_mail.style.backgroundColor='#FFCCCC';
}else{
mon_mail = document.form_contact.txt_mail.value;
if (!mon_mail.match('^[-_\.0-9a-zA-Z]{1,}@[-_\.0-9a-zA-Z]{1,}[\.][0-9a-zA-Z]{2,}$')) {
err=1;
document.form_contact.txt_mail.style.backgroundColor='#FFCCCC';
}else{
document.form_contact.txt_mail.style.backgroundColor='#FFFFFF';
}
}
if(document.form_contact.txt_message.value==""){
err=1;
document.form_contact.txt_message.style.backgroundColor='#FFCCCC';
}else{
document.form_contact.txt_message.style.backgroundColor='#FFFFFF';
}
if(document.form_contact.captcha.value==""){
err=1;
document.form_contact.captcha.style.backgroundColor='#FFCCCC';
}else{
document.form_contact.captcha.style.backgroundColor='#FFFFFF';
}
//alert(err);
if (err==0){
return true;
}else{
return false;
}
}
</script>
<style type="text/css">
<!--
body {
background-image:none;
}
.Style1 {color: #FF0000}
-->
</style>
</head>
<body>
<form id="form_contact" name="form_contact" method="post" action="reponse.php" onsubmit="return validform()">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td colspan="3"><div align="center"></div></td>
</tr>
<tr>
<td class="petit_texte"><div align="right">Nom :</div></td>
<td width="12%" class="petit_texte"><label>
<input name="txt_nom" type="text" class="champ" id="txt_nom" value="<?php echo($txt_nom);?>" size="25" />
</label></td>
<td width="59%" class="petit_texte">Prénom
: <input name="txt_prenom" type="text" class="champ" id="txt_prenom" value="<?php echo($txt_prenom);?>" size="25" /></td>
</tr>
<tr>
<td class="petit_texte"><div align="right">Email :</div></td>
<td colspan="2" class="petit_texte"><input name="txt_mail" type="text" class="champ" id="txt_mail" value="<?php echo($txt_mail);?>" size="40" /></td>
</tr>
<tr>
<td class="petit_texte"><div align="right">Titre :</div></td>
<td colspan="2" class="petit_texte"><input name="txt_titre" type="text" class="champ" id="txt_titre" value="<?php echo($txt_titre);?>" size="30" /></td>
</tr>
<tr>
<td class="petit_texte"><div align="right">Message :</div></td>
<td colspan="2" class="petit_texte"><label>
<textarea name="txt_message" cols="50" class="champ" id="txt_message"><?php echo($txt_message);?></textarea>
<br />
</label></td>
</tr>
<tr>
<td class="petit_texte"><div align="right">Recopiez les chiffres :</div></td>
<td colspan="2" class="petit_texte"><label for="captcha"><img src="captcha.php" alt="Captcha" /> <a href="wav.php"><img src="sound.png" alt="ecouter le captcha" border="0" /></a></label>
<input name="captcha" type="text" class="champ" id="captcha" />
<span class="Style1"><?PHP echo $txt_erreur; ?></span></td>
</tr>
<tr>
<td class="petit_texte"> </td>
<td colspan="2" class="petit_texte"><input type="image" name="imageField" id="imageField" src="images/b_valider.png" /></td>
</tr>
</table>
</form>
<?php include ("referencement.php");
?>
</body>
</html>
Code vérification d'envoi :
Code PHP :
<?php
session_start();
// variables du formulaire
$nom = $_POST['txt_nom'];
$prenom = $_POST['txt_prenom'];
$mail = $_POST['txt_mail'];
$titre = $_POST['txt_titre'];
$mess = $_POST['txt_message'];
/////////////////////////////////////////////
$captcha = $_POST['captcha'];
$sess_captcha = $_SESSION['captcha'];
if ($captcha<>$sess_captcha){
header("location:contact.php?txt_erreur=Chiffres incorrects&nom=$nom&prenom=$prenom&email=$mail&titre=$titre&message=$mess");
}else
{
// Plusieurs destinataires
$to = 'adresse@email.com';
// Sujet et dans le sujet on rajoute le nom et le prénom
$subject = "Mon sujet email - ".$_POST['txt_nom']." ".$_POST['txt_prenom'];
// message
$message = "
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />
<title>Email</title>
</head>
<body>
<table width=\"80%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"24%\">nom</td>
<td width=\"76%\">".$nom."</td>
</tr>
<tr>
<td>prenom</td>
<td>".$prenom."</td>
</tr>
<tr>
<td>email</td>
<td>".$mail."</td>
</tr>
<tr>
<td>titre</td>
<td>".$titre."</td>
</tr>
<tr>
<td>message</td>
<td>".$mess."</td>
</tr>
</table>
</body>
</html>
";
// Pour envoyer un mail HTML, l'en-tete Content-type doit etre defini
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// En-etes additionnels
$headers .= 'To: Moi <adresse@email.com>' . "\r\n"; //destinataire
$headers .= 'From: Sujet du formulaire <adresse@email.com>' . "\r\n"; //expediteur
//$headers .= 'Cc: toto <adresse2@email.com>' . "\r\n"; //copie conforme
//$headers .= 'Bcc:' . "\r\n"; copie cachée
?>
<!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>Message envoyé !</title>
</head>
<body>
<?php
mail($to, $subject, utf8_decode(stripslashes($message)), $headers);
?>
<img src="images/coche_verte.png" alt="verification ok" width="32" height="32" align="absmiddle" /> Message envoyé !
</body>
</html>
<?php
}
?>
malgré la vérification je reçois des mails vide, moi quand je le test ca fonctionne je ne fois pas ou est l'erreur. Peut-on encore l'améliorer.
Cordialement,
Bruno