-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformhandler.php
62 lines (50 loc) · 2.22 KB
/
formhandler.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
<?php
// This is a very simple PHP script that outputs the name of each bit of information in the browser window, and then sends it all to an email address you add to the script.
// Many thanks to Adam Eivy for his invaluable help with modifying the PHP.
// Credit to Drew King for updating this script to work with AJAX.
if (empty($_POST)) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit();
}
// Function to sanitize user input
function clear_user_input($value) {
$value = str_replace("\n", '', trim($value));
$value = str_replace("\r", '', $value);
return $value;
}
if ($_POST['message'] == 'Please share any comments you have here') $_POST['message'] = '';
// Sanitize and set name and email
$name = isset($_POST['name']) ? clear_user_input($_POST['name']) : 'Anonymous';
$email = isset($_POST['email']) ? clear_user_input($_POST['email']) : '[email protected]';
// Validate email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo json_encode(["status" => "error", "message" => "Invalid email address."]);
exit();
}
// Create body of message
$body = "First message:\n";
foreach ($_POST as $key => $value) {
if (is_array($value)) {
$value = implode(', ', $value);
}
$key = clear_user_input($key);
$value = clear_user_input($value);
$$key = $value;
$body .= "$key: $value\n";
}
$from = 'From: [email protected]' . "\r\n" . 'Reply-To: ' . $email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
// Log the email content for debugging
error_log("Attempting to send email with the following details:");
error_log("From: " . $from);
error_log("Body: " . $body);
// Send email
$success = mail('[email protected]', 'Inquiry about Therapy With Diana', $body, $from);
//$success = TRUE; // for testing
// Pass result of mail() function to JavaScript to display message in the browser.
header('Content-Type: application/json');
if ($success) {
echo json_encode(["status" => "Success", "confirmation" => "Thank you, $name! Your message has been successfully sent from $email", "message" => "Your message: \"$message\""]);
} else {
echo json_encode(["status" => "error", "message" => "Sorry, there was a problem sending your message. Please try again."]);
}
?>