Formulaire de contact

hedichkir Messages postés 1 Date d'inscription mercredi 2 décembre 2015 Statut Membre Dernière intervention 2 décembre 2015 - 2 déc. 2015 à 11:33
 hedichkir - 2 déc. 2015 à 13:55
Bonjour,

Je souhaite rajouter à mon code PHP l’attachement d'un fichier avec si possible des conditions ...

<?php
$name       = @trim(stripslashes($_POST['name']));
$from       = @trim(stripslashes($_POST['email']));
$subject    = @trim(stripslashes($_POST['subject']));
$message    = @trim(stripslashes($_POST['message']));
$to         = 'contact@domaine.com';
 
$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
 
mail($to, $subject, $message, $headers);
 
die;


Merci d'avance

1 réponse

jordane45 Messages postés 38151 Date d'inscription mercredi 22 octobre 2003 Statut Modérateur Dernière intervention 2 mai 2024 344
2 déc. 2015 à 13:26
Bonjour,


Tu peux t'inspirer de ceci :

function sendMail($to,$objet,$message,$piece_jointe=NULL){
	
  // Creation de l'entete du mail a envoyer
  // --------------------------------------
  $boundary = md5(uniqid(microtime(), TRUE));
  $headers = "From: $this->_from\r\n";
  $headers .= "Cc: $this->_cc\n";
  $headers .= 'Mime-Version: 1.0'."\r\n";
  $headers .= 'Content-Type: multipart/mixed;boundary='.$boundary."\r\n";
  $headers .= "\r\n";

 
  // Piece jointe
  // ------------
  if (trim($piece_jointe) != '' && file_exists($piece_jointe)) {
   $file_type = filetype($piece_jointe);
   $file_size = filesize($piece_jointe);
   $nom_fichier = basename($piece_jointe); 
   $handle = fopen($piece_jointe, 'r') or die("Le fichier $piece_jointe ne peut être ouvert");
   $content = fread($handle, $file_size);
   $content = chunk_split(base64_encode($content));
   $f = fclose($handle);

   $message .= '--'.$boundary."\r\n";
   $message .= 'Content-type:'.$file_type.';name='. preg_replace("/^[0-9]+_/", "", $nom_fichier_joint) ."\r\n";
   $message .= 'Content-transfer-encoding:base64'."\r\n\r\n";
   $message .= $content."\r\n";
  }

  // Envoi du mail
  // -------------
  mail($to, $objet, $message, $headers);
}

0
Merci !!! je le testerais ...
0
Rejoignez-nous