Salve a tutti!Vi chiedo cortesemente un grande favore,non essendo esperto di php.
Devo creare un form mail che abbia i campi obbligatori e che permetta anche di allegare un file (qualsiasi).
Il suo contenuto deve essere più o meno così :
Nome*
Cognome*
Mail*
Città
Telefono*
Codice fiscale*
Partita Iva*
Allega file*
*= campi obbligatori
Ho provato molti script, ma con molti script se inserisco l'allegato mi arrivano per email anche tutti i dati nome, cognome, mail, citta', mentre se non inserisco allegato l'email mi arriva vuota.
Io voglio che anche non inserendo l'allegato l'email venga vista correttamente con tutti i suoi dati.
E' possibile?
Vi posto il codice php che ho utilizzato
<?php 
 
$to= "mia email"; 
$nome=$_POST['nome']; 
$cognome=$_POST['cognome']; 
$oggetto=$_POST['oggetto']; 
$mail=$_POST['mail']; 
$citta=$_POST['citta']; 
$telefono=$_POST['telefono']; 
$codice_fiscale=$_POST['codice_fiscale']; 
$p_iva=$_POST['p_iva']; 
 
$messaggio=' 
 
Nome : 
'.$nome.' 
 
Cognome : 
'.$cognome.' 
 
Oggetto : 
'.$oggetto.' 
 
Mail : 
'.$mail.' 
 
Città : 
'.$citta.' 
 
Telefono : 
'.$telefono.' 
 
Codice Fiscale : 
'.$codice_fiscale.' 
 
p_iva : 
'.$p_iva.' 
 
'; 
 
$allegato = $_FILES['allegato']['tmp_name']; 
$allegato_type = $_FILES['allegato']['type']; 
$allegato_name = $_FILES['allegato']['name']; 
 
$headers = "From: " . $mail; 
$msg = ""; 
 
if (is_uploaded_file($allegato)) 
{ 
$file = fopen($allegato,'rb'); 
$data = fread($file, filesize($allegato)); 
fclose($file); 
$data = chunk_split(base64_encode($data)); 
$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
 
$headers .= "\nMIME-Version: 1.0\n"; 
$headers .= "Content-Type: multipart/mixed;\n"; 
$headers .= " boundary=\"{$mime_boundary}\""; 
$msg .= "This is a multi-part message in MIME format.\n\n"; 
$msg .= "--{$mime_boundary}\n"; 
 
 
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
$msg .= "Content-Transfer-Encoding: 7bit\n\n"; 
$msg .= $messaggio . "\n\n"; 
$msg .= "--{$mime_boundary}\n"; 
 
$msg .= "Content-Disposition: attachment;\n"; 
$msg .= " filename=\"{$allegato_name}\"\n"; 
$msg .= "Content-Transfer-Encoding: base64\n\n"; 
$msg .= $data . "\n\n"; 
 
$msg .= "--{$mime_boundary}--\n"; 
} 
else 
{ 
echo "<p>Errore!</p>"; 
} 
 
 
if (mail($to, $oggetto, $msg, $headers)) 
{ 
echo "<p>Mail inviata con successo!</p>"; 
}else{ 
echo "<p>Errore!</p>"; 
} 
?> 
il file form.html è questo
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> 
<html xmlns=""> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Form mail</title> 
 
<script type="text/javascript" language="javascript"> 
function valida(){ 
if (!confirm('Confermi i dati inseriti?')) return false; 
 
if (document.getElementById('nome').value=='') { 
alert('Compilare correttamente il campo Nome!'); 
document.getElementById('nome').focus(); 
return false; 
} 
 
if (document.getElementById('cognome').value=='') { 
alert('Compilare correttamente il campo Cognome!'); 
document.getElementById('cognome').focus(); 
return false; 
} 
 
if (document.getElementById('mail').value=='') { 
alert('Compilare correttamente il campo Mail!'); 
document.getElementById('mail').focus(); 
return false; 
} 
 
if (document.getElementById('citta').value=='') { 
alert('Compilare correttamente il campo Città!'); 
document.getElementById('citta').focus(); 
return false; 
} 
 
if (document.getElementById('telefono').value=='') { 
alert('Compilare correttamente il campo Telefono!'); 
document.getElementById('telefono').focus(); 
return false; 
} 
 
if (document.getElementById('codice_fiscale').value=='') { 
alert('Compilare correttamente il campo Codice_fiscale!'); 
document.getElementById('codice_fiscale').focus(); 
return false; 
} 
 
if (document.getElementById('p_iva').value=='') { 
alert('Compilare correttamente il campo P_iva!'); 
document.getElementById('p_iva').focus(); 
return false; 
} 
 
 
return true; 
}//valida 
</script> 
 
</head> 
 
<body> 
<form action="invio.php" method="post" enctype="multipart/form-data" onsubmit="return valida();"> 
<input type="hidden" name="destinatario" value="emailmia"> 
<table align="center" width="600" cellpadding="2" cellspacing="2"> 
<tr> 
<td>Nome *</td> 
<td><input type="text" name="nome" id="nome" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Cognome *</td> 
<td><input type="text" name="cognome" id="cognome" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Oggetto</td> 
<td><input name="oggetto" type="text" id="oggetto" value="oggetto" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Mail *</td> 
<td><input type="text" name="mail" id="mail" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Città *</td> 
<td><input type="text" name="citta" id="citta" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Telefono *</td> 
<td><input type="text" name="telefono" id="telefono" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Codice fiscale *</td> 
<td><input type="text" name="codice_fiscale" id="codice_fiscale" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>P iva *</td> 
<td><input type="text" name="p_iva" id="p_iva" size="30" maxlength="100" /></td> 
</tr> 
<tr> 
<td>Allegato *<br /> 
</td> 
<td><input type="file" name="allegato" id="allegato" /></td> 
</tr> 
</table> 
<div align="center"> 
<input type="submit" value="Invia dati" /> 
</div> 
</form> 
</body> 
</html>
Il problema è che se io mando la mail con allegato allora vedo sia l'allegato inserito sia i dati inseriti, se invece invio senza allegato la mail che arriva è completamente vuota.
Potete aiutarmi gentilmente?
Mi spiego meglio:
Io voglio che sia con allegato sia senza allegato, i dati inseriti arrivino correttamente quando ricevo la mail. Attualmente cosi come è ricevo i dati SOLO se viene inserito l'allegato.
Potete aiutarmi?