Subversion Repositories web_pages

Rev

Rev 6 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
5 rodolico 103
                if ( $config['debug'] ) {
104
                   echo "_FILES <pre>" . print_r($_FILES, true) . "</pre>";
105
                }
4 randell 106
                $attachments = $_FILES[$field['name']];
2 rodolico 107
            } else {
108
                $message .= "{$field['label']}: " . htmlspecialchars($_POST[$field['name']]) . "\n";
109
            }
110
        }
4 randell 111
 
2 rodolico 112
        // Prepare to send email using PHPMailer
6 randell 113
        $mail = new PHPMailer(true);
114
 
2 rodolico 115
        try {
6 randell 116
            // Server settings
2 rodolico 117
            $mail->isSMTP();
118
            $mail->Host = $config['smtp_host'];
119
            $mail->SMTPAuth = true;
120
            $mail->Username = $config['smtp_username'];
121
            $mail->Password = $config['smtp_password'];
122
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
7 randell 123
            // if ignore_ssl is set to true, ignore SSL certificate verification
124
            if ($config['ignore_ssl']) {
125
                $mail->SMTPOptions = array(
126
                    'ssl' => array(
127
                        'verify_peer' => false,
128
                        'verify_peer_name' => false,
129
                        'allow_self_signed' => true
130
                    )
131
                );
132
            }
2 rodolico 133
            $mail->Port = $config['smtp_port'];
6 randell 134
 
135
            // Set the sender's email address from the form
136
            if (!empty($_POST['requestor_email']) && !empty($_POST['requestor_name'])) {
137
                $mail->setFrom($_POST['requestor_email'], $_POST['requestor_name']);
138
            } else {
139
                $mail->setFrom($config['from'], $config['from_name']);
140
            }
141
 
142
            // Add recipient
2 rodolico 143
            $mail->addAddress($config['to']);
144
            if (!empty($config['bcc'])) {
145
                $bccs = explode(',', $config['bcc']);
146
                foreach ($bccs as $bcc) {
147
                    $mail->addBCC($bcc);
148
                }
149
            }
6 randell 150
 
151
            // Add reply-to address
2 rodolico 152
            if (!empty($_POST['requestor_email']) && !empty($_POST['requestor_name'])) {
153
                $mail->AddReplyTo($_POST['requestor_email'], $_POST['requestor_name']);
154
            }
155
 
6 randell 156
            // Email subject and body
2 rodolico 157
            $mail->Subject = htmlspecialchars($_POST['short_description']);
158
            $mail->Body = $message;
159
 
160
            // Attach files if any
161
            if (!empty($attachments)) {
4 randell 162
                if ($config['debug']) {
163
                    echo "Attachments: <pre>" . print_r($attachments, true) . "</pre>";
2 rodolico 164
                }
4 randell 165
                if (is_array($attachments['name'])) {
166
                    for ($i = 0; $i < count($attachments['name']); $i++) {
167
                        if ($config['debug']) {
168
                            echo "Processing Attachment.<br />";
169
                            echo $attachments['name'][$i];
170
                        }
7 randell 171
                        // Only send if the attachment uploaded OK
172
                        if ($attachments['error'][$i] !== UPLOAD_ERR_OK) {
173
                            continue;
174
                        }
4 randell 175
                        $mail->addAttachment($attachments['tmp_name'][$i], $attachments['name'][$i]);
176
                    }
177
                }
2 rodolico 178
            }
179
 
6 randell 180
            // Send the email
2 rodolico 181
            $mail->send();
6 randell 182
            echo $config['success_message'];
2 rodolico 183
        } catch (Exception $e) {
6 randell 184
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
2 rodolico 185
        }
186
    }
187
}
188
?>
189
 
190
<!DOCTYPE html>
191
<html lang="en">
192
    <head>
193
        <meta charset="UTF-8">
194
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
195
        <title><?php echo htmlspecialchars($config['page_title']); ?></title>
196
        <style>
197
            body {
198
                font-family: Arial, sans-serif;
199
                background-color: #f4f4f4;
200
                margin: 0;
201
                padding: 20px;
202
            }
203
            .form-container {
204
                max-width: 600px;
205
                margin: 0 auto;
206
                background-color: #fff;
207
                padding: 20px;
208
                border-radius: 8px;
209
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
210
            }
211
            h1 {
212
                font-size: 24px;
213
                margin-bottom: 20px;
214
                color: #333;
215
            }
216
            label {
217
                display: block;
218
                margin-bottom: 5px;
219
                font-weight: bold;
220
            }
221
            .required-field::after {
222
                content: " *";
223
                color: red;
224
            }
225
            input[type="text"], input[type="email"], select, textarea {
226
                width: 100%;
227
                padding: 10px;
228
                margin-bottom: 15px;
229
                border: 1px solid #ccc;
230
                border-radius: 4px;
231
                font-size: 16px;
232
                box-sizing: border-box;
233
            }
234
            textarea {
235
                resize: vertical;
236
                min-height: 100px;
237
            }
238
            input[type="file"] {
239
                margin-bottom: 15px;
240
            }
241
            input[type="submit"] {
242
                background-color: #28a745;
243
                color: white;
244
                padding: 10px 20px;
245
                border: none;
246
                border-radius: 4px;
247
                font-size: 16px;
248
                cursor: pointer;
249
            }
250
            input[type="submit"]:hover {
251
                background-color: #218838;
252
            }
253
            .form-group {
254
                margin-bottom: 20px;
255
            }
256
            .hidden-fields {
257
                display: none;
258
            }
259
            .message-error {
260
                color: white;
261
                background-color: red;
262
                text-align: center;
263
                width: 100%;
264
                padding: 4px;                    
265
            }
266
            .message-success {
267
                color: white;
268
                background-color: green;
269
                text-align: center;
270
                width: 100%;padding: 4px;
271
            }
272
        </style>
273
    </head>
274
    <body>
275
        <div class="form-container">
4 randell 276
                <?php if (isset($result)): ?>
2 rodolico 277
                <div class="<?= $result_error ? 'message-error' : 'message-success' ?>">
4 randell 278
                <?= htmlspecialchars($result) ?>
2 rodolico 279
                </div>
280
            <?php endif; ?>
281
 
4 randell 282
                <?php if (isset($error) && !empty($error)): ?>
2 rodolico 283
                <div class="message-error">
4 randell 284
                <?= htmlspecialchars($error) ?>
2 rodolico 285
                </div>
4 randell 286
<?php endif; ?>
287
 
2 rodolico 288
            <h1><?php echo htmlspecialchars($config['page_title']); ?></h1>
289
            <form action="" method="POST" enctype="multipart/form-data">
290
                <div class="hidden-fields">
291
                    <input type="hidden" name="to" value="<?php echo htmlspecialchars($config['to']); ?>">
292
                    <input type="hidden" name="bcc" value="<?php echo htmlspecialchars($config['bcc']); ?>">
293
                </div>
294
 
4 randell 295
                        <?php foreach ($config['fields'] as $field): ?>
2 rodolico 296
                    <div class="form-group">
297
                        <label for="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required-field' : '' ?>">
4 randell 298
                        <?= htmlspecialchars($field['label']) ?>
2 rodolico 299
                        </label>
300
 
4 randell 301
                            <?php if ($field['type'] == 'select'): ?>
2 rodolico 302
                            <select name="<?= htmlspecialchars($field['name']) ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>">
303
                                <?php foreach ($field['options'] as $value => $label): ?>
7 randell 304
                                    <option value="<?= htmlspecialchars($value) ?>" <?= isset($_POST[$field['name']]) && $_POST[$field['name']] == $value ? 'selected' : '' ?>><?= htmlspecialchars($label) ?></option>
305
                                <?php endforeach; ?>
306
 
2 rodolico 307
                            </select>
308
                        <?php elseif ($field['type'] == 'textarea'): ?>
7 randell 309
                            <textarea name="<?= htmlspecialchars($field['name']) ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>"><?= isset($_POST[$field['name']]) ? htmlspecialchars($_POST[$field['name']]) : '' ?></textarea>
2 rodolico 310
                        <?php else: ?>
7 randell 311
                            <input type="<?= htmlspecialchars($field['type']) ?>" name="<?= htmlspecialchars($field['name']) ?><?= $field['multiple'] ? '[]' : '' ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>" <?= $field['multiple'] ? 'multiple' : '' ?>" value="<?= isset($_POST[$field['name']]) ? htmlspecialchars($_POST[$field['name']]) : '' ?>">
4 randell 312
                    <?php endif; ?>
2 rodolico 313
                    </div>
4 randell 314
<?php endforeach; ?>
2 rodolico 315
 
316
                <input type="submit" value="Submit">
317
            </form>
318
        </div>
319
    </body>
320
</html>