I have this code and it works. I have only one side effect and I'd like to know how to solve it.. In my form I have three file selectors. The user can, if he want, upload up to three files.. If the user upload three files I receive the email plus the attachments without problems, everything is perfect! but if the user upload one or only two files I receive the mail plus the attachment but I have receive also an empty "noname" attachment.. Is there a way to avoid this empty attachment? (if the user uploads only one file I have two empty attachments while if he uploads two files I have one empty attachmetns..)
Here the code:
$to = "my.email@mail.com";
$email = $_POST['email'];
$subject = "Proposta di Vendita/Affitto";
$nome = $_POST['v_nome'];
$email = $_POST['v_email'];
$tel = $_POST['v_tel'];
$categoria = $_POST['categoria_2'];
$tipologia = $_POST['tipologia_2'];
$comune = $_POST['comune_2'];
$camere = $_POST['v_camere'];
$bagni = $_POST['v_bagni'];
$stanze = $_POST['v_stanze'];
$balconi = $_POST['v_balconi'];
$terrazzi = $_POST['v_terrazzi'];
$box = $_POST['v_box'];
$cantina = $_POST['v_cantina'];
$trattamento_dati_personali = $_POST['checkbox_dati_personali'];
$query_1 = mysql_query("SELECT categoria.nome FROM categoria WHERE categoria.id = '$categoria'");
while($rows=mysql_fetch_array($query_1)){
$categoria = $rows['nome'];
}
$query_2 = mysql_query("SELECT tipologia.nome FROM tipologia WHERE tipologia.id = '$tipologia'");
while($rows=mysql_fetch_array($query_2)){
$tipologia = $rows['nome'];
}
$query_3 = mysql_query("SELECT comuni.nome FROM comuni WHERE comuni.id = '$comune'");
while($rows=mysql_fetch_array($query_3)){
$comune = $rows['nome'];
}
$message .= "Ricevi questa email perché ti é stata inviata una richiesta dal Sig/Sig.ra:" . "<br /><br />";
$message .= "<b>" . "Nome: " . "</b>" . $nome . "<br />";
$message .= "<b>" . "Email: " . "</b>" . $email . "<br />";
$message .= "<b>" . "Tel: " . "</b>" . $tel . "<br />";
$message .= "<b>" . "Categoria: " . "</b>" . $categoria . "<br />";
$message .= "<b>" . "Tipologia: " . "</b>" . $tipologia . "<br />";
$message .= "<b>" . "Comune: " . "</b>" . $comune . "<br />";
$message .= "<b>" . "Camere: " . "</b>" . $camere . "<br />";
$message .= "<b>" . "Bagni: " . "</b>" . $bagni . "<br />";
$message .= "<b>" . "Stanze: " . "</b>" . $stanze . "<br />";
$message .= "<b>" . "Balconi: " . "</b>" . $balconi . "<br />";
$message .= "<b>" . "Terrazzi: " . "</b>" . $terrazzi . "<br />";
$message .= "<b>" . "Box: " . "</b>" . $box . "<br />";
$message .= "<b>" . "Cantina: " . "</b>" . $cantina . "<br />";
$message .= "<b>" . "Trattamento Dati Personali: " . "</b>" . $trattamento_dati_personali . "<br />";
// Temporary paths of selected files
$file1 = $_FILES['file1']['tmp_name'];
$file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['tmp_name'];
// File names of selected files
$filename1 = $_FILES['file1']['name'];
$filename2 = $_FILES['file2']['name'];
$filename3 = $_FILES['file3']['name'];
// array of filenames to be as attachments
$files = array($file1, $file2, $file3);
$filenames = array($filename1, $filename2, $filename3);
// include the from email in the headers
$headers = "From: $email";
// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";
// headers used for send attachment with email
$headers .= "
MIME-Version: 1.0
" . "Content-Type: multipart/mixed;
" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}
" . "Content-Type: text/html; charset=\"iso-8859-1\"
" . "Content-Transfer-Encoding: 7bit
" . $message . "
";
$message .= "--{$boundary}
";
// attach the attachments to the message
for($x = 0; $x < count($files); $x++){
$file = fopen($files[$x],"r");
$content = fread($file,filesize($files[$x]));
fclose($file);
$content = chunk_split(base64_encode($content));
$message .= "Content-Type: {\"application/octet-stream\"};
" . " name=\"$files[$x]\"
" . "Content-Disposition: attachment;
" . " filename=\"$filenames[$x]\"
" . "Content-Transfer-Encoding: base64
" . $content . "
";
$message .= "--{$boundary}
";
}
// sending mail
$sendmail = mail($to, $subject, $message, $headers);
// verify if mail is sent or not
if ($sendmail) {
echo "Richiesta inviata correttamente";
} else {
echo "Errore durante l'invio";
}
header("Location: post_vendita.php");
exit;