<?php  

    session_start();
	
	// Enter your Name here!
	$yourname = "Your Name";
	
	// Enter your Email Address here!
	$youremail = "example@example.com";
	
	// Enter your Desired Email Subject!
	$mailsubject = "Message from your Support Form";
	
	// Allowed file types add file extensions WITHOUT the dot!
    $allow_types = array ('image/pjpeg','image/gif','image/tiff','image/jpeg','image/png','application/msword','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/pdf','aplication/xpdf','application/x-rar-compressed','application/zip');

	// Maximum file size for attachments in KB NOT Bytes for simplicity.
	// 2048kb = 2MB, 1024kb = 1MB, 512kb = 1/2MB 
	$max_file_size = 1024 * 1024 *2;
	
	$firstname = strip_tags(trim($_POST["firstname"]));
	$lastname = strip_tags(trim($_POST["lastname"]));	
	$useremail = strip_tags(trim($_POST["useremail"]));
	$usersubject = strip_tags(trim($_POST["usersubject"]));
	$usermessage = strip_tags(trim($_POST["usermessage"]));
    $verification = strip_tags(trim($_POST["verification"]));
	
	if (empty($firstname)) {
	    echo '<div class="contact-error-message">Attention! Please enter your First Name</div>';
    }
	elseif(empty($lastname)) {
	    echo '<div class="contact-error-message">Attention! Please enter your Last Name</div>';
    }
    elseif(empty($useremail)) {
	    echo '<div class="contact-error-message">Attention! Please enter your Email</div>';
    } 
    elseif(!preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$useremail)){
	    echo '<div class="contact-error-message">Attention! Enter a valid Email Address</div>';
    }
    elseif(empty($usersubject)) {
	    echo '<div class="contact-error-message">Attention! Please enter your Subject</div>';
    }
	elseif(empty($usermessage)) {
	    echo '<div class="contact-error-message">Attention! Please enter your Message</div>';
    } 
	elseif(empty($_FILES['userfile1']['name'])){
	    echo '<div class="contact-error-message">Attention! Please enter your First Upload File</div>';
    }
	elseif(empty($_FILES['userfile2']['name'])){
	    echo '<div class="contact-error-message">Attention! Please enter your Second Upload File</div>';
    }
	elseif(empty($_FILES['userfile3']['name'])){
	    echo '<div class="contact-error-message">Attention! Please enter your Third Upload File</div>';
    }
	elseif(array_search($_FILES['userfile1']['type'], $allow_types) === false) {
	    echo '<div class="contact-error-message">In First Upload Files Allowed are Images & Doc Docx Pdf Rar Zip</div>';
    }
	elseif(array_search($_FILES['userfile2']['type'], $allow_types) === false) {
	    echo '<div class="contact-error-message">In Second Upload Files Allowed are Images & Doc Docx Pdf Rar Zip</div>';
    }
	elseif(array_search($_FILES['userfile3']['type'], $allow_types) === false) {
	    echo '<div class="contact-error-message">In Third Upload Files Allowed are Images & Doc Docx Pdf Rar Zip</div>';
    }
	elseif($_FILES['userfile1']['size'] > $max_file_size) {
	    echo '<div class="contact-error-message">Attention! In First Upload Max size of file is 2048 KB</div>';
    }
	elseif($_FILES['userfile2']['size'] > $max_file_size) {
	    echo '<div class="contact-error-message">Attention! In Second Upload Max size of file is 2048 KB</div>';
    }
	elseif($_FILES['userfile3']['size'] > $max_file_size) {
	    echo '<div class="contact-error-message">Attention! In Third Upload Max size of file is 2048 KB</div>';
    }
    elseif(empty($verification)) {
	    echo '<div class="contact-error-message">Attention! Please enter Verification Code</div>';
    } 
    elseif($verification != '18') {
	    echo '<div class="contact-error-message">Attention! Enter a valid Verification Code</div>';
    }
	else {
	
        $random_file1 = uniqid();
		$random_file2 = uniqid();
		$random_file3 = uniqid();
        $finalname1 = $random_file1.$_FILES['userfile1']['name'];
        $finalname2 = $random_file2.$_FILES['userfile2']['name'];
        $finalname3 = $random_file3.$_FILES['userfile3']['name'];
		
        if ($_FILES['userfile1']['error'] == 0 || $_FILES['userfile2']['error'] == 0 || $_FILES['userfile3']['error'] == 0) {
		    move_uploaded_file($_FILES['userfile1']['tmp_name'], '../upload/' .$finalname1);
		    move_uploaded_file($_FILES['userfile2']['tmp_name'], '../upload/' .$finalname2);
		    move_uploaded_file($_FILES['userfile3']['tmp_name'], '../upload/' .$finalname3);
			
		require "class.autoload.php";
        require "message.php";
		
		$mail = new PHPMailer();
		$mail->IsSMTP();
		$mail->SMTPAuth = true; 
		$mail->SMTPSecure = 'ssl'; 
		$mail->Host = 'your-host';
		$mail->Port = 465;
		$mail->Username = 'your-username';
		$mail->Password = 'your-password';
		
		$mail->IsHTML(true);
		$mail->From = $useremail;
		$mail->CharSet = "UTF-8";
		$mail->FromName = $firstname . $lastname;
		$mail->Encoding = "base64";
		$mail->Timeout = 200;
		$mail->ContentType = "text/html";
		$mail->addAddress($youremail, $yourname);
		$mail->Subject = $mailsubject;
		$mail->AddAttachment('../upload/'.$finalname1);
		$mail->AddAttachment('../upload/'.$finalname2);
		$mail->AddAttachment('../upload/'.$finalname3);	
		$mail->Body = $message;
		$mail->AltBody = "To view this message, please use an HTML compatible email";
			
		// Use this if you want more that one user receive a email from contact form ... Just turn True or False!
		$recipients = false;
		if($recipients == true){
			$recipients = array(
				"example@example.com" => "Person Name",
				"example@example.com" => "Person Name",
			);
		
			foreach($recipients as $email => $name){
				$mail->AddBCC($email, $name);
			}	
		}
		
		if($mail->Send()) {
		
		    require "automessage.php";
			
			$automail = new PHPMailer();
		    $automail->IsSMTP();
		    $automail->SMTPAuth = true; 
		    $automail->SMTPSecure = 'ssl'; 
		    $automail->Host = 'your-host';
		    $automail->Port = 465;
		    $automail->Username = 'your-username';
		    $automail->Password = 'your-password';
            $automail->From = $youremail;
            $automail->FromName = $yourname;
			$automail->isHTML(true);                                 
		    $automail->CharSet = "UTF-8";
		    $automail->Encoding = "base64";
		    $automail->Timeout = 200;
		    $automail->ContentType = "text/html";
            $automail->AddAddress($useremail, $firstname . $lastname);
            $automail->Subject = "We received your ticket from our Support form";
            $automail->Body = $automessage;
		    $automail->AltBody = "To view this message, please use an HTML compatible email";
      
            if($automail->Send()){
			    // Email has sent successfully.
			    echo '<div class="contact-success-message">Congratulations! Your Message has been Send</div>';
            }
			else {
			    // Email have an Error.	
			    echo '<div class="contact-error-message">Attention! There is an Error</div>';
		    }
		} 
	  }
	}
?>