1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
<?php
function typemime($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;
}
//==============================================================================================
function mail_attachement($to , $bcc , $sujet , $message , $fichier , $typemime , $nom , $reply , $from)
{
$limite = "_parties_".md5(uniqid (rand()));
$mail_mime = "Date: ".date('l j F Y, G:i')."\n";
$mail_mime .= "MIME-Version: 1.0\n";
$mail_mime .= "Content-Type: multipart/mixed;\n";
$mail_mime .= " boundary=\"----=$limite\"\n\n";
//le fichier
$attachement = "------=$limite\n";
//d'abord un message texte qui peut être en html
$attachement .= "Content-Type: text/html; charset=ISO-8859-1\n";
$attachement .= "Content-Transfer-Encoding: 8bit\n";
$attachement .= $message;
$attachement .= "\n\n\n------=$limite\n";
// traitement du fichier à attacher
$attachement .= "Content-Type: $typemime; name=\"$nom\"\n";
$attachement .= "Content-Transfer-Encoding: base64\n";
$attachement .= "Content-Disposition: attachment; filename=\"$nom\"\n\n";
$fd = fopen( $fichier, "r" );
$contenu = fread( $fd, filesize( $fichier ) );
fclose( $fd );
$attachement .= chunk_split(base64_encode($contenu));
$attachement .= "\n\n\n------=$limite\n";
return mail($to, $sujet, $attachement, "Reply-to: $reply\nFrom: $from\nBcc: $bcc\n".$mail_mime);
}
//==================================================================================================
$expediteur = $_POST['expediteur'];
$destinataire = $_POST['destinataire'];
$copie = $_POST['copie'];
$sujet = $_POST['sujet'];
$texte = str_repeat($_POST['message'],1);//$message = stripslashes($_POST['message']);
$texte = wordwrap($texte, 66 , "<br>" ); // pour découper le message en lignes de 70
//$nomfichier = basename( $_FILES['fichier']['name']);
$nomfichier = $_FILES['fichier']['name'];
if(!($expediteur && $destinataire))
{
echo "<script> javascript:history.back() </script>";
exit;
}
if($nomfichier)
{
$mimeType = typemime($nomfichier);
$fichier_telechargeable = $_FILES['fichier']['tmp_name']; // fichier temporaire chargé par le formulaire
$replyto = ""; // peut être nul
mail_attachement($destinataire,$copie,$sujet,$texte,$fichier_telechargeable,$mimeType,$nomfichier, $replyto, $expediteur);
}
else // sans attachement
mail( $destinataire , $sujet, $texte, "Content-Type: text/html; charset=ISO-8859-1\nFrom: $expediteur\nBCc: $copie\n");
echo "<script> javascript:location.replace(document.referrer) </script>";
exit;
?> |
Partager