codice HTML:
<?php
header('Content-type: text/html; charset=utf-8');
//impostazioni
$attachmentpresent = FALSE;
$destinName = "Paola & Alberto";
$destinAddress = "commerciale@lalegatoria.it";
$smtpHost="lalegatoria.it";
$smtpUsername="*****@lalegatoria.it";
$smtpPassword="******";
$allowedSubjects = array("","legatoria","restauro","grafica","stampa","amministrazione","altro");
$allowedExts = array("jpeg", "jpg", "doc", "docx", "pdf", "zip", "rar");
$allowedMimes = array("image/jpeg","image/pjpeg","application/msword","application/pdf","application/x-zip","application/x-rar-compressed");
//ricezione dati dalla maschera //ho commentato queste linee perchè non includi nessun file esterno che definisca queste funzioni che chiami
$email = $_POST['email'];
$name = $_POST['name']; //strlencheck($name,0,60,"Hai indicato un nome troppo lungo, o non hai indicato un nome proprio. Torna indietro e correggi l'errore.");
$companyname = $_POST['companyname']; //strlencheck($companyname,0,60,"Hai indicato un nome aziendale troppo lungo, o non hai indicato un nome proprio. Torna indietro e correggi l'errore.");
$phone = $_POST['phone']; //checkphone($phone,0,"Hai indicato un numero di telefono improprio. Torna indietro e correggi l'errore.");
$besttime = $_POST['besttime'];
$subject="";
if (!isset($_POST['subject'])
|| !in_array($_POST['subject'],$allowedSubjects)) :
die ("Non hai scelto l'oggetto del tuo messaggio. Torna indietro e correggi l'errore.");
else:
$subject = $_POST['subject'];
endif;
$message= $_POST['message'];
/* --------- attachments settings ------ */
$allowedExts = array("jpeg", "jpg", "doc", "docx", "pdf", "zip", "rar");
$allowedTypes = array("image/jpeg","image/pjpeg","application/msword","application/pdf","application/x-zip","application/x-rar-compressed");
if (!array_key_exists("attachment",$_FILES) || trim($_FILES["attachment"]["name"])==""):
//nessun allegato caricato
else:
if ($_FILES["attachment"]["error"] > 0):
//errore di upload, quindi non devo occuparmi del file uploadato
echo "Errore di caricamento: " . $_FILES["attachment"]["error"] . "<br>";
else:
if (file_exists("attachments/" . $_FILES["attachment"]["name"])):
//il file esiste già
echo "Errore di caricamento, il file ".$_FILES["attachment"]["name"]." esiste già.";
else:
$temp = explode(".", $_FILES["attachment"]["name"]);
$extension = end($temp);
if (!in_array($_FILES["attachment"]["type"],$allowedMimes)
|| ($_FILES["attachment"]["size"] > 800000)
|| !in_array($extension, $allowedExts) ):
echo "Errore: il file allegato non è di un tipo permesso o è troppo grande.<br>";
else:
echo "Nome file originario: " . $_FILES["attachment"]["name"] . "<br>";
echo "Mime type del file: " . $_FILES["attachment"]["type"] . "<br>";
echo "Dimensione del file: " . ($_FILES["attachment"]["size"] / 1024) . " Kb<br>";
echo "File temporaneo: " . $_FILES["attachment"]["tmp_name"];
if (move_uploaded_file($_FILES["attachment"]["tmp_name"],"attachments/" . $_FILES["attachment"]["name"])):
echo "<br>File salvato in: " . "attachments/" . $_FILES["attachment"]["name"];
$attachmentpresent = true;
else:
echo "Errore spostando il file in: " . "attachments/" . $_FILES["attachment"]["name"];
endif;
endif;
endif;
endif;
endif;
/* ------------------ SMTP EMAIL SETTINGS ------------------------- */
//caricamento librerie
require_once("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
//parametri server smtp
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = $smtpHost; // SMTP server
$mail->SMTPDebug = 1; // 0 for production use, 1 errors and messages, 2 messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPPort = 25; // set the SMTP port for the GMAIL server
$mail->Username = $smtpUsername; // SMTP account username
$mail->Password = $smtpPassword; // SMTP account password
//parametri messaggio email
$mail->WordWrap = 150;
$mail->CharSet = 'UTF-8'; // so it interprets foreign characters
$mail->From = $smtpUsername;
$mail->ReplyTo = $email;
$mail->AddAddress($destinAddress, $destinName);
//se presente un allegato provo anche ad allegarlo alla email
if ($attachmentpresent):
$mail->AddAttachment("attachments/" . $_FILES["attachment"]["name"]); // attachment
endif;
//oggetto e corpo email
$mail->Subject = "RICHIESTA PREVENTIVO DAL NS. SITO WEB";
$mail->Body .= "Abbiamo appena ricevuto una richiesta di preventivo da: ".$name."\n";
$mail->Body .= "Nome dell'azienda: ".$companyname."\n\n";
$mail->Body .= "Contattare al numero: ".$phone."\n";
$mail->Body .= "preferibilmente: ".$besttime."\n";
$mail->Body .= "oppure all'indirizzo email: ".$email."\n\n";
$mail->Body .= "Il messaggio ha per oggetto: ".$subject."\n";
$mail->Body .= "Testo del messaggio: ".$message."\n\n";
//in ogni caso nella mail metto il link al file sul server
if ($attachmentpresent):
$mail->Body .= "Il cliente ha inviato un allegato, che puoi scaricare da: http://www.lalegatoria.it/contact/attachments/".$_FILES["attachment"]["name"]."\n\n";
else:
$mail->Body .= "Il cliente non ha inviato un allegato.\n\n";
endif;
$mail->Body .= "Non dimentichiamoci che la richiesta ha carattere di urgenza!";
//invio email
if(!$mail->Send()):
//fallito
echo 'Non è stato possibile inviare il Vs. messaggio.';
echo 'Mailer error: ' . $mail->ErrorInfo;
else:
//riuscito
echo '<br>La Vs. email è stata inviata. Riceverete ns. riscontro a breve.';
//decommenta questa linea per eliminare il file una volta speditolo correttamente come allegato
//unlink("attachments/" . $_FILES["attachment"]["name"]);
endif;
?>