-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit_contact_form.php
More file actions
32 lines (28 loc) · 1.24 KB
/
submit_contact_form.php
File metadata and controls
32 lines (28 loc) · 1.24 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
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Sanitize and validate inputs
$name = htmlspecialchars(trim($_POST['name']));
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$subject = htmlspecialchars(trim($_POST['subject']));
$message = htmlspecialchars(trim($_POST['message']));
// Check for empty fields
if (empty($name) || empty($email) || empty($subject) || empty($message)) {
echo json_encode(['status' => 'error', 'message' => 'All fields are required']);
exit;
}
// You can add more validation here (like checking the email format)
// Set up email parameters (for example, using mail() function or a library like PHPMailer)
$to = 'your_email@example.com';
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
// Send email
if (mail($to, $subject, $message, $headers)) {
echo json_encode(['status' => 'success', 'message' => 'Your message has been sent.']);
} else {
echo json_encode(['status' => 'error', 'message' => 'Unable to send your message.']);
}
} else {
echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
}
?>