<?php
$check = array(
'sex' => array(
'filter' => FILTER_VALIDATE_INT,
'flags' => FILTER_REQUIRE_SCALAR,
'options' => array('min_range' => 1, 'max_range' => 2)
),
'email' => FILTER_VALIDATE_EMAIL,
'worker' => FILTER_VALIDATE_INT,
'message' => FILTER_SANITIZE_SPECIAL_CHARS
);
$error_message = array(
'sex' => 'Некорректное значение поля «Пол»!',
'email' => 'Некорректное значение поля «Адрес почты»!',
'worker' => 'Некорректное значение поля «Сотрудник»!',
'message' => 'Некорректное значение поля «Сообощение»!',
);
$errors = array();
$post = array_map('trim', $_POST);
if(array_diff_key($post, $check)) exit;
$data = filter_var_array($post, $check);
$errors += array_intersect_key($error_message, array_filter($data, function($v) { return !$v; }));
if($errors) {
header('Content-Type: application/json');
exit(json_encode(array('result'=> 'error', 'data'=> array_values($errors))));
} else {
$sex = array(1 => 'мужчина', 'женщина');
$worker = array(1 => 'Сергей', 'Иван', 'Николай', 'Антон', 'Виктор');
if(!$name = $worker[$data['worker']]) exit;
$text = 'Текстовый вариант';
$html = "<p><b>Пол:</b> " . $sex[$data['sex']] . "</p>
<p><b>E-mail:</b> " . $data['email'] . "</p>
<p><b>Сотрудник:</b> " . $name . "</p>
<p><b>Сообщение:</b> " . $data['message'] . "</p>";
$to = 'pochta@mail.ru';
$subject = '=?utf-8?b?' . base64_encode($data['subject']) . '?=';
$headers = 'From: =?utf-8?B?' . base64_encode($name) . '?= <' . $data['email'] . '>' . PHP_EOL;
$headers .= 'Reply-To: receiver@domain3.com' . PHP_EOL;
$headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
$headers .= 'Mime-Version: 1.0' . PHP_EOL;
$headers .= 'Content-Type: multipart/alternative; boundary="==boundary-2"' . PHP_EOL.PHP_EOL;
$body .= '--==boundary-2' . PHP_EOL.PHP_EOL;
$body .= 'Content-type: text/plain; charset="utf-8"' . PHP_EOL;
$body .= 'Content-Transfer-Encoding: 8bit' . PHP_EOL.PHP_EOL;
$body .= $text . PHP_EOL; //текстовая версия
$body .= '--==boundary-2' . PHP_EOL;
$body .= 'Content-Type: multipart/related; boundary="==boundary-1"; type="text/html"' . PHP_EOL.PHP_EOL;
$body .= '--==boundary-1' . PHP_EOL;
$body .= 'Content-type: text/html; charset="utf-8"' . PHP_EOL;
$body .= 'Content-Transfer-Encoding: base64' . PHP_EOL.PHP_EOL;
$body .= chunk_split(base64_encode($html)) . PHP_EOL.PHP_EOL; //html версия
$body .= '--==boundary-1--' . PHP_EOL;
$body .= '--==boundary-2--' . PHP_EOL;
mail($to, $subject, $body, $headers);
header('Content-Type: application/json');
exit(json_encode(array('result'=> 'done', 'data'=> array('Письмо отправлено'))));
}