Guarda a caso ho proprio lo script che fa al caso tuo ovviamente è da adattare e ci sono vari controlli da aggiungere ma le basi ci sono:
Lo script comprende 3 file: index.php che contiene il form di invio, mail.php che compie la funzione di postino e pclzip.lib.php libreria in cui è definita la classe pclzip (http://www.phpconcept.net/pclzip/index.en.php per la documentazione).
Lo script prevede anche l'esistenza di una cartella tmp.
Index.php
Codice PHP:
<html>
<head>
<title>Documento senza titolo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form action="mail.php" method="post" enctype="multipart/form-data" name="mail">
</p>
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<tr>
<td>Indirizzo</td>
<td><input name="address" type="text" id="address"></td>
</tr>
<tr>
<td>Oggetto</td>
<td><input name="subject" type="text" id="subject"></td>
</tr>
<tr>
<td>Allegato</td>
<td><input name="attch" type="file" id="attch"></td>
</tr>
<tr>
<td colspan="2"> <div align="left">
<textarea cols="60" rows="10" name="testo" id="testo"></textarea>
</div></td>
</tr>
<tr>
<td><input type="submit" name="invia" value="Invia"></td>
<td><input type="reset"></td>
</tr>
</table>
</p>
</form>
</body>
</html>
Mail.php
Codice PHP:
<?php
function missing_val()
{
echo "Un dato necessario per l'invio della mail è mancante";
exit;
}
if(!file_exists("pclzip.lib.php"))
{
echo "Una libreria indispensabile per lo script non è stata trovata";
}
else
{
include("pclzip.lib.php");
$mittente = "me@localhost";
$reply = $mittente;
if(isset($_POST['testo'])) $testo = $_POST['testo'];
if(!isset($_POST['address']) && !isset($_POST['subject']))
{
missing_val();
}
else
{
$destinatario = $_POST['address'];
$oggetto = $_POST['subject'];
}
if(!empty($_FILES['attch']['tmp_name']))
{
//MAIL CON ALLEGATO
$attch_tmp = $_FILES['attch']['tmp_name'];
$attch_name = $_FILES['attch']['name'];
move_uploaded_file($attch_tmp,"./tmp/".$attch_name);
$n = explode(".",$attch_name);
$l = count($n);
$n[$l-1] = "zip";
$nome = implode(".",$n);
$zip = new PclZip("./tmp/".$nome);
$zip->add("./tmp/"."$attch_name",PCLZIP_OPT_REMOVE_PATH, "tmp");
$attch_type = mime_content_type("./tmp/".$nome);
$attch_size = filesize("./tmp/".$nome);
$boundary = md5(uniqid(microtime()));
$intestazioni = "From: $mittente\n";
$intestazioni .= "Reply-To: $reply\n";
$intestazioni .= "MIME-version: 1.0\n";
$intestazioni .= "Content-type: multipart/mixed;\n boundary=\"$boundary\"\n";
$intestazioni .= "Content-transfer-encoding: 7BIT\nX-attachments: $nome";
$body_top = "--$boundary\n";
$body_top .= "Content-type: text/html; charset=iso-8859-1\n";
$body_top .= "Content-transfer-encoding: 7BIT\n";
$body_top .= "Content-description: Mail message body\n\n";
$msg_body = $body_top . $testo;
$filez = fopen("./tmp/".$nome, "r");
$contents = fread($filez, $attch_size);
$encoded_attach = chunk_split(base64_encode($contents));
fclose($filez);
$msg_body .= "\n\n--$boundary\n";
$msg_body .= "Content-type: $attch_type; name=\"$nome\"\n";
$msg_body .= "Content-Transfer-Encoding: BASE64\n";
$msg_body .= "Content-disposition: attachment; filename=\"$nome\"\n\n";
$msg_body .= "$encoded_attach\n";
$msg_body .= "--$boundary--\n";
unlink("./tmp/".$attch_name);
unlink("./tmp/".$nome);
}
else
{
//MAIL SENZA ALLEGATO
$intestazioni = "From: $mittente\n";
$intestazioni .= "Reply-To: $reply\n";
$intestazioni .= "MIME-Version: 1.0\r\n";
$intestazioni .= "Content-type: text/html; charset=iso-8859-1\r\n";
$msg_body = $testo;
}
if(@mail($destinatario,$oggetto,$msg_body,$intestazioni))
{
echo "Mail inviata con successo";
}
else
{
echo "Errore nell'invio della mail";
}
}
?>
Se ti servono delucidazioni chiedi pure