<html>
<head>
<title>Modulo contatti</title>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body topmargin="50" margineight="50">
<table width="400" align="center">
<tr>
<td>
<?PHP
// IDENTIFICA UN POTENZIALE ERRORE
$errore = 0;


// Manda mail a
$to = $_POST['to'];

// Mittente

$from = $_POST['from'];

// Controlla ESPRESSIONE REGOLARE DELL'EMAIL
$re_1 = ereg("^[^@ ]+@[^@ ]+.+[^@ ]+$", $from);
$re_2 = ereg("^[^@ ]+@[^@ ]+.+[^@ ]+$", $to);



// RECUPERA I DATI DAL FORM

$nome = $_POST['nome'];
$cognome = $_POST['cognome'];
$indirizzo = $_POST['indirizzo'];
$citta = $_POST['citta'];
$azienda = $_POST['azienda'];
$tel = $_POST['tel'];
$fax = $_POST['fax'];






// Valorizzo le variabili relative all'allegato
$allegato = $_FILES['allegato']['tmp_name'];
$allegato_type = $_FILES['allegato']['type'];
$allegato_name = $_FILES['allegato']['name'];

// Creo 2 variabili che riempirò più avanti...
$headers = "From: " . $from;
$msg = "";

// Verifico se il file è stato caricato correttamente via HTTP
// In caso affermativo proseguo nel lavoro...
if (is_uploaded_file($allegato))
{
// Apro e leggo il file allegato
$file = fopen($allegato,'rb');
$data = fread($file, filesize($allegato));
fclose($file);

// Adatto il file al formato MIME base64 usando base64_encode
$data = chunk_split(base64_encode($data));

// Genero il "separatore"
// Serve per dividere, appunto, le varie parti del messaggio.
// Nel nostro caso separerà la parte testuale dall'allegato
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Aggiungo le intestazioni necessarie per l'allegato
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";

// Definisco il tipo di messaggio (MIME/multi-part)
$msg .= "This is a multi-part message in MIME format.\n\n";

// Metto il separatore
$msg .= "--{$mime_boundary}\n";

// Questa è la parte "testuale" del messaggio
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= $corpo . "\n\n";

// Metto il separatore
$msg .= "--{$mime_boundary}\n";

// Aggiungo l'allegato al messaggio
$msg .= "Content-Disposition: attachment;\n";
$msg .= " filename=\"{$allegato_name}\"\n";
$msg .= "Content-Transfer-Encoding: base64\n\n";
$msg .= $data . "\n\n";

// chiudo con il separatore
$msg .= "--{$mime_boundary}--\n";
}
else
{
$msg = $corpo;
}


// CONTROLLA I CAMPI
if ($nome == "") { $errore = 1; }
elseif ($re_1 == FALSE) { $errore = 2; }
elseif ($cognome == "") { $errore = 3; }
elseif ($re_2 == FALSE) { $errore = 4; }

// SE I CAMPI SONO STATI COMPILATI CORRETTAMENTE...
else
{
// FORMATTA LA MAIL





$corpo = '
<html>
<head>
</head>

<body>
<table width="500" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td colspan="2">';
$corpo .= '<br><br>';


$corpo .= '</td></tr>';
$corpo .= '<tr><td width="50%" valign="top">';
$corpo .= 'Colazione da tiffany';



$corpo .= '<br><br>';
$corpo .= '</td></tr>';
$corpo .= '</table>
</body>
</html>';


$mail_in_html .= "MIME-Version: 1.0\r\n";
$mail_in_html .= "Content-type: text/html; charset=UTF-8\r\n";
$mail_in_html .= "From: $from\r\n";
$mail_in_html .= "BCC:$to\r\n";

if (mail($to,"Imbarcazione $cantiere",$msg,$mail_in_html))

// INVIA LA MAIL CONTROLLANDO CHE L'INVIO VADA A BUON FINE
{
$errore = 0;
}
else
{
$errore = 5;
}
}


?>

<html>
<head>
<title>Invia ad un amico</title>
</head>
<body>

<p align="center">
<?PHP
switch ($errore)
{
case 0;
print "Segnalazione inviata con successo";
break;

case 1;
print "Inserisci il tuo nome";
break;

case 2;
print "Errore mail destinatario";
break;

case 3;
print "Inserisci il cognome";
break;

case 4;
print "Inserisci la tua mail";
break;

case 5;
print "Il server non è riuscito ad inviare la mail";
break;
}
?>
<br><br>
<a href="javascript:window.history.back();">Indietro</a>
</p>

</td>
</tr>
</table>

</body>
</html>