Ciao a tutti prendendo spunto da vari 3D, pillole e articoli su freephp ho iniziato a creare uno script per l'invio di mail che supporta l'allegato (e inoltre prima di spedirlo lo zippa)
Lo script funziona alla grande unico problema non mi spedisce il testo della mail se metto l'allegato... ecco il codice
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;
$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 (qui non mi spedisce il testo della mail)
$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 = "--Message-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";
$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 (qui il testo e' spedito)
$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";
}
}
?>
Ho provato a fare un echo di $testo e la variabile è settata, ho provato a fare echo di $msg_body e mi sembra che sia a posto... se qualcuno ha una soluzione plz non so più cosa inventarmi