-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit_form.php
More file actions
72 lines (59 loc) · 2.5 KB
/
submit_form.php
File metadata and controls
72 lines (59 loc) · 2.5 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
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$env = parse_ini_file(".env");
$emailUser = $env['EMAIL_USER'];
$emailPass = $env['EMAIL_PASS'];
$server = $env['SERVER'];
$secretKey = $env['SECRET_KEY'];
$recaptchaSecret = $secretKey;
$recaptchaResponse = $_POST['g-recaptcha-response'];
// Check if it's a POST request
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$recaptchaSecret = $secretKey;
$recaptchaResponse = $_POST['g-recaptcha-response'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$recaptchaSecret&response=$recaptchaResponse");
$responseKeys = json_decode($response, true);
// Verify the CAPTCHA
if (intval($responseKeys["success"]) !== 1) {
http_response_code(400); // Bad Request
echo json_encode(["success" => false, "message" => "Please complete the CAPTCHA."]);
exit; // Stop further processing
} else {
// CAPTCHA was completed successfully
// Continue processing the form
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$messageBody = $_POST['message'];
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = $server; // Your SMTP server
$mail->SMTPAuth = true;
$mail->Username = $emailUser; // SMTP username
$mail->Password = $emailPass; // SMTP password
$mail->SMTPSecure = 'ssl'; // 'ssl' or 'tls'
$mail->Port = 465; // TCP port to connect to; 465 for ssl
//Recipients
$mail->setFrom($email, $name);
$mail->addAddress($emailUser);
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $messageBody;
$mail->send();
http_response_code(200); // OK
echo json_encode(['success' => true, "message" => "Message sent successfully!"]); // For success
exit; // Stop further processing
} catch (Exception $e) {
http_response_code(500); // Internal Server Error
echo json_encode(["success" => false, "message" => "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"]);
exit; // Stop further processing
}
}
}