-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_email.php
More file actions
65 lines (58 loc) · 2.39 KB
/
send_email.php
File metadata and controls
65 lines (58 loc) · 2.39 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
<?php
include('hms/include/config.php'); // Database connection
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Sanitize & validate inputs
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$subject = trim($_POST['subject'] ?? '');
$message = trim($_POST['message'] ?? '');
if (!$name || !$email || !$phone || !$subject || !$message) {
http_response_code(400);
echo "All fields are required.";
exit;
}
// Insert into DB using prepared statement
$stmt = $con->prepare("INSERT INTO contact_us (name, email, phone, subject, message, created_at) VALUES (?, ?, ?, ?, ?, NOW())");
if ($stmt) {
$stmt->bind_param("sssss", $name, $email, $phone, $subject, $message);
$stmt->execute();
if ($stmt->affected_rows > 0) {
// Email setup
$to = "swaroopsasmile3505@gmail.com"; // Change to your receiving email
$email_subject = "New Contact Form Submission: " . htmlspecialchars($subject);
$email_message = "
<html>
<head><title>Contact Form Submission</title></head>
<body>
<h3>New Contact Form Submission</h3>
<p><strong>Name:</strong> " . htmlspecialchars($name) . "</p>
<p><strong>Email:</strong> " . htmlspecialchars($email) . "</p>
<p><strong>Phone:</strong> " . htmlspecialchars($phone) . "</p>
<p><strong>Subject:</strong> " . htmlspecialchars($subject) . "</p>
<p><strong>Message:</strong><br>" . nl2br(htmlspecialchars($message)) . "</p>
</body>
</html>
";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <$email>\r\n";
if (mail($to, $email_subject, $email_message, $headers)) {
echo "Success";
} else {
echo "Saved to DB, but email failed.";
}
} else {
http_response_code(500);
echo "Failed to insert data.";
}
$stmt->close();
} else {
http_response_code(500);
echo "Database Error: " . $con->error;
}
} else {
http_response_code(405); // Method Not Allowed
echo "Invalid request.";
}
?>