<?php
//captcha
include "captcha.config.php";
session_start();
//settings
$site_name = 'http://www.antepartners.com'; //used to catch spammers - make sure to have http://www. in front.

$send_email_to = 'mat.small@antepartners.com'; //recipient(s), to add more than one list them following by ; ie email1@g.com;email2@g.com;email3@g.com
$email_subject = 'AntePartners Web Site Email'; //subject

$field_name_to_use_for_From = ''; //leave empty if none;
$fake_email_to_use_for_From = 'no-reply@antepartners.com'; //only used if above if empty

$field_name_to_use_for_Reply_To = ''; //leave empty if none;
$fake_email_to_use_for_Reply_To = $_POST['email']; //only used if above if empty

$path_to_thank_u_page = 'http://www.antepartners.com/thankyou.html'; //where script should go on succesful send?
$path_to_error_page = 'http://www.antepartners.com/error.html'; //where script should go on error?

$table_form = true; //output email in table form?
$table_id = 'test'; //only used if above set to true
$table_cellpadding = '1'; //only used if above set to true
$table_cellspacing = '2'; //only used if above set to true
$table_border = '3'; //only used if above set to true

$make_field_names_bold = true; //puts <strong></strong> tags around form fields names

$remove_underscores_or_dashes_in_field_names = true; //removes underscores, ie First_Name or First-Name will become First Name

$fields_to_exclude_from_email = 'submit;reset'; //which fields to not include in the email? add names of buttons, etc in field1;field2;field3 form

$path_to_template = 'email_template.html';


//code
if(!isset($_SERVER['HTTP_USER_AGENT']))
{
	header('location: '.$path_to_error_page.'?msg='.urlencode('Wrong User Agent'));
}
if(!$_SERVER['REQUEST_METHOD'] == "POST")
{
	header('location: '.$path_to_error_page.'?msg='.urlencode('Wrong Method'));
}
$fromArray = parse_url(strtolower($_SERVER['HTTP_REFERER']));
$pos = strpos($site_name, $fromArray['host']);
if($pos === false)
{
	header('location: '.$path_to_error_page.'?msg='.urlencode('Wrong Referer'));
}
$badStrings = array("Content-Type:","MIME-Version:","Content-Transfer-Encoding:","bcc:","cc:");
foreach($_POST as $k => $v)
{
	foreach($badStrings as $v2)
	{
		if(strpos($v, $v2) !== false)
		{
			header('location: '.$path_to_error_page.'?msg='.urlencode('Header Spoofing Detected'));
		}
	}
}    
unset($k, $v, $v2, $badStrings, $authHosts, $fromArray, $wwwUsed);

//captcha
$string = strtoupper(strip_tags($_SESSION['string']));
$userstring = strtoupper(strip_tags($_POST['userstring']));
session_destroy();   
if (($string == $userstring) && (strlen($string) > 4))
{
function mailer($to_email, $from_email, $reply_email, $subject, $msg)
{
$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset=utf-8\r\n";
$header .= "Content-Transfer-encoding: 8bit\r\n";
$header .= "From: ".$from_email."\r\n";
$header .= "Reply-To: ".$reply_email."\r\n";
$header .= "Message-ID: <".md5(uniqid(time()))."@{$_SERVER['SERVER_NAME']}>\r\n";
$header .= "Return-Path: ".$reply_email."\r\n";           
$header .= "X-Priority: 1\r\n";
$header .= "X-MSmail-Priority: High\r\n";
$header .= "X-Mailer: Microsoft Office Outlook, Build 11.0.5510\r\n";
$header .= "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441\r\n";
$header .= "X-Sender: ".$from_email."\r\n";
$header .= "X-AntiAbuse: This is a solicited email for priviate mailing list.\r\n";
$header .= "X-AntiAbuse: Servername - {$_SERVER['SERVER_NAME']}\r\n";
$header .= "X-AntiAbuse: User - ".$from_email."\r\n";
return mail($to_email, $subject, $msg, $header);
}

$template=file_get_contents($path_to_template);
if(is_bool($template) && $template == false)
{
	mailer('info@modalinc.com', 'error@'.$site_name, 'error@'.$site_name, 'Error - template cant be loaded', 'check sendmail script on '.$site_name.', error loading template');
	header('location: '.$path_to_error_page.'?msg='.urlencode('Template Error'));
}
else
{
	$template = str_replace('!_EMAIL-TITLE_!',$email_subject,$template);
	$default_from_email = $fake_email_to_use_for_From;
	$default_reply_email = $fake_email_to_use_for_Reply_To;
	if(!empty($field_name_to_use_for_Reply_To) && isset($_POST[$field_name_to_use_for_Reply_To]))
	{
		$default_reply_email=strip_tags($_POST[$field_name_to_use_for_Reply_To]);
	}
	if(!empty($field_name_to_use_for_From) && isset($_POST[$field_name_to_use_for_From]))
	{
		$default_from_email=strip_tags($_POST[$field_name_to_use_for_From]);
	}
	$exclude = explode(';',$fields_to_exclude_from_email);
	foreach($exclude as $val)
	{
		if(!empty($val) && isset($_POST[$val]))
		{
			unset($_POST[$val]);
		}
	}
	$pre_msg='';
	$post_msg='';
	$pre_key='';
	$post_key='';
	$post_val='';
	$msg='';
	if($table_form)
	{
		$pre_msg.='<table';
		if(!empty($table_id))
		{
			$pre_msg.=' id="'.trim($table_id).'"';
		}
		if(!empty($table_cellpadding))
		{
			$pre_msg.=' cellpadding="'.trim($table_cellpadding).'"';
		}
		if(!empty($table_cellspacing))
		{
			$pre_msg.=' cellspacing="'.trim($table_cellspacing).'"';
		}
		if(!empty($table_border))
		{
			$pre_msg.=' border="'.trim($table_border).'"';
		}
		$pre_msg.='>';
		$pre_key='<tr><td>';
		$post_key='</td><td>';
		$post_val='</td></tr>';
		$post_msg='</table>';
	}
	if($make_field_names_bold)
	{
		$pre_key=$pre_key.'<strong>';
		$post_key='</strong>'.$post_key;
	}
	foreach ($_POST as $key => $value)
	{
		if($remove_underscores_or_dashes_in_field_names)
		{
			$key=str_replace('_',' ',trim($key));
			$key=str_replace('-',' ',trim($key));
		}
		$msg.=$pre_key.$key.$post_key.trim(strip_tags($value)).$post_val;
	}
	$msg = $pre_msg.$msg.$post_msg;
	$template = str_replace('!_EMAIL-TEXT_!',$msg,$template);
	
	$emails = explode(';',$send_email_to);
	$overal_result=true;
	foreach($emails as $target)
	{
		if(!empty($target))
		{
			$overal_result = mailer($target, $default_from_email, $default_reply_email, $email_subject, $template);
		}
	}
	if($overal_result)
	{
		header('location: '.$path_to_thank_u_page);
	}
	else
	{
		header('location: '.$path_to_error_page.'?msg='.urlencode('Cant Send Email'));
	}
}
}
else
{
header('location: '.$path_to_error_page.'?msg='.urlencode('Wrong Captcha Code'));
}
?>