Subversion Repositories web_pages

Rev

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