-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_email_simple.php
More file actions
133 lines (118 loc) · 4.48 KB
/
Copy pathsend_email_simple.php
File metadata and controls
133 lines (118 loc) · 4.48 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: application/json');
// Simple email function using PHP's mail() function
function sendSimpleEmail($to, $subject, $body) {
$headers = "From: shi3ldmaidens@gmail.com\r\n";
$headers .= "Reply-To: shi3ldmaidens@gmail.com\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$result = mail($to, $subject, $body, $headers);
file_put_contents("debug_log.txt", "Simple email to {$to}: " . ($result ? "SUCCESS" : "FAILED") . PHP_EOL, FILE_APPEND);
return $result;
}
// Handle POST Requests
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Contact Form
if (isset($_POST['name'], $_POST['email'], $_POST['message'])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
if ($name === '' || $email === '' || $message === '') {
echo json_encode([
'status' => 'error',
'message' => 'All contact fields are required.'
]);
exit;
}
// Save to database
$mysqli = new mysqli("localhost", "root", "", "shieldmaidens_db", 3308);
if ($mysqli->connect_error) {
echo json_encode([
'status' => 'error',
'message' => 'Database connection failed: ' . $mysqli->connect_error
]);
exit;
}
$stmt = $mysqli->prepare("INSERT INTO contact_messages (name, email, message) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $name, $email, $message);
$stmt->execute();
$stmt->close();
$mysqli->close();
// Send emails
$user_sent = sendSimpleEmail(
$email,
"We've received your message",
"Hi {$name},\n\nThank you for reaching out! We'll reply shortly.\n\n– ShieldMaidens Team"
);
$admin_sent = sendSimpleEmail(
"shi3ldmaidens@gmail.com",
"New Contact Message from {$name}",
"🛡️ New message:\n\nName: {$name}\nEmail: {$email}\nMessage:\n{$message}"
);
if ($user_sent && $admin_sent) {
echo json_encode([
'status' => 'success',
'message' => 'Thank you for contacting Shieldmaidens! We value inclusivity, empowerment, and safety. Our team will get back to you shortly. 💖'
]);
} else {
echo json_encode([
'status' => 'error',
'message' => 'Message saved but email sending failed. Please try again later.'
]);
}
exit;
}
// Mailing List Form
if (isset($_POST['mailingEmail'])) {
$email = trim($_POST['mailingEmail']);
if ($email === '') {
echo json_encode([
'status' => 'error',
'message' => 'Email is required for subscription.'
]);
exit;
}
// Save to database
$mysqli = new mysqli("localhost", "root", "", "shieldmaidens_db", 3308);
if ($mysqli->connect_error) {
echo json_encode([
'status' => 'error',
'message' => 'Database connection failed: ' . $mysqli->connect_error
]);
exit;
}
$stmt = $mysqli->prepare("INSERT IGNORE INTO mailing_list (email) VALUES (?)");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->close();
$mysqli->close();
$user_sent = sendSimpleEmail($email, "Welcome to Our Mailing List", "Thank you for subscribing! We'll keep you updated with our latest news.");
$admin_sent = sendSimpleEmail("shi3ldmaidens@gmail.com", "New Mailing List Subscription", "A new user has subscribed: {$email}");
if ($user_sent && $admin_sent) {
echo json_encode([
'status' => 'success',
'message' => "Thank you for subscribing to our news, let's get updated! 📰"
]);
} else {
echo json_encode([
'status' => 'error',
'message' => 'Subscription saved but email sending failed. Please try again later.'
]);
}
exit;
}
// Unknown POST
echo json_encode([
'status' => 'error',
'message' => 'Invalid request or missing parameters.'
]);
exit;
}
// Invalid request method
echo json_encode([
'status' => 'error',
'message' => 'Invalid request method.'
]);
exit;
?>