<?php
include_once __DIR__ . "/../includes/init.php";

$contact_message = $engine->database->tbl_contact_message->new();
$contact_message->name = substr($_POST["name"], 0, 255);
$contact_message->email = substr($_POST["email"], 0, 255);
$contact_message->subject = substr($_POST["subject"], 0, 255);
$contact_message->message = $_POST["message"];
$engine->database->tbl_contact_message->add($contact_message);
$engine->database->tbl_contact_message->save();

$contact_message->mail_to = substr(MAIN_EMAIL, 0, 255);
$contact_message->mail_subject = substr("Online enquiry #" . $contact_message->id . " [" . $contact_message->subject . "]", 0, 255);
$contact_message->mail_message = "
<html>
<body>
<strong>Subject: </strong> " . $contact_message->subject . " <br>
<strong>Sent: </strong>" . date("Y/m/d H:i") . " <br>
<strong>From: </strong> " . $contact_message->name . " <br>
<strong>E-mail: </strong> " . $contact_message->email . " <br>
<strong>Message: </strong><br>" . $contact_message->message . " <br>
</body>
</html>
";
$engine->database->tbl_contact_message->save();

// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$headers .= "From: <" . $contact_message->email . ">" . "\r\n";

mail($contact_message->mail_to, $contact_message->mail_subject, $contact_message->mail_message, $headers);

echo "OK";
