ops dimenticavo gli script
file form.php (che dovrebbe andare nel mio modello)
Codice PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Invia e-mail</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<h1>Invia e-mail</h1>
<?php
if(isset($_GET['err']))
{
?>
<p style="color: red;"><?php echo $_GET['err']; ?></p>
<?php
}
elseif(isset($_GET['sent']))
{
?>
<p style="color: green;">Il messaggio è stato inviato con successo!</p>
<?php
}
?>
<form action="send.php" method="post">
<table cellspacing="10">
<tr>
<td><label>Nome:</label></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><label>Cognome:</label></td>
<td><input type="text" name="surname" /></td>
</tr>
<tr>
<td><label>E-mail:</label></td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td><label>Messaggio:</label></td>
<td><textarea name="message" cols="50" rows="5"></textarea></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Invia" /></td>
<td><input type="reset" name="reset" value="Resetta" /></td>
</tr>
</table>
</form>
</body>
</html>
__________________________________________________ __________
file send.php (che dovrebbe essere un nuovo file)
Codice PHP:
<?php
// specifica la tua email e l'oggetto del messaggio
$your_email = 'admin@site.com';
$subject = 'Nuovo messaggio';
// funzione per la visualizzazione degli errori
function show_error($errtxt)
{
// creo la query per la pagina
$query = array('err' => $errtxt);
$query = http_build_query($query);
// reindirizzo alla pagina
header("Location: form.php?{$query}");
// interrompo lo script
exit();
}
// se il form è stato inviato
if(isset($_POST['submit']))
{
// recupero i dati inviati
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$surname = isset($_POST['surname']) ? trim($_POST['surname']) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
// controlla che i campi non siano vuoti
if($name == '')
show_error('Non hai inserito il tuo nome.');
if($surname == '')
show_error('Non hai inserito il tuo cognome.');
if(!preg_match('/^([\w\-\+\.]+)@([\w\-\+\.]+).([\w\-\+\.]+)$/', $email))
show_error('L\'e-mail che hai fornito non è valida.');
if($message == '')
show_error('Non hai inserito il tuo messaggio.');
// crea il messaggio da inviare
$msgtxt = "Un nuovo messaggio ti è stato inviato da un utente.\n";
$msgtxt = "L'utente ha fornito queste informazioni:\n";
$msgtxt = "---------------------------------------------------\n";
$msgtxt = "Nome: {$name}\n";
$msgtxt = "Cognome: {$surname}\n";
$msgtxt = "E-mail: {$email}\n";
$msgtxt = "---------------------------------------------------\n";
$msgtxt = "{$message}\n";
$msgtxt = "---------------------------------------------------\n";
$msgtxt = "L'indirizzo IP dell'utente è {$_SERVER['REMOTE_ADDR']}.";
// tenta di inviare il messaggio
if(!mail($your_email, $subject, $msgtxt))
show_error('Impossibile inviare il messaggio. Riprova più tardi.');
// visualizza il messaggio di avvenuto invio
header('Location: form.php?sent=true');
}
// altrimenti
{
// reindirizza al file del form
show_error('Non hai inviato i dati. Ritenta.');
}
?>
ciao e grazie