envoi de mails avec fichier joint
Posté : 12 sept. 2011, 21:54
Bonjour,
j'ai essayé laborieusement les différents exemples présents dans les forum, aucun ne marche !
voici mon test : on reçoit bien un mail prétendant avoir une pièce jointe mais quand on essaye de lire le mail il la perd, elle disparaît !!!.
Le premier (ou la première) qui aura une bonne idée sera bien récompensé.
merci d''avance
A
j'ai essayé laborieusement les différents exemples présents dans les forum, aucun ne marche !
voici mon test : on reçoit bien un mail prétendant avoir une pièce jointe mais quand on essaye de lire le mail il la perd, elle disparaît !!!.
Code : Tout sélectionner
<?php
function attachtype($file)
{
$ext = explode(".", $file);
switch($ext[1])
{
default:
$type = "application/octet-stream";
break;
case "gz":
$type = "application/x-gzip";
break;
case "tgz":
$type = "application/x-gzip";
break;
case "zip":
$type = "application/zip";
break;
case "pdf":
$type = "application/pdf";
break;
case "png":
$type = "image/png";
break;
case "gif":
$type = "image/gif";
break;
case "jpg":
case"jpeg":
$type = "image/jpeg";
break;
case "txt":
$type = "text/plain";
break;
case "htm":
$type = "text/html";
break;
case "html":
$type = "text/html";
break;
}
return $type;
}
//============================================================
$expediteur = $_POST['expediteur'];
$destinataire = $_POST['destinataire'];
$copie = $_POST['copie'];
$message = stripslashes($_POST['message']);
$sujet = $_POST['sujet'];
$fichier = basename( $_FILES['fichier']['name']);
if(!($expediteur && $destinataire))
{
echo "<script> javascript:history.back() </script>";
exit;
}
$boundary = "----=".md5(uniqid(microtime(), TRUE));
if($fichier)
{
$upload_file = $_FILES['fichier']['tmp_name']; // fichier temporaire chargé par le formulaire
$fichier = basename( $_FILES['fichier']['name']);
$sujet = $_POST["sujet"];
$message = htmlspecialchars($_POST["message"], ENT_QUOTES);
$attach_name = $_FILES["fichier"]["name"];
$attach_type = attachtype($fichier);
$mailheaders = "From: \"$expediteur\"\r\n";
$mailheaders .= "BCc: $copie\r\n";
$mailheaders .= "MIME-Version: 1.0\r\n";
$mailheaders .= "Content-Type: multipart/mixed; boundary=--\"".$boundary."\"";
$mailheaders .= "--$boundary\r\n\r\n";
$mailheaders .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
//$mailheaders .= "X-attachments: $attach_name\r\n";
$body_top = "--$boundary\r\n";
$body_top .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$body_top .= "Content-transfer-encoding: 8bit\r\n";
$msg_body = $body_top . $message;
$msg_body .= "\r\n\r\n--$boundary--\n";
$msg_body .= "Content-type: $attach_type; name=\"$attach_name\"\r\n";
$longueur = filesize($upload_file);
$msg_body .= "Content-Length: $longueur\r\n";
$msg_body .= "Content-transfer-Encoding: BASE64\r\n";
$msg_body .= "Content-disposition: attachment; filename=\"$attach_name\"\r\n\r\n";
$msg_body .= chunk_split(base64_encode(file_get_contents($upload_file)))."\r\n";
$msg_body .= "--$boundary--\r\n";
if(mail($destinataire, $sujet, $msg_body, $mailheaders))
echo "<br><br> SUCCES <br>";
}
else
if(mail( $destinataire , $sujet, $message, "Content-Type: text/html; charset=ISO-8859-1\r\nFrom: $expediteur\r\nBCc: $copie\r\n"))
{
echo "<br><br> SUCCES <br>";
}
//header("location: $HTTP_REFERER"); // retour arrière vers la page d'appel vierge
?>Le premier (ou la première) qui aura une bonne idée sera bien récompensé.
merci d''avance
A