<?php
/**
 * PHP Contact form
 *
 * Add basic form validation: required fields & email address valid
 * Sends email to
 */
$email_to = 'robert@reject.pl'; // update to your email
$email_to_name = 'Robert'; // update to your name
$form_message = '<p>Please use the form below to contact us.</p>';
$form_errors = array();
$form_values = $_POST; // Optionally change to $_GET & <form action="contact.php" method="get"> on form
$required_fields = array( // if any of these fields are blank are error will show
  'contact-name' => 'Name',
  'contact-email' => 'Email',
  'contact-message' => 'Message',
);

print_r($form_values);
print_r($required_fields);

if (!empty($form_values)) {
  // Form was submitted, validate required fields
  if (!empty($required_fields)) {
    foreach ($required_fields as $required => $label) {
      if (empty($form_values[$required])) {
        $form_errors[$required] = "The $label field is required and cannot be left blank."; //Message to show use
      }
    }
  }
  
  if (!empty($form_values['contact-email'])) {
    // Email address submitted, validate it
    // Remove all illegal characters from email
    $email = filter_var($form_values['contact-email'], FILTER_SANITIZE_EMAIL);

    // Validate e-mail
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
      // Valid, send email
      require 'assets/plugins/PHPMailer/PHPMailerAutoload.php';
      $mail = new PHPMailer;

$mail->Host       = "smtp.vifnet.pl"; // SMTP server example
$mail->SMTPDebug  = 1;                     // enables SMTP debug information (for testing)
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->Port       = 587;                    // set the SMTP port for the GMAIL server
$mail->Username   = "slawek"; // SMTP account username example
$mail->Password   = "12slawek34";  

      $mail->setFrom($email, $form_values['contact-name']);
      $mail->addAddress($email_to, $email_to_name);
      $mail->Subject = 'Contact form enquiry from ' . $form_values['contact-name'];
      $email_body = array();
      $email_body[] = '<strong>Email:</strong> ' . $form_values['contact-email'] .'<br />';
      $email_body[] = '<strong>Name:</strong> ' . $form_values['contact-name'] .'<br />';
      $email_body[] = '<strong>Message:</strong> ' . $form_values['contact-message'] .'<br />';
      $email_body[] = 'Email sent at '. date('r', time());
      $mail->msgHTML(implode('<br />', $email_body));
      print_r($email_body);
      print_r($mail);
      if (!$mail->send()) {
        $form_errors[] = 'Error in sendng enquiry please email us at ' . $email_to . ' instead.';
      }
      else {
        $form_message = '<div class="alert alert-success" role="alert"><strong>Enquiry Successfully sent!</strong> Thank you we will get back to you as soon as possible.</div>';
        print_r($form_message);
      }      
    }
    else {
      // Invalid
      $form_errors['contact-email'] = 'The email address inputted is invalid.';
    }
  }
  
  // Make errors into message to user
  if (!empty($form_errors)) {
    $form_message = '<div class="alert alert-danger" role="alert"><strong>Error!</strong> The following errors have occurred submitting the form: <hr />' . implode('<br />', $form_errors) .'</div>';
  }  
}
print_r($form_errors);
?>
