-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_mail.php
More file actions
79 lines (62 loc) · 2.3 KB
/
Copy pathsend_mail.php
File metadata and controls
79 lines (62 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require "PHPMailer/src/PHPMailer.php";
require "PHPMailer/src/Exception.php";
define('SMARTCAPTCHA_SERVER_KEY', 'your_server_key');
function check_captcha($token) {
$ch = curl_init();
$args = http_build_query([
"secret" => SMARTCAPTCHA_SERVER_KEY,
"token" => $token,
"ip" => $_SERVER['REMOTE_ADDR'],
]);
curl_setopt($ch, CURLOPT_URL, "https://smartcaptcha.yandexcloud.net/validate?$args");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$server_output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode !== 200) {
echo "Allow access due to an error: code=$httpcode; message=$server_output\n";
return true; // Change this to false if you want to deny access on error
}
$resp = json_decode($server_output);
return $resp->status === "ok";
}
$token = $_POST['smart-token'];
if (check_captcha($token)) {
// SmartCaptcha passed, proceed with sending email
$mail = new PHPMailer(true);
$mail->CharSet = "UTF-8";
$mail->IsHTML(true);
$mail->AddEmbeddedImage('img/logo.webp', 'logo_2u');
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$email_template = "template_mail.html";
$body = file_get_contents($email_template);
$body = str_replace('%name%', $name, $body);
$body = str_replace('%email%', $email, $body);
$body = str_replace('%phone%', $phone, $body);
$body = str_replace('%message%', $message, $body);
// адрес получателя
$mail->addAddress("jarekismail@gmail.com");
// адрес отправителя
$mail->setFrom("feedback@inputstudios.ru");
$mail->Subject = "[Заявка с формы]";
$mail->MsgHTML($body);
if (!$mail->send()) {
$message = "Ошибка отправки";
} else {
$message = "Данные отправлены!";
}
} else {
// SmartCaptcha verification failed
$message = "Ошибка валидации SmartCaptcha";
}
$response = ["message" => $message];
header('Content-type: application/json');
echo json_encode($response);
?>