Subversion Repositories web_pages

Rev

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