<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$message = $_REQUEST['message'];
$agree = isset($_REQUEST['agree']);
$errors = false;
$requiredFields = ['name', 'email', 'phone', 'agree', 'message'];
$errorFields = [];
// проверяем обязательные поля
foreach($requiredFields as $field){
if (!$_REQUEST[$field]){
array_push($errorFields, $field);
$errors = true;
}
}
if (!$errors) :
$subjectuser = "Сообщение";
$headersuser = "From: Сообщение\r\n";
$headersuser .= "Reply-To: ". strip_tags($name) . "\r\n";
$headersuser .= "MIME-Version: 1.0\r\n";
$headersuser .= "Content-Type: text/html;charset=utf-8 \r\n";
$my_message = "<h1>ТЕМА</h1><br/>";
$my_message .= "Имя: {$name}<br/>";
if ($phone){ $my_message .= "Телефон: {$phone}<br />"; }
if ($email){ $my_message .= "E-mail: {$email}<br />"; }
if ($message){ $my_message .= "Комментарий: <br>" . $message . "<hr/>"; }
$my_message .= "Время заказа: " . date('H:i:s d.m.Y');
require_once($_SERVER['DOCUMENT_ROOT'] . '/include/libs/PHPMailer/class.phpmailer.php'); //Подключаем PHPMailer
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->CharSet = "UTF-8";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.yandex.ru"; // SMTP server
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->SMTPSecure = 'ssl'; // Secure SMTP
$mail->Username = 'from@matveevs,ru'; // SMTP account username
$mail->Password = "fromPassword"; // SMTP account password
$mail->SetFrom('from@matveevs,ru', 'site.ru');
$mail->AddReplyTo('from@matveevs,ru','site.ru');
$mail->Subject = "Сообщение с сайта site.ru";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($my_message);
$address = 'to@matveevs.ru';
$mail->AddAddress($address, "User");
$mail->CharSet="UTF-8";
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) :
$res = Array(
"result" => false,
"msg" => "Ошибка отправления"
);
else:
$res = Array(
"result" => true,
"msg" => "Спасибо! Ваша заявка отправлена"
);
require $_SERVER["DOCUMENT_ROOT"] . '/addToBitrix.php';
endif;
else:
$res = Array(
"result" => false,
"msg" => "Ошибка: заполните все поля формы",
"errors" => $errorFields
);
endif;
echo json_encode($res, JSON_UNESCAPED_UNICODE);