Aide amélioration formulaire php

Répondre


Cette question est un moyen d’empêcher des soumissions automatisées de formulaires par des robots.
Smileys
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: =D> #-o =P~ :^o :non: :priere: 8-|
Voir plus de smileys
  Revue du sujet
 

  Étendre la vue Revue du sujet : Aide amélioration formulaire php

Re: Aide amélioration formulaire php

par Invité » 25 mai 2014, 17:14

Merci de ton aide.

Pourrais tu m'aider à mettre sur "papier" tes recommandations pour mettre à jour ce code ? :$
J'ai lu pas mal de cours et tuto sur le php mais je suis incapable d’exécuter les modifs moi même :s

d'avance, merci

Re: Aide amélioration formulaire php

par sirakawa » 25 mai 2014, 15:13

Il faut repenser tout le formulaire:
$erreurs = "";
si name n'exite pas $erreurs = "nom doit être renseigné";
$expedier = false;
si email invalide ou absent se fait par $email = filter_var ( $email , FILTER_SANITIZE_EMAIL ); $bon_email = filter_var($email, FILTER_VALIDATE_EMAIL); qui retourne le booleen false si lemail est mal construiit, sinon l'email.
$erreurs .= "email invalide";
$expedier = false;
si message vide
$erreurs = "message vide";
expedier = false;
if ($expedier == false)
{
print "$erreurs";
// se débrouiller pour reourner au formulaire (ou exit; ou die();)
}
else
{
envoyer message
}

Re: Aide amélioration formulaire php

par truser » 25 mai 2014, 14:44

Merci bien de ton aide.
Après test du formulaire, j'obtiens pas mal d'erreur alors que je reçois bien le mail. Dans un premier temps peux-tu m'aider à les résoudre pour partir sur un code propre et ensuite je m'attellerai à ajouter une deuxième pièce jointe ;).

Image

Re: Aide amélioration formulaire php

par sirakawa » 25 mai 2014, 14:11

Quelques commentaires sur un code qui est une véritable cochonnerie:
<?php
/*si on se sert de a méthode POST*/
if ($_SERVER['REQUEST_METHOD']=="POST")
{

   // Définition de l'adresse To " ; il douit s'agir du reply to
   $to="[email protected]";

   //Suject du mail
   $subject="Join Us E-mail with Resume attachment";

/* code suivant aberrant même s'il fonctionne:
	les tests suivent l'affectation du nom et d el'email de l'expéditeur 
	le test sur les champs vides n'empêche pas la poursuite du script

*/
   // Erreur 1 Get the sender's name and email address plug them a variable to be used later
   $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">";
   
   // erreur 2Y a-t-il des champs vides?
   if(empty($_POST['name'])  || empty($_POST['email']) || empty($_POST['message']))
   {
      $errors .= "\n Error: all fields are required";
   }
   
   //Erreur 1 Lire les valeurs du POSt (et non paxs de l'input) Get all the values from input
   $name = $_POST['name'];
   $email_address = $_POST['email'];
   $message = $_POST['message'];

   //Erreur 1 Tester l'adresse email Check the email address Qui plus est il existe une excellente fonction php pour faire ça: filter_var()
   if (!eregi(   "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address))
   {
      $errors .= "\n Error: Invalid email address";
   }
 //A ce niveau tout passe même si tout est aberrant
   // Now Generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";

   // Now Store the file information to a variables for easier access
   $tmp_name = $_FILES['filename1']['tmp_name'];
   $type = $_FILES['filename1']['type'];
   $file_name = $_FILES['filename1']['name'];
   $size = $_FILES['filename1']['size'];
   

   // Now here we setting up the message of the mail
   $message = "\n\n Name: $name \n\n Email: $email_address \n\nMessage: \n\n $message \n\nHere is your file: $file_name";

   // Check if the upload succeded, the file will exist
   if (file_exists($tmp_name)){

      // Check to make sure that it is an uploaded file and not a system file
      if(is_uploaded_file($tmp_name)){

         // Now Open the file for a binary read
         $file = fopen($tmp_name,'rb');

         // Now read the file content into a variable
         $data = fread($file,filesize($tmp_name));

         // close the file
         fclose($file);

         // Now we need to encode it and split it into acceptable length lines
         $data = chunk_split(base64_encode($data));
     }

      // Now we'll build the message headers
      $headers = "From: $from\r\n" .
         "MIME-Version: 1.0\r\n" .
         "Content-Type: multipart/mixed;\r\n" .
         " boundary=\"{$mime_boundary}\"";

      // Next, we'll build the message body note that we insert two dashes in front of the  MIME boundary when we use it
      $message = "This is a multi-part message in MIME format.\n\n" .
         "--{$mime_boundary}\n" .
         "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
         "Content-Transfer-Encoding: 7bit\n\n" .
         $message . "\n\n";

      // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached
      $message .= "--{$mime_boundary}\n" .
         "Content-Type: {$type};\n" .
         " name=\"{$file_name}\"\n" .
         //"Content-Disposition: attachment;\n" .
         //" filename1=\"{$fileatt_name}\"\n" .
         "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n" .
         "--{$mime_boundary}--\n";

      // Thats all.. Now we need to send this mail... :)
      if (@mail($to, $subject, $message, $headers))
     {
         ?>
         <div><center><h1>Mail Sent successfully !!</h1></center></div>
         <?php
     }else
     {
         ?>
         <div><center>
           <h1>Error !! Unable to send Mail..</h1></center></div>
         <?php
     }
   }
}
?></div>
<div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<div>
  <p><label for="email">Email:</label><br>
<input id="email" name="email" type="text" />
  </p>
  <p>
    <label for="tele">Upload Your Resume:</label><br>
    <input id="tele" name="filename1" type="file" />
  </p>
  <p>
    <label for="message">Message:<br></label>
    <textarea cols="71" rows="5" name="message"></textarea>
  </p>
</div>
<input class="formbtn" type="submit" value="Send Message" />
</form>
</div>
</div>
</body>
</html>

Aide amélioration formulaire php

par truser » 25 mai 2014, 12:05

Bonjour à tous,
Je sollicite votre aide pour améliorer (et surtout comprendre) un code php permettant d'envoyer via un formulaire un email contenant une pièce jointe. Ce code marche parfaitement, seulement, j'aurai bien aimé comprendre et que l'on m'aide pour avoir la possibilité de joindre une deuxième pièce jointe à ce formulaire/mail.
Côté html pas de problème, mais côté php, je rame...

voici l'intégralité du code :

Code : Tout sélectionner

<!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>Download PHP mail with attachment.</title> <link rel="stylesheet" media="screen" href="calendar.css" /> <meta name="google-site-verification" content="liGddaK7I8_x0tSdKv36CRi_rMfRt3yMNjILkbOAxxY" /> <link href='http://feeds2.feedburner.com/webinfopedia' rel='alternate' title='Webinfopedia-Learn SEO, Web Designing and Web development easily with example and demos' type='application/rss+xml'/> <meta name="msvalidate.01" content="1CBFD6FA96646CD69CE09C869B2F6313" /> <META name="y_key" content="2e447c925218040f" /> <link rel="search" type="application/opensearchdescription+xml" href="http://www.webinfopedia.com/classes/opensearch.xml" title="SEO,PHP and Ajax blog" /> <meta content="Example for sending PHP mail with attachment. Learn how to Mail with attachment in PHP" name="description" /> <meta content="PHP mail with attachment, PHP attachment script, download PHP mail with attachment, attachment in PHP, Free PHP attachment Script, Download PHP mail attachment script, PHP, Mail, Php mail, PHP attachment Download" name="keywords" /> <meta name="author" content="webinfopedia" /> <meta name="copyright" content="webinfopedia.com" /> <meta name="Robots" content="index, follow" /> <meta name="language" content="English" /> <link rel="icon" type="image/x-icon" href="http://www.webinfopedia.com/favicon.ico" /> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-22897853-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <style type="text/css"> <!-- body,td,th {font-family: Trebuchet MS, Arial, Helvetica, sans-serif; font-size: 12px; color: #333;} body {margin-left:0px;} .maindiv{ width:690px; margin:0 auto;} .textbox{ padding:2px 4px; width:200px;} .submit{ border:solid 1px #008000; background:#008000; color:#FFF; font-weight:bold;} --> </style> </head> <body> <div class="maindiv"> <div><?php if ($_SERVER['REQUEST_METHOD']=="POST"){ // Set the "To" email address $to="[email protected]"; //Subject of the mail $subject="Join Us E-mail with Resume attachment"; // Get the sender's name and email address plug them a variable to be used later $from = stripslashes($_POST['name'])."<".stripslashes($_POST['email']).">"; // Check for empty fields if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) { $errors .= "\n Error: all fields are required"; } // Get all the values from input $name = $_POST['name']; $email_address = $_POST['email']; $message = $_POST['message']; // Check the email address if (!eregi( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email_address)) { $errors .= "\n Error: Invalid email address"; } // Now Generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // Now Store the file information to a variables for easier access $tmp_name = $_FILES['filename1']['tmp_name']; $type = $_FILES['filename1']['type']; $file_name = $_FILES['filename1']['name']; $size = $_FILES['filename1']['size']; // Now here we setting up the message of the mail $message = "\n\n Name: $name \n\n Email: $email_address \n\nMessage: \n\n $message \n\nHere is your file: $file_name"; // Check if the upload succeded, the file will exist if (file_exists($tmp_name)){ // Check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // Now Open the file for a binary read $file = fopen($tmp_name,'rb'); // Now read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // Now we need to encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } // Now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // Next, we'll build the message body note that we insert two dashes in front of the MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Now we'll insert a boundary to indicate we're starting the attachment we have to specify the content type, file name, and disposition as an attachment, then add the file content and set another boundary to indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$file_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename1=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; // Thats all.. Now we need to send this mail... :) if (@mail($to, $subject, $message, $headers)) { ?> <div><center><h1>Mail Sent successfully !!</h1></center></div> <?php }else { ?> <div><center> <h1>Error !! Unable to send Mail..</h1></center></div> <?php } } } ?></div> <div> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data"> <div> <p><label for="email">Email:</label><br> <input id="email" name="email" type="text" /> </p> <p> <label for="tele">Upload Your Resume:</label><br> <input id="tele" name="filename1" type="file" /> </p> <p> <label for="message">Message:<br></label> <textarea cols="71" rows="5" name="message"></textarea> </p> </div> <input class="formbtn" type="submit" value="Send Message" /> </form> </div> </div> </body> </html>
Merci d'avance