-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact.php
More file actions
198 lines (169 loc) · 6.18 KB
/
Copy pathcontact.php
File metadata and controls
198 lines (169 loc) · 6.18 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
// contact-handler.php
header('Content-Type: application/json');
// ============================================
// CONFIGURATION - EDIT THESE VALUES
// ============================================
$recipientEmail = 'css-030-22@must.ac.mw'; // YOUR RECIPIENT
$siteName = 'WeClean Services';
// SMTP Configuration (use your actual SMTP credentials)
$smtpConfig = [
'host' => 'smtp.gmail.com', // or your university's SMTP server
'port' => 587,
'username' => 'your-email@gmail.com', // SMTP username
'password' => 'your-app-password', // SMTP password
'encryption' => 'tls' // or 'ssl' for port 465
];
// ============================================
// DO NOT EDIT BELOW THIS LINE
// ============================================
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'message' => 'Method not allowed.']);
exit;
}
// Helper: sanitize text input
function cleanInput(string $value): string {
return htmlspecialchars(strip_tags(trim($value)), ENT_QUOTES, 'UTF-8');
}
// Collect + sanitize fields
$fullName = cleanInput($_POST['fullName'] ?? '');
$email = cleanInput($_POST['email'] ?? '');
$phone = cleanInput($_POST['phone'] ?? '');
$service = cleanInput($_POST['service'] ?? '');
$message = cleanInput($_POST['message'] ?? '');
// Server-side validation
$errors = [];
if (mb_strlen($fullName) < 2) {
$errors['fullName'] = 'Please enter your full name.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Please enter a valid email address.';
}
if (!preg_match('/^[0-9+()\s-]{7,}$/', $phone)) {
$errors['phone'] = 'Please enter a valid phone number.';
}
$allowedServices = ['Residential Cleaning', 'Office Cleaning', 'Industrial Cleaning', 'Specialized Cleaning'];
if (!in_array($service, $allowedServices, true)) {
$errors['service'] = 'Please select a valid service.';
}
if (!empty($errors)) {
http_response_code(422);
echo json_encode(['success' => false, 'message' => 'Validation failed.', 'errors' => $errors]);
exit;
}
// Prepare email content
$subject = "New cleaning request from {$fullName}";
$body = "You have a new enquiry from the {$siteName} website.\n\n"
. "Name: {$fullName}\n"
. "Email: {$email}\n"
. "Phone: {$phone}\n"
. "Service requested: {$service}\n\n"
. "Message:\n{$message}\n";
// Log enquiry to file (backup)
$logLine = sprintf(
"[%s] %s <%s> | %s | %s\n",
date('Y-m-d H:i:s'),
$fullName,
$email,
$phone,
$service
);
@file_put_contents(__DIR__ . '/submissions.log', $logLine, FILE_APPEND | LOCK_EX);
// ============================================
// METHOD 1: PHPMailer with SMTP (RECOMMENDED)
// ============================================
function sendWithPHPMailer($recipient, $subject, $body, $fromEmail, $fromName, $smtpConfig) {
try {
// Check if PHPMailer is installed
if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
// Try to autoload
require_once __DIR__ . '/vendor/autoload.php';
}
if (!class_exists('PHPMailer\PHPMailer\PHPMailer')) {
throw new Exception('PHPMailer not installed. Please run: composer require phpmailer/phpmailer');
}
$mail = new PHPMailer\PHPMailer\PHPMailer(true);
// Server settings
$mail->isSMTP();
$mail->Host = $smtpConfig['host'];
$mail->SMTPAuth = true;
$mail->Username = $smtpConfig['username'];
$mail->Password = $smtpConfig['password'];
$mail->SMTPSecure = $smtpConfig['encryption'] === 'ssl'
? PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS
: PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $smtpConfig['port'];
$mail->SMTPDebug = 0; // Set to 2 for debugging
// Recipients
$mail->setFrom($smtpConfig['username'], $fromName);
$mail->addAddress($recipient); // css-030-22@must.ac.mw
$mail->addReplyTo($fromEmail, $fromName);
// Content
$mail->isHTML(false);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->send();
return ['success' => true, 'message' => 'Email sent successfully'];
} catch (Exception $e) {
return ['success' => false, 'error' => $e->getMessage()];
}
}
// ============================================
// METHOD 2: Native PHP mail() (Fallback)
// ============================================
function sendWithNativeMail($recipient, $subject, $body, $fromEmail, $fromName) {
$headers = [
'From: ' . $fromName . ' <no-reply@wecleanservices.com>',
'Reply-To: ' . $fromEmail,
'Content-Type: text/plain; charset=UTF-8',
'X-Mailer: PHP/' . phpversion()
];
return @mail($recipient, $subject, $body, implode("\r\n", $headers));
}
// ============================================
// SEND THE EMAIL
// ============================================
$emailSent = false;
$errorMessage = '';
// Try PHPMailer first
if (class_exists('PHPMailer\PHPMailer\PHPMailer')) {
$result = sendWithPHPMailer(
$recipientEmail,
$subject,
$body,
$email,
$fullName,
$smtpConfig
);
if ($result['success']) {
$emailSent = true;
} else {
$errorMessage = $result['error'];
}
}
// Fallback to native mail() if PHPMailer failed or not installed
if (!$emailSent) {
$emailSent = sendWithNativeMail($recipientEmail, $subject, $body, $email, $fullName);
if (!$emailSent) {
$errorMessage = 'Both SMTP and native mail() failed';
}
}
// ============================================
// RESPONSE
// ============================================
if ($emailSent) {
echo json_encode([
'success' => true,
'message' => 'Thank you! We will get back to you shortly.',
'delivered_to' => $recipientEmail
]);
} else {
// Log the error but still return success to user
error_log("Email delivery failed for {$recipientEmail}: {$errorMessage}");
echo json_encode([
'success' => true,
'message' => 'Request received. We will contact you shortly.'
]);
}
?>