<?php

if (isset($_POST['career'])) {

	$from_email = 'icvassessments@gmail.com'; //from mail, sender email addrress 
	$recipient_email = 'icvassessments@gmail.com'; //recipient email addrress 

	//Load POST data from HTML form 
	$sender_name = $_POST['fname'] . $_POST['lname'];  //sender name
	$reply_to_email = $_POST["email"]; //sender email, it will be used in "reply-to" header 
	$subject = "Career..Us"; //subject for the email 
	$message = $_POST["message"]; //body of the email 


	/*Always remember to validate the form fields like this 
	if(strlen($sender_name)<1) 
	{ 
		die('Name is too short or empty!'); 
	} 
	*/

	//Get uploaded file data using $_FILES array 
	$tmp_name = $_FILES['resume_pdf']['tmp_name']; // get the temporary file name of the file on the server 
	$file_name = $_FILES['resume_pdf']['name']; // get the name of the file 
	$size = $_FILES['resume_pdf']['size']; // get size of the file for size validation 
	$file_type = $_FILES['resume_pdf']['type']; // get type of the file 
	$file_error = $_FILES['resume_pdf']['error']; // get the error (if any) 

	//validate form field for attaching the file 
	if ($file_error > 0) {
		die('Upload error or No files uploaded');
	}

	//read from the uploaded file & base64_encode content 
	$handle = fopen($tmp_name, "r"); // set the file handle only for reading the file 
	$content = fread($handle, $size); // reading the file 
	fclose($handle);				 // close upon completion 

	$encoded_content = chunk_split(base64_encode($content));

	$boundary = md5("random"); // define boundary with a md5 hashed value 

	//header 
	$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
	$headers .= "From:" . $from_email . "\r\n"; // Sender Email 
	$headers .= "Reply-To: " . $reply_to_email . "\r\n"; // Email addrress to reach back 
	$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 
	$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
	$headers .= "boundary = $boundary\r\n"; //Defining the Boundary 

	//plain text 
	$body = "Name : " . $sender_name . "<br>";
	$body = "salary : " . $_POST['salary'] . "<br>";
	$body = "starting : " . $_POST['start'] . "<br>";
	$body .= "Phone : " . $_POST["phone"] . "<br>";
	$body .= "Fax Id : " . $_POST["fax"] . "<br>";
	$body .= "Position  : " . $_POST["position_value"] . "<br>";

	$body .= "<div style='color: red'>" . $message . "</div>\r\n\r\n";


	$body = "--$boundary\r\n";
	$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
	$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
	$body .= chunk_split(base64_encode($message));

	//attachment 
	$body .= "--$boundary\r\n";
	$body .= "Content-Type: $file_type; name=" . $file_name . "\r\n";
	$body .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
	$body .= "Content-Transfer-Encoding: base64\r\n";
	$body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
	$body .= $encoded_content; // Attaching the encoded file with email 




	$secretKey = "6Lcn9IcjAAAAAHS4iNBMS6bV4Bti30VG9-GHwaER";
	$responseKey = $_POST['g-recaptcha-response'];
	$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey";
	$response = file_get_contents($url);
	$response = json_decode($response);



	$sentMailResult = mail($recipient_email, $subject, $body, $headers);


	if ($response->success) {

		if ($sentMailResult) {
			echo "<script>alert('Thank You " . $me . " your Career was sent');</script>";
			//echo "File Sent Successfully."; 
			unlink($file_name); // delete the file after attachment sent. 
			echo "<script>window.location.href='career_f.php';</script>";
		} else {
			echo "<script>alert('Sorry but the email could not be sent Please go back and try again!');</script>";

			echo "<script>window.location.href='career_f.php';</script>";

		}
	} else {
		echo "<script> alert('Verification Failed'); </script>";
		echo "<script> window.location.href='career_f.php'; </script>";
	}
}

?>