async function formSend(e) {
e.preventDefault();
let error = formValidate(form);
let formData = new FormData(form);
if(error === 0){
let response = await fetch('sendemail.php',{
method: 'POST',
body: formData
});
if (response.ok) {
let result = await response.json();
alert(result.message);
document.querySelectorAll('#btn').forEach(item => {
item.addEventListener('onsubmit', () => {
document.getElementsByClassName('training__thanks').style.display = 'flex';
});
});
form.reset();
} else {
alert('Ошибка');
}
} else {
alert('Заполните обязательные поля');
}
}
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require "./PHPMailer/src/PHPMailer.php";
require "./PHPMailer/src/Exception.php";
require "./PHPMailer/src/SMTP.php";
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'gmail@gmail.com';
$mail->Password = 'secret';
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->CharSet = "UTF-8";
$mail->IsHTML(true);
$mail->setFrom('gmail@gmail.com, 'САИКТ');
$mail->addAddress("gmail@gmail.com", "САИКТ");
$theme = '[ЗАЯВКА НА ТРЕНИНГ]';
$mail->Subject = $theme;
$body = '<h1>Данные заявки</h1>';
$name = $_POST["name"];
$tel = $_POST["tel"];
$email = $_POST["email"];
if(trim(!empty($name))){
$body.='<p><strong>Имя:</strong> '.$name.'</p>';
}
if(trim(!empty($tel))){
$body.='<p><strong>Телефон:</strong> '.$tel.'</p>';
}
if(trim(!empty($email))){
$body.='<p><strong>От:</strong> '.$email.'</p>';
}
$mail->Body = $body;
if(!$mail->send()) {
$message = 'Ошибка!';
} else {
$message = 'Данные отправлены!';
}
$response = ['message' => $message];
header('Content-type: application/json');
echo json_encode($response);
$mail->smtpClose();
?>