<?php
/*
 * @author Balaji
 */
 
error_reporting(1);

require_once('config.php');

    $user_email = trim($_POST['email']);
    $name = trim($_POST['name']);
    $sub = trim($_POST['subject']);   
    
    $con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass,$mysql_database);

  if (mysqli_connect_errno())
  {
  echo "<br>Failed to connect to MySQL: " . mysqli_connect_error();
  }
    $query =  "SELECT * FROM site_info";
    $result = mysqli_query($con,$query);
        
    while($row = mysqli_fetch_array($result)) {
    $admin_email =   Trim($row['email']);

    }
    
   $emailTo = $admin_email;

	// Do not edit anything from here unless you know what you are doing
	$contactErrors = array();

	if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{	
		if(trim($_POST['subject']) === '')
		{
			$contactErrors['subject'] = 'Subject is required.';
            goto me;
		}
		else
		{
 		$subject = trim($_POST['subject']);
		}
		
        $name = "Admin";
        
		if(trim($_POST['email']) === '')
		{
			$contactErrors['email'] = 'Your email address is required.';
		}
		else if (!preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,10})$^", trim($_POST['email'])))
		{
			$contactErrors['email'] = 'Your email address seems to be invalid.';
                        goto me;
		}
		else
		{
			$email = trim($_POST['email']);
		}
		
		if(trim($_POST['message']) === '')
		{
			$contactErrors['message'] = 'Your message is required.';
   goto me;
		}
		else
		{
			if (function_exists('stripslashes'))
			{
				$message = stripslashes(trim($_POST['message']));
			}
			else
			{
				$message = trim($_POST['message']);
			}
		}
		
		if (empty($contactErrors) && trim($emailTo) !== '')
		{			
			$body = "Name: $name \n\nEmail: $email \n\nMessage: $message";
			$headers = 'From: ' . $name . ' <' . $emailTo . '>' . "\r\n" . 'Reply-To: ' . $email;
			
			mail($emailTo, $subject, $body, $headers);
			$emailSent = true;
		}
        
me:
  if (isset($emailSent))
    {
       echo '1';
    }
    else
    {
        echo '0';
    } 
    }
?>