forked from gyanshankar1708/GrowCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact-handler.php
More file actions
462 lines (400 loc) · 16.2 KB
/
Copy pathcontact-handler.php
File metadata and controls
462 lines (400 loc) · 16.2 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
* Enhanced Contact Form Handler
* Handles form submissions with file uploads, calendar booking, and messaging integration
*/
// Set headers for JSON response and CORS
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, X-Requested-With');
// Handle preflight OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
// Configuration
$config = [
'max_file_size' => 10 * 1024 * 1024, // 10MB
'allowed_file_types' => ['pdf', 'doc', 'docx', 'jpg', 'jpeg', 'png', 'gif'],
'upload_directory' => 'uploads/',
'admin_email' => 'info@growcraft.com',
'from_email' => 'noreply@growcraft.com',
'smtp_host' => 'localhost', // Configure your SMTP settings
'smtp_port' => 587,
'smtp_username' => '',
'smtp_password' => '',
];
try {
// Get form data
$formData = [
'name' => trim($_POST['name'] ?? ''),
'email' => trim($_POST['email'] ?? ''),
'phone' => trim($_POST['phone'] ?? ''),
'company' => trim($_POST['company'] ?? ''),
'service_type' => $_POST['service-type'] ?? '',
'budget' => $_POST['budget'] ?? '',
'timeline' => $_POST['timeline'] ?? '',
'preferred_date' => $_POST['preferred-date'] ?? '',
'preferred_time' => $_POST['preferred-time'] ?? '',
'message' => trim($_POST['message'] ?? ''),
'contact_method' => $_POST['contact-method'] ?? 'email',
];
// Validate required fields
$errors = validateFormData($formData);
if (!empty($errors)) {
http_response_code(400);
echo json_encode(['error' => implode('. ', $errors)]);
exit;
}
// Handle file uploads
$uploadedFiles = [];
if (isset($_FILES['files']) && !empty($_FILES['files']['name'][0])) {
$uploadedFiles = handleFileUploads($_FILES['files'], $config);
if (isset($uploadedFiles['error'])) {
http_response_code(400);
echo json_encode(['error' => $uploadedFiles['error']]);
exit;
}
}
// Save to database (optional)
$contactId = saveToDatabase($formData, $uploadedFiles);
// Send email notifications
$emailSent = sendEmailNotifications($formData, $uploadedFiles, $config);
// Send auto-reply to customer
sendAutoReply($formData, $config);
// Prepare calendar event (if consultation is requested)
$calendarEvent = null;
if (!empty($formData['preferred_date']) && !empty($formData['preferred_time'])) {
$calendarEvent = createCalendarEvent($formData);
}
// Success response
echo json_encode([
'success' => true,
'message' => 'Thank you! Your message has been sent successfully. We\'ll get back to you within 24 hours.',
'contact_id' => $contactId,
'files_uploaded' => count($uploadedFiles),
'calendar_event' => $calendarEvent
]);
} catch (Exception $e) {
error_log("Contact form error: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'An error occurred while processing your request. Please try again.']);
}
/**
* Validate form data
*/
function validateFormData($data) {
$errors = [];
// Required fields
if (empty($data['name'])) $errors[] = 'Name is required';
if (empty($data['email'])) $errors[] = 'Email is required';
if (empty($data['service_type'])) $errors[] = 'Service type is required';
if (empty($data['message'])) $errors[] = 'Message is required';
// Email validation
if (!empty($data['email']) && !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Invalid email address';
}
// Phone validation (if provided)
if (!empty($data['phone'])) {
$phone = preg_replace('/[^0-9+]/', '', $data['phone']);
if (strlen($phone) < 10) {
$errors[] = 'Invalid phone number';
}
}
// Message length
if (!empty($data['message']) && strlen($data['message']) < 10) {
$errors[] = 'Message must be at least 10 characters long';
}
return $errors;
}
/**
* Handle file uploads
*/
function handleFileUploads($files, $config) {
$uploadedFiles = [];
$uploadDir = $config['upload_directory'];
// Create upload directory if it doesn't exist
if (!is_dir($uploadDir)) {
if (!mkdir($uploadDir, 0755, true)) {
return ['error' => 'Failed to create upload directory'];
}
}
$fileCount = count($files['name']);
for ($i = 0; $i < $fileCount; $i++) {
if ($files['error'][$i] !== UPLOAD_ERR_OK) {
continue;
}
$fileName = $files['name'][$i];
$fileTmpName = $files['tmp_name'][$i];
$fileSize = $files['size'][$i];
$fileType = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
// Validate file size
if ($fileSize > $config['max_file_size']) {
return ['error' => "File '{$fileName}' is too large. Maximum size is 10MB."];
}
// Validate file type
if (!in_array($fileType, $config['allowed_file_types'])) {
return ['error' => "File '{$fileName}' has an unsupported format."];
}
// Generate unique filename
$uniqueFileName = time() . '_' . uniqid() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '', $fileName);
$uploadPath = $uploadDir . $uniqueFileName;
// Move uploaded file
if (move_uploaded_file($fileTmpName, $uploadPath)) {
$uploadedFiles[] = [
'original_name' => $fileName,
'stored_name' => $uniqueFileName,
'file_path' => $uploadPath,
'file_size' => $fileSize,
'file_type' => $fileType
];
} else {
return ['error' => "Failed to upload file '{$fileName}'"];
}
}
return $uploadedFiles;
}
/**
* Save contact data to database
*/
function saveToDatabase($formData, $uploadedFiles) {
try {
// Database configuration
$dsn = "mysql:host=localhost;dbname=growcraft;charset=utf8mb4";
$username = "your_db_username";
$password = "your_db_password";
$pdo = new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
// Insert contact record
$sql = "INSERT INTO contacts (
name, email, phone, company, service_type, budget, timeline,
preferred_date, preferred_time, message, contact_method,
files_json, created_at, status
) VALUES (
:name, :email, :phone, :company, :service_type, :budget, :timeline,
:preferred_date, :preferred_time, :message, :contact_method,
:files_json, NOW(), 'new'
)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
'name' => $formData['name'],
'email' => $formData['email'],
'phone' => $formData['phone'],
'company' => $formData['company'],
'service_type' => $formData['service_type'],
'budget' => $formData['budget'],
'timeline' => $formData['timeline'],
'preferred_date' => $formData['preferred_date'] ?: null,
'preferred_time' => $formData['preferred_time'] ?: null,
'message' => $formData['message'],
'contact_method' => $formData['contact_method'],
'files_json' => json_encode($uploadedFiles)
]);
return $pdo->lastInsertId();
} catch (PDOException $e) {
error_log("Database error: " . $e->getMessage());
return null;
}
}
/**
* Send email notifications
*/
function sendEmailNotifications($formData, $uploadedFiles, $config) {
$subject = "New Contact Form Submission - " . $formData['service_type'];
$emailBody = generateEmailBody($formData, $uploadedFiles);
// Send email using PHP mail() or SMTP
$headers = [
'From: ' . $config['from_email'],
'Reply-To: ' . $formData['email'],
'Content-Type: text/html; charset=UTF-8',
'X-Mailer: PHP/' . phpversion()
];
return mail($config['admin_email'], $subject, $emailBody, implode("\r\n", $headers));
}
/**
* Generate email body
*/
function generateEmailBody($formData, $uploadedFiles) {
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: #0078FF; color: white; padding: 20px; text-align: center; }
.content { padding: 20px; background: #f9f9f9; }
.field { margin-bottom: 15px; }
.label { font-weight: bold; color: #555; }
.value { margin-top: 5px; }
.files { background: white; padding: 15px; border-radius: 5px; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h2>New Contact Form Submission</h2>
</div>
<div class="content">
<div class="field">
<div class="label">Name:</div>
<div class="value"><?= htmlspecialchars($formData['name']) ?></div>
</div>
<div class="field">
<div class="label">Email:</div>
<div class="value"><?= htmlspecialchars($formData['email']) ?></div>
</div>
<?php if ($formData['phone']): ?>
<div class="field">
<div class="label">Phone:</div>
<div class="value"><?= htmlspecialchars($formData['phone']) ?></div>
</div>
<?php endif; ?>
<?php if ($formData['company']): ?>
<div class="field">
<div class="label">Company:</div>
<div class="value"><?= htmlspecialchars($formData['company']) ?></div>
</div>
<?php endif; ?>
<div class="field">
<div class="label">Service Interest:</div>
<div class="value"><?= ucfirst(str_replace('-', ' ', $formData['service_type'])) ?></div>
</div>
<?php if ($formData['budget']): ?>
<div class="field">
<div class="label">Budget:</div>
<div class="value"><?= htmlspecialchars($formData['budget']) ?></div>
</div>
<?php endif; ?>
<?php if ($formData['timeline']): ?>
<div class="field">
<div class="label">Timeline:</div>
<div class="value"><?= htmlspecialchars($formData['timeline']) ?></div>
</div>
<?php endif; ?>
<?php if ($formData['preferred_date'] && $formData['preferred_time']): ?>
<div class="field">
<div class="label">Preferred Consultation:</div>
<div class="value"><?= date('F j, Y', strtotime($formData['preferred_date'])) ?> at <?= date('g:i A', strtotime($formData['preferred_time'])) ?></div>
</div>
<?php endif; ?>
<div class="field">
<div class="label">Preferred Contact Method:</div>
<div class="value"><?= ucfirst($formData['contact_method']) ?></div>
</div>
<div class="field">
<div class="label">Message:</div>
<div class="value"><?= nl2br(htmlspecialchars($formData['message'])) ?></div>
</div>
<?php if (!empty($uploadedFiles)): ?>
<div class="field">
<div class="label">Uploaded Files:</div>
<div class="files">
<?php foreach ($uploadedFiles as $file): ?>
<div><?= htmlspecialchars($file['original_name']) ?> (<?= formatBytes($file['file_size']) ?>)</div>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<div class="field">
<div class="label">Submitted:</div>
<div class="value"><?= date('F j, Y g:i A') ?></div>
</div>
</div>
</div>
</body>
</html>
<?php
return ob_get_clean();
}
/**
* Send auto-reply to customer
*/
function sendAutoReply($formData, $config) {
$subject = "Thank you for contacting GrowCraft - We'll be in touch soon!";
$replyBody = "
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; }
.header { background: #0078FF; color: white; padding: 20px; text-align: center; }
.content { padding: 20px; }
</style>
</head>
<body>
<div class='container'>
<div class='header'>
<h2>Thank You for Contacting GrowCraft!</h2>
</div>
<div class='content'>
<p>Dear " . htmlspecialchars($formData['name']) . ",</p>
<p>Thank you for reaching out to us! We have received your inquiry about <strong>" . ucfirst(str_replace('-', ' ', $formData['service_type'])) . "</strong> and appreciate your interest in our services.</p>
<p>Here's what happens next:</p>
<ul>
<li>Our team will review your requirements within 24 hours</li>
<li>We'll get back to you via your preferred contact method: <strong>" . ucfirst($formData['contact_method']) . "</strong></li>
<li>If you requested a consultation, we'll confirm your preferred time slot</li>
</ul>
<p>In the meantime, feel free to:</p>
<ul>
<li>Visit our website to learn more about our services</li>
<li>Follow us on social media for updates and tips</li>
<li>Contact us directly for urgent inquiries: +91 999 999 999</li>
</ul>
<p>We're excited to help grow your business!</p>
<p>Best regards,<br>
The GrowCraft Team<br>
info@growcraft.com<br>
+91 999 999 999</p>
</div>
</div>
</body>
</html>
";
$headers = [
'From: ' . $config['from_email'],
'Content-Type: text/html; charset=UTF-8',
'X-Mailer: PHP/' . phpversion()
];
return mail($formData['email'], $subject, $replyBody, implode("\r\n", $headers));
}
/**
* Create calendar event data
*/
function createCalendarEvent($formData) {
if (empty($formData['preferred_date']) || empty($formData['preferred_time'])) {
return null;
}
$startDateTime = $formData['preferred_date'] . 'T' . $formData['preferred_time'] . ':00';
$endDateTime = date('Y-m-d\TH:i:s', strtotime($startDateTime . ' +1 hour'));
return [
'title' => 'Consultation - ' . $formData['name'],
'start' => $startDateTime,
'end' => $endDateTime,
'description' => "Consultation for " . $formData['service_type'] . "\n\nClient: " . $formData['name'] . "\nEmail: " . $formData['email'] . "\nPhone: " . $formData['phone'],
'location' => 'Online/Office'
];
}
/**
* Format bytes to human readable format
*/
function formatBytes($size, $precision = 2) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$base = log($size, 1024);
return round(pow(1024, $base - floor($base)), $precision) . ' ' . $units[floor($base)];
}
?>