<?php



// Replace this with your own email address

//$siteOwnersEmail = 'brunomartins.1988@gmail.com';





if($_POST) {

	print_r($_POST);

	print_r($_FILES);

	//Get the uploaded file information

	$name_of_uploaded_file = basename($_FILES['uploaded_file']['name']);



	//get the file extension of the file

	$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);



	$size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs



   	$fname = trim(stripslashes($_POST['contactFname']));

   	$lname = trim(stripslashes($_POST['contactLname']));

   	$email = trim(stripslashes($_POST['contactEmail']));

   	$subject = trim(stripslashes($_POST['contactSubject']));

   	$contact_message = trim(stripslashes($_POST['contactMessage']));



   	// Check First Name

	if (strlen($fname) < 2) {

		$error['fname'] = "Por favor preencha o seu primeiro nome.";

	}

	// Check Last Name

	if (strlen($lname) < 2) {

		$error['lname'] = "Por favor preencha o seu ultimo nome.";

	}

	// Check Email

	if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {

		$error['email'] = "Por favor digite um e-mail válido.";

	}

	// Check Message

	if (strlen($contact_message) < 15) {

		$error['message'] = "Por favor digite a sua mensagem. Deve conter um mínimo de 15 caractéres.";

	}

   // Subject

	//if ($subject == '') { $subject = "Contact Form Submission"; }

	if ($subject == 0)

	{

		$subject = "Formulário de contacto - Outros";

	}

	elseif ($subject == 1)

	{

		$subject = "Formulário de contacto - Multas";

	}

	elseif ($subject == 2)

	{

		$subject = "Formulário de contacto - Esclarecimento de dúvidas";

	}

	elseif ($subject == 3)

	{

		$subject = "Formulário de contacto - Aconselhamento Juridico";

	}

	else

	{

		$subject = "Formulário de contacto";	

	}



	// Set Name

	$name = $fname . " " . $lname;



    // Set Message

    $message .= "De: " . $name . "<br />";

	$message .= "Email: " . $email . "<br />";

   	$message .= "Mensagem: <br />";

   	$message .= $contact_message;

   	$message .= "<br /> ----- <br /> Este e-mail foi enviado pelo formulário de contacto do website. <br />";



   	// Set From: header

   	$from =  $name . " <" . $email . ">";



   	// Email Headers

	$headers = "From: " . $from . "\r\n";

	$headers .= "Reply-To: ". $email . "\r\n";

 	$headers .= "MIME-Version: 1.0\r\n";

	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";





   	if (!$error) {



      ini_set("sendmail_from", $siteOwnersEmail); // for windows server

      $mail = mail($siteOwnersEmail, $subject, $message, $headers);



	  if ($mail) 

	  { 

		echo "OK"; 

	  }

      else 

      { 

      	echo "Ocorreu um erro. Por favor tente novamente mais tarde."; 

      }

		

	} # end if - no validation error



	else {



		$response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;

		$response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;

		$response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;

		$response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

		

		echo $response;



	} # end if - there was a validation error



}



?>