| 2 |
rodolico |
1 |
<?php
|
| 4 |
randell |
2 |
|
|
|
3 |
// File version 0.5
|
| 2 |
rodolico |
4 |
// Load PHPmailer
|
|
|
5 |
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
6 |
use PHPMailer\PHPMailer\Exception;
|
|
|
7 |
|
|
|
8 |
require 'PHPMailer/Exception.php';
|
|
|
9 |
require 'PHPMailer/PHPMailer.php';
|
|
|
10 |
require 'PHPMailer/SMTP.php';
|
|
|
11 |
|
|
|
12 |
$config_file = 'config.php';
|
|
|
13 |
|
|
|
14 |
if (!file_exists($config_file)) {
|
|
|
15 |
// Default configuration is generated here
|
|
|
16 |
$default_config = <<<EOD
|
|
|
17 |
<?php
|
|
|
18 |
return [
|
|
|
19 |
'to' => 'your-email@example.com',
|
|
|
20 |
'bcc' => '',
|
|
|
21 |
'from' => 'no-reply@example.com',
|
|
|
22 |
'from_name' => 'mail sender',
|
|
|
23 |
'smtp_host' => 'smtp.example.com',
|
|
|
24 |
'smtp_port' => 587,
|
|
|
25 |
'smtp_username' => 'your-username',
|
|
|
26 |
'smtp_password' => 'your-password',
|
|
|
27 |
'debug' => false,
|
|
|
28 |
'page_title' => 'Support Request Form',
|
|
|
29 |
'fields' => [
|
|
|
30 |
[
|
|
|
31 |
'name' => 'requestor_name',
|
|
|
32 |
'type' => 'text',
|
|
|
33 |
'label' => 'Requestor Name',
|
|
|
34 |
'required' => true,
|
|
|
35 |
],
|
|
|
36 |
[
|
|
|
37 |
'name' => 'requestor_phone',
|
|
|
38 |
'type' => 'text',
|
|
|
39 |
'label' => 'Requestor Phone',
|
|
|
40 |
'required' => false,
|
|
|
41 |
],
|
|
|
42 |
[
|
|
|
43 |
'name' => 'requestor_email',
|
|
|
44 |
'type' => 'email',
|
|
|
45 |
'label' => 'Requestor Email',
|
|
|
46 |
'required' => true,
|
|
|
47 |
],
|
|
|
48 |
[
|
|
|
49 |
'name' => 'checkbox_example',
|
|
|
50 |
'type' => 'checkbox',
|
|
|
51 |
'label' => 'Checkbox Example',
|
|
|
52 |
'required' => false,
|
|
|
53 |
],
|
|
|
54 |
[
|
|
|
55 |
'name' => 'select_example',
|
|
|
56 |
'type' => 'select',
|
|
|
57 |
'label' => 'Select Example',
|
|
|
58 |
'required' => true,
|
|
|
59 |
'options' => [
|
|
|
60 |
'1' => 'option 1',
|
|
|
61 |
'2' => 'option 2',
|
|
|
62 |
],
|
|
|
63 |
],
|
|
|
64 |
[
|
|
|
65 |
'name' => 'attachments',
|
|
|
66 |
'type' => 'file',
|
|
|
67 |
'label' => 'Attachments',
|
|
|
68 |
'required' => false,
|
|
|
69 |
'multiple' => true, // Support multiple files
|
|
|
70 |
],
|
|
|
71 |
],
|
|
|
72 |
'success_message' => 'Your request has been sent successfully!',
|
|
|
73 |
]
|
|
|
74 |
EOD;
|
|
|
75 |
|
|
|
76 |
if (file_put_contents($config_file, $default_config) === false) {
|
|
|
77 |
die('Error: Could not create the default config file. Please check your file permissions or create a config.php file manually. Here is an example: <pre>' . htmlspecialchars($default_config) . '</pre>');
|
|
|
78 |
} else {
|
|
|
79 |
die('A default config file has been created at ' . $config_file . '. Please update it with your actual settings.');
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$config = require($config_file);
|
|
|
84 |
|
|
|
85 |
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
|
86 |
$error = '';
|
|
|
87 |
// Validate required fields
|
|
|
88 |
foreach ($config['fields'] as $field) {
|
|
|
89 |
if ($field['required'] && empty($_POST[$field['name']])) {
|
|
|
90 |
$error .= "The field '{$field['label']}' is required. ";
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
if ($error) {
|
|
|
95 |
// Error in form submission
|
|
|
96 |
$error = "Please fill in all required fields.";
|
|
|
97 |
} else {
|
|
|
98 |
// Collect form data
|
|
|
99 |
$message = '';
|
|
|
100 |
foreach ($config['fields'] as $field) {
|
|
|
101 |
if ($field['type'] === 'file' && isset($_FILES[$field['name']])) {
|
|
|
102 |
// Handle file attachments separately
|
| 4 |
randell |
103 |
echo "_FILES <pre>" . print_r($_FILES, true) . "</pre>";
|
|
|
104 |
$attachments = $_FILES[$field['name']];
|
| 2 |
rodolico |
105 |
} else {
|
|
|
106 |
$message .= "{$field['label']}: " . htmlspecialchars($_POST[$field['name']]) . "\n";
|
|
|
107 |
}
|
|
|
108 |
}
|
| 4 |
randell |
109 |
|
| 2 |
rodolico |
110 |
// Prepare to send email using PHPMailer
|
|
|
111 |
$mail = new PHPMailer();
|
|
|
112 |
try {
|
|
|
113 |
if ($config['debug']) {
|
|
|
114 |
$mail->SMTPDebug = 2; // Enable verbose debug output
|
|
|
115 |
}
|
|
|
116 |
$mail->isSMTP();
|
|
|
117 |
$mail->SMTPOptions = array(
|
| 4 |
randell |
118 |
'ssl' => array(
|
|
|
119 |
'verify_peer' => false,
|
|
|
120 |
'verify_peer_name' => false,
|
|
|
121 |
'allow_self_signed' => true
|
|
|
122 |
)
|
|
|
123 |
);
|
| 2 |
rodolico |
124 |
$mail->Host = $config['smtp_host'];
|
|
|
125 |
$mail->SMTPAuth = true;
|
|
|
126 |
$mail->Username = $config['smtp_username'];
|
|
|
127 |
$mail->Password = $config['smtp_password'];
|
|
|
128 |
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
|
129 |
$mail->Port = $config['smtp_port'];
|
|
|
130 |
$mail->setFrom($config['from'], $config['from_name']);
|
|
|
131 |
$mail->addAddress($config['to']);
|
|
|
132 |
if (!empty($config['bcc'])) {
|
|
|
133 |
$bccs = explode(',', $config['bcc']);
|
|
|
134 |
foreach ($bccs as $bcc) {
|
|
|
135 |
$mail->addBCC($bcc);
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
if (!empty($_POST['requestor_email']) && !empty($_POST['requestor_name'])) {
|
|
|
139 |
$mail->AddReplyTo($_POST['requestor_email'], $_POST['requestor_name']);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
$mail->Subject = htmlspecialchars($_POST['short_description']);
|
|
|
143 |
$mail->Body = $message;
|
|
|
144 |
|
|
|
145 |
// Attach files if any
|
|
|
146 |
if (!empty($attachments)) {
|
| 4 |
randell |
147 |
if ($config['debug']) {
|
|
|
148 |
echo "Attachments: <pre>" . print_r($attachments, true) . "</pre>";
|
| 2 |
rodolico |
149 |
}
|
| 4 |
randell |
150 |
if (is_array($attachments['name'])) {
|
|
|
151 |
for ($i = 0; $i < count($attachments['name']); $i++) {
|
|
|
152 |
if ($config['debug']) {
|
|
|
153 |
echo "Processing Attachment.<br />";
|
|
|
154 |
echo $attachments['name'][$i];
|
|
|
155 |
}
|
|
|
156 |
$mail->addAttachment($attachments['tmp_name'][$i], $attachments['name'][$i]);
|
|
|
157 |
}
|
|
|
158 |
} else {
|
|
|
159 |
$mail->addAttachment($attachments['tmp_name'], $attachments['name']);
|
|
|
160 |
}
|
| 2 |
rodolico |
161 |
}
|
|
|
162 |
|
|
|
163 |
$mail->send();
|
|
|
164 |
$result = $config['success_message'];
|
|
|
165 |
} catch (Exception $e) {
|
|
|
166 |
$result = "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
|
|
|
167 |
$result_error = true;
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
?>
|
|
|
172 |
|
|
|
173 |
<!DOCTYPE html>
|
|
|
174 |
<html lang="en">
|
|
|
175 |
<head>
|
|
|
176 |
<meta charset="UTF-8">
|
|
|
177 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
178 |
<title><?php echo htmlspecialchars($config['page_title']); ?></title>
|
|
|
179 |
<style>
|
|
|
180 |
body {
|
|
|
181 |
font-family: Arial, sans-serif;
|
|
|
182 |
background-color: #f4f4f4;
|
|
|
183 |
margin: 0;
|
|
|
184 |
padding: 20px;
|
|
|
185 |
}
|
|
|
186 |
.form-container {
|
|
|
187 |
max-width: 600px;
|
|
|
188 |
margin: 0 auto;
|
|
|
189 |
background-color: #fff;
|
|
|
190 |
padding: 20px;
|
|
|
191 |
border-radius: 8px;
|
|
|
192 |
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
|
193 |
}
|
|
|
194 |
h1 {
|
|
|
195 |
font-size: 24px;
|
|
|
196 |
margin-bottom: 20px;
|
|
|
197 |
color: #333;
|
|
|
198 |
}
|
|
|
199 |
label {
|
|
|
200 |
display: block;
|
|
|
201 |
margin-bottom: 5px;
|
|
|
202 |
font-weight: bold;
|
|
|
203 |
}
|
|
|
204 |
.required-field::after {
|
|
|
205 |
content: " *";
|
|
|
206 |
color: red;
|
|
|
207 |
}
|
|
|
208 |
input[type="text"], input[type="email"], select, textarea {
|
|
|
209 |
width: 100%;
|
|
|
210 |
padding: 10px;
|
|
|
211 |
margin-bottom: 15px;
|
|
|
212 |
border: 1px solid #ccc;
|
|
|
213 |
border-radius: 4px;
|
|
|
214 |
font-size: 16px;
|
|
|
215 |
box-sizing: border-box;
|
|
|
216 |
}
|
|
|
217 |
textarea {
|
|
|
218 |
resize: vertical;
|
|
|
219 |
min-height: 100px;
|
|
|
220 |
}
|
|
|
221 |
input[type="file"] {
|
|
|
222 |
margin-bottom: 15px;
|
|
|
223 |
}
|
|
|
224 |
input[type="submit"] {
|
|
|
225 |
background-color: #28a745;
|
|
|
226 |
color: white;
|
|
|
227 |
padding: 10px 20px;
|
|
|
228 |
border: none;
|
|
|
229 |
border-radius: 4px;
|
|
|
230 |
font-size: 16px;
|
|
|
231 |
cursor: pointer;
|
|
|
232 |
}
|
|
|
233 |
input[type="submit"]:hover {
|
|
|
234 |
background-color: #218838;
|
|
|
235 |
}
|
|
|
236 |
.form-group {
|
|
|
237 |
margin-bottom: 20px;
|
|
|
238 |
}
|
|
|
239 |
.hidden-fields {
|
|
|
240 |
display: none;
|
|
|
241 |
}
|
|
|
242 |
.message-error {
|
|
|
243 |
color: white;
|
|
|
244 |
background-color: red;
|
|
|
245 |
text-align: center;
|
|
|
246 |
width: 100%;
|
|
|
247 |
padding: 4px;
|
|
|
248 |
}
|
|
|
249 |
.message-success {
|
|
|
250 |
color: white;
|
|
|
251 |
background-color: green;
|
|
|
252 |
text-align: center;
|
|
|
253 |
width: 100%;padding: 4px;
|
|
|
254 |
}
|
|
|
255 |
</style>
|
|
|
256 |
</head>
|
|
|
257 |
<body>
|
|
|
258 |
<div class="form-container">
|
| 4 |
randell |
259 |
<?php if (isset($result)): ?>
|
| 2 |
rodolico |
260 |
<div class="<?= $result_error ? 'message-error' : 'message-success' ?>">
|
| 4 |
randell |
261 |
<?= htmlspecialchars($result) ?>
|
| 2 |
rodolico |
262 |
</div>
|
|
|
263 |
<?php endif; ?>
|
|
|
264 |
|
| 4 |
randell |
265 |
<?php if (isset($error) && !empty($error)): ?>
|
| 2 |
rodolico |
266 |
<div class="message-error">
|
| 4 |
randell |
267 |
<?= htmlspecialchars($error) ?>
|
| 2 |
rodolico |
268 |
</div>
|
| 4 |
randell |
269 |
<?php endif; ?>
|
|
|
270 |
|
| 2 |
rodolico |
271 |
<h1><?php echo htmlspecialchars($config['page_title']); ?></h1>
|
|
|
272 |
<form action="" method="POST" enctype="multipart/form-data">
|
|
|
273 |
<div class="hidden-fields">
|
|
|
274 |
<input type="hidden" name="to" value="<?php echo htmlspecialchars($config['to']); ?>">
|
|
|
275 |
<input type="hidden" name="bcc" value="<?php echo htmlspecialchars($config['bcc']); ?>">
|
|
|
276 |
</div>
|
|
|
277 |
|
| 4 |
randell |
278 |
<?php foreach ($config['fields'] as $field): ?>
|
| 2 |
rodolico |
279 |
<div class="form-group">
|
|
|
280 |
<label for="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required-field' : '' ?>">
|
| 4 |
randell |
281 |
<?= htmlspecialchars($field['label']) ?>
|
| 2 |
rodolico |
282 |
</label>
|
|
|
283 |
|
| 4 |
randell |
284 |
<?php if ($field['type'] == 'select'): ?>
|
| 2 |
rodolico |
285 |
<select name="<?= htmlspecialchars($field['name']) ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>">
|
|
|
286 |
<?php foreach ($field['options'] as $value => $label): ?>
|
|
|
287 |
<option value="<?= htmlspecialchars($value) ?>"><?= htmlspecialchars($label) ?></option>
|
| 4 |
randell |
288 |
<?php endforeach; ?>
|
| 2 |
rodolico |
289 |
</select>
|
|
|
290 |
<?php elseif ($field['type'] == 'textarea'): ?>
|
|
|
291 |
<textarea name="<?= htmlspecialchars($field['name']) ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>"></textarea>
|
|
|
292 |
<?php else: ?>
|
| 4 |
randell |
293 |
<input type="<?= htmlspecialchars($field['type']) ?>" name="<?= htmlspecialchars($field['name']) ?><?= $field['multiple'] ? '[]' : '' ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>" <?= $field['multiple'] ? 'multiple' : '' ?>>
|
|
|
294 |
<?php endif; ?>
|
| 2 |
rodolico |
295 |
</div>
|
| 4 |
randell |
296 |
<?php endforeach; ?>
|
| 2 |
rodolico |
297 |
|
|
|
298 |
<input type="submit" value="Submit">
|
|
|
299 |
</form>
|
|
|
300 |
</div>
|
|
|
301 |
</body>
|
|
|
302 |
</html>
|