
le 07/02/2005 à 17:29
"mail ()" et pièces jointes
Je fais comme ceci mes mails avec pièce jointe :
<?php
$destinataire = 'mail@site.com';
$sujet = 'le sujet';
$text_mail = 'Mon super mail qui déchire';
$fp = fopen($_FILES['fichier']['tmp_name'], "rb");
$attachment = fread($fp, filesize($_FILES['fichier']['tmp_name']));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));
$boundary = "-----=".md5(uniqid(rand()));
$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\n";
$msg .= "--$boundary\n";
$msg .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$msg .= "Content-Transfer-Encoding:8bit\n";
$msg .= "\n";
$msg .= $text_mail."\n";
$msg .= "\n";
$msg .= "--$boundary\n";
switch(strrchr($_FILES['fichier']['name'], ".")){
case ".gz": $msg .= 'Content-Type: application/x-gzip; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".tgz": $msg .= 'Content-Type: application/x-gzip; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".zip": $msg .= 'Content-Type: application/zip; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".pdf": $msg .= 'Content-Type: application/pdf; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".png": $msg .= 'Content-Type: image/png; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".gif": $msg .= 'Content-Type: image/gif; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".jpg": $msg .= 'Content-Type: image/jpeg; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".txt": $msg .= 'Content-Type: text/plain; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".htm": $msg .= 'Content-Type: text/html; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
case ".html": $msg .= 'Content-Type: text/html; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
default: $msg .= 'Content-Type: application/octet-stream; name="'.$_FILES['fichier']['name'].'"'."\n"; break;
}
$msg .= "Content-Transfer-Encoding: base64\n";
$msg .= "Content-Disposition: attachment; filename=\"$file\"\n";
$msg .= "\n";
$msg .= $attachment . "\n";
$msg .= "\n\n";
$msg .= "--$boundary--\n";
$header = "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
$header .= "\n";
mail($destinataire, $sujet , $msg, "Reply-to: $destinataire\nFrom: $destinataire\n".$header);
?>