-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-form.php
83 lines (69 loc) · 2.35 KB
/
process-form.php
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
80
81
82
83
<?php
/*
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$message = test_input($_POST["message"]);
// Validate the name
if (empty($name)) {
$nameErr = "Name is required";
}
// Validate the email
if (empty($email)) {
$emailErr = "Email is required";
} else {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
// Validate the message
if (empty($message)) {
$messageErr = "Message is required";
}
// If there are no errors, send the email
if (empty($nameErr) && empty($emailErr) && empty($messageErr)) {
$to = "amaraneni.rithwik@gmail.com";
$subject = "New message from your portfolio website";
$message_body = "Name: $name\n\nEmail: $email\n\nMessage:\n$message";
$headers = "From: $email\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= "Content-type: text/plain\r\n";
if (mail($to, $subject, $message_body, $headers)) {
header("Location: index.html");
exit;
} else {
$messageErr = "There was an error sending your message. Please try again later.";
}
}
}
// Function to test and sanitize input data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}*/
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
if (!empty($email) && !empty($message)) {
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
$filename = 'messages.txt';
date_default_timezone_set('Asia/Kolkata'); // Replace 'Your/Timezone' with your actual timezone
$content = "Date: " . date('d F Y h:i:s A') . "\n";
$content .= "Name: $name\n";
$content .= "Email: $email\n";
$content .= "Message: $message\n\n";
// Save the message to a file
if (file_put_contents($filename, $content, FILE_APPEND | LOCK_EX) !== false) {
echo "Your message has been sent successfully.";
} else {
echo "Failed to send the message.";
}
} else {
echo "Enter a valid email address!";
}
} else {
echo "Email and message fields are required!";
}
?>