<?php
require_once('recaptchalib.php');
require("class.phpmailer.php");
include("class.smtp.php");

$privatekey = "6LfnPeoSAAAAAN0FdHllWIZNNUS9f075Imh9H-jD";
	  $resp = recaptcha_check_answer ($privatekey,
									$_SERVER["REMOTE_ADDR"],
									$_POST["recaptcha_challenge_field"],
									$_POST["recaptcha_response_field"]);
		 if ($resp->is_valid) :
		 
		 	$mail = new PHPMailer();
			
			/*$mail->IsSMTP();             
			$mail->Port = 587;               // set mailer to use SMTP
			//$mail->Host = "mail917.opentransfer.com";  // specify main and backup server
			$mail->Host = "mail.jaynewaynedesign.info";  // specify main and backup server
			$mail->SMTPAuth = true;     // turn on SMTP authentication
			$mail->Username = "authenticate@jaynewaynedesign.info";  // SMTP username
			$mail->Password = "authme"; // SMTP password*/
			
			$mail->IsSMTP(); // telling the class to use SMTP
			$mail->Host       = "mail.skynetindia.info"; // SMTP server
			$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
													   // 1 = errors and messages
													   // 2 = messages only
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
			$mail->Username   = "hr@skynetindia.info";  // GMAIL username
			$mail->Password   = "sd&*(&*$&#";            // GMAIL password
			
			
			$mail->From = $_POST['email_from'];
			$mail->FromName = "skynet";
			
			$mail->AddAddress("developer4@skynettechnologies.com");//
			$mail->WordWrap = 50;                                 // set word wrap to 50 characters
			$mail->AddAttachment("/var/tmp/file.tar.gz");         // add attachments
			$mail->AddAttachment("/tmp/image.jpg", "new.jpg");    // optional name
			$mail->IsHTML(true);                                  // set email format to HTML
			
			function get_form_data(){
			
				global $_REQUEST;
			
				global $_POST;
			
				global $_GET;
			
				
				unset($_POST['g-recaptcha-response']);
				
			
				$vars = ($_REQUEST == 'GET') ? $_GET : $_POST;
			
				//strip spaces from all fields
			
				foreach ($vars as $k=>$v) $vars[$k] = trim($v);
			
				return $vars;
			
			}
			
			$vars=get_form_data();
			
			$database_file = 'email.csv';
			$database_fields = '*'; 
			
			
			function save_form($vars){
			
				global $database_file, $database_fields;
			
				$f = fopen($database_file, 'a');
			
				if (!$f){
			
					die("Cannot open db file for save");
			
				}
			
				foreach ($vars as $k=>$v) {
			
					$vars[$k] = str_replace(array("|", "\r","\n"), array('_',' ',' '), $v);
			
				}
			
				if (is_array($database_fields)) {
			
					$vars_orig = $vars; 
			
					$vars = array();
			
					foreach ($database_fields as $k)
			
						$vars[$k] = $vars_orig[$k];
			
				}
			
				$str = join('|', $vars);
			
				fwrite($f, $str."\n");
			
				fclose($f);
			
			}
			
			function _build_fields($vars){
			
				$skip_fields = array(
			
					'name_from', 
			
					'email_to', 
			
					'name_to', 
			
					'subject',
					
					'captchacode', 
			
					'f_security', 
			
					'hdcaptcha');
			
				// order by numeric begin, if it exists
			
				$is_ordered = 0;
			
				foreach ($vars as $k=>$v) 
			
					if (in_array($k, $skip_fields)) unset($vars[$k]);
			
			
			
				$new_vars = array();
			
				foreach ($vars as $k=>$v){
			
					// remove _num, _reqnum, _req from end of field names
			
					$k = preg_replace('/_(req|num|reqnum)$/', '', $k);
			
					// check if the fields is ordered
			
					if (preg_match('/^\d+[ \:_-]/', $k)) $is_ordered++;
			
					//remove number from begin of fields
			
					$k = preg_replace('/^\d+[ \:_-]/', '', $k);
			
					$new_vars[$k] = $v;
			
				}
			
				$vars = $new_vars;
			
			
			
				$max_length = 10; // max length of key field 
			
				foreach ($vars as $k=>$v) {
			
					$klen = strlen($k);
			
					if (($klen > $max_length) && ($klen < 40))
			
						$max_length = $klen;
			
				}
			
			
			
				if ($is_ordered)
			
					ksort($vars);
			
			
			
				// make output text
			
				$out = "";
				$out="<table>";
			
				foreach ($vars as $k=>$v){
			
					$k = str_replace('f_', ' ', $k);
					$k = str_replace('_', ' ', $k);
			
					$k = ucfirst($k);
			
					$len_diff = $max_length - strlen($k);
			
					if ($len_diff > 0) 
			
						$fill = str_repeat('.', $len_diff);
			
					else 
			
						$fill = '';
					$out .= "<tr><td>".$k."</td><td> : </td> </td><td>$v</td></tr>";
			
				}
				$out=$out."</table>";
				return $out;
			
			}
			
			$str= _build_fields($vars);
			$mail->Subject = "Form Submited From skynettechnologies";
			$mail->Body    = $str;
			echo $mail->Body;
			//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
			
			if(!$mail->send())
			{
			   echo "Message could not be sent. <p>";
			   echo "Mailer Error: " . $mail->ErrorInfo;
			   exit;
			}else{
					echo "Message has been sent";
			}
			
		
			
else:
        echo ("<SCRIPT LANGUAGE='JavaScript'>
				alert('Robot verification failed, please try again.');
				return false;
				</SCRIPT>");
        endif;


?>