File: /homepages/29/d194883696/htdocs/firstnetworking/handlers/contact.php
<?php
include_once '../Admin_config.php';
new ContactHandler();
class ContactHandler {
public $name = "";
public $email = "";
public $message = "";
public $errors = [];
private $_recipient = "";
function __construct(){
$this->_recipient = ADMIN_EMAIL;
if (isset($_POST["name"])) {
$this->name = filter_var(ucfirst($_POST['name']), FILTER_SANITIZE_STRING);
if (empty($this->name)) {
$this->errors[] = "name field Empty";
}
}else {
$this->errors[] = "name not set";
}
if (isset($_POST["email"])) {
$this->email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (empty($this->email)) {
$this->errors[] = "email field Empty";
}
}else {
$this->errors[] = "email not set";
}
if (isset($_POST["message"])) {
$this->message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
if (empty($this->message)) {
$this->errors[] = "Message field Empty";
}
}else {
$this->errors[] = "Message not set";
}
if (!empty($this->errors)) {
echo json_encode($this->errors);
}else {
echo json_encode($_POST);
$headers = "From: $name <$email> \r\n";
$headers .= "reply-to: $email \r\n";
$headers .= "content-type: text/html; charset=iso-8859-1 \r\n";
$headers .= "Return-Path:$this->_recipient \r\n";
$headers .= "x-mailer:". phpversion()." \r\n";
try {
$sendEmail = mail($recipient, $subject, $message, $headers);
if ($sendEmail === TRUE) {
echo json_encode(["success_message" => "Message Sent"]);
}
} catch (Exception $e) {
$this->errors[] = "There was a server error while trying to process your Message";
echo json_encode($this->errors);
}
}
}
}
?>