-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.php
More file actions
48 lines (42 loc) · 1.73 KB
/
contact.php
File metadata and controls
48 lines (42 loc) · 1.73 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
<?php
// Only process POST requests
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get form field values and sanitize them
$name = isset($_POST["name"]) ? htmlspecialchars(strip_tags(trim($_POST["name"]))) : "";
$email = isset($_POST["email"]) ? filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL) : "";
$subject = isset($_POST["subject"]) ? htmlspecialchars(strip_tags(trim($_POST["subject"]))) : "";
$message = isset($_POST["message"]) ? htmlspecialchars(strip_tags(trim($_POST["message"]))) : "";
// Validate inputs
if ($name === "" || $email === "" || $subject === "" || $message === "") {
echo "Please fill in all required fields.";
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid email format.";
exit;
}
// Create email message
$email_message = "Name: $name\n";
$email_message .= "Email: $email\n";
$email_message .= "Subject: $subject\n";
$email_message .= "Message:\n$message\n";
// Set email recipient and subject
$to = "josehcortes02@gmail.com";
$email_subject = "New Contact Form Submission: $subject";
// Set email headers
$headers = "From: $name <$email>\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
// Send email
if (mail($to, $email_subject, $email_message, $headers)) {
// Success message
echo "Thank you for your message! We'll get back to you shortly.";
} else {
// Error message
echo "Sorry, there was an error sending your message. Please try again later.";
}
} else {
// Not a POST request
echo "Access Denied. You must use POST method to send data.";
}