Vous pouvez utiliser cette fonction:
function mail_file($to, $subject, $messagehtml, $from, $fileatt, $replyto="") {
// handles mime type for better receiving
$ext = strrchr($fileatt , '.');
$ftype = "";
if ($ext == ".doc") $ftype = "application/msword";
if ($ext == ".jpg") $ftype = "image/jpeg";
if ($ext == ".gif") $ftype = "image/gif";
if ($ext == ".zip") $ftype = "application/zip";
if ($ext == ".pdf") $ftype = "application/pdf";
if ($ftype=="") $ftype = "application/octet-stream";
// read file into $data var
$file = fopen($fileatt, "rb");
$data = fread($file, filesize($fileatt));
fclose($file);
// split the file into chunks for attaching
$content = chunk_split(base64_encode($data));
$uid = md5(uniqid(time()));
// build the headers for attachment and html
$h = "From: $from\r\n";
if ($replyto) $h .= "Reply-To: ".$replyto."\r\n";
$h .= "MIME-Version: 1.0\r\n";
$h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$h .= "This is a multi-part message in MIME format.\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-type:text/html; charset=iso-8859-1\r\n";
$h .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$h .= $messagehtml."\r\n\r\n";
$h .= "--".$uid."\r\n";
$h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n";
$h .= "Content-Transfer-Encoding: base64\r\n";
$h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n";
$h .= $content."\r\n\r\n";
$h .= "--".$uid."--";
// send mail
return mail($to, $subject, strip_tags($messagehtml), str_replace("\r\n","\n",$h)) ;
}
http://www.barattalo.it/2010/01/10/sending-emails-with-attachment-and-html-with-php/
Avez-vous pensé à regarder dans Zend Framework? J'ai commencé à utiliser seulement sa classe Zend_Mail. Je suis tombé amoureux de ça, et maintenant j'utilise ZF sur tous mes sites. En utilisant ZF, vous pouvez valider les entrées, telles que votre fichier, gérer le téléchargement, l'attacher au courrier et l'envoyer en quelques lignes de code. – Phliplip