Subversion Repositories web_pages

Rev

Rev 7 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

// File version 0.5
// Load PHPmailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$config_file = 'config.php';

if (!file_exists($config_file)) {
    // Default configuration is generated here
    $default_config = <<<EOD
            <?php
                return [
                    'to' => 'your-email@example.com',
                    'bcc' => '',
                    'from' => 'no-reply@example.com',
                    'from_name' => 'mail sender',
                    'smtp_host' => 'smtp.example.com',
                    'smtp_port' => 587,
                    'smtp_username' => 'your-username',
                    'smtp_password' => 'your-password',
                    'debug' => false,
                    'page_title' => 'Support Request Form',
                    'fields' => [
                        [
                            'name' => 'requestor_name',
                            'type' => 'text',
                            'label' => 'Requestor Name',
                            'required' => true,
                        ],
                        [
                            'name' => 'requestor_phone',
                            'type' => 'text',
                            'label' => 'Requestor Phone',
                            'required' => false,
                        ],
                        [
                            'name' => 'requestor_email',
                            'type' => 'email',
                            'label' => 'Requestor Email',
                            'required' => true,
                        ],
                        [
                            'name' => 'checkbox_example',
                            'type' => 'checkbox',
                            'label' => 'Checkbox Example',
                            'required' => false,
                        ],
                        [
                            'name' => 'select_example',
                            'type' => 'select',
                            'label' => 'Select Example',
                            'required' => true,
                            'options' => [
                                '1' => 'option 1',
                                '2' => 'option 2',
                            ],
                        ],
                        [
                            'name' => 'attachments',
                            'type' => 'file',
                            'label' => 'Attachments',
                            'required' => false,
                            'multiple' => true, // Support multiple files
                        ],
                    ],
                    'success_message' => 'Your request has been sent successfully!',
                ]
            EOD;

    if (file_put_contents($config_file, $default_config) === false) {
        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>');
    } else {
        die('A default config file has been created at ' . $config_file . '. Please update it with your actual settings.');
    }
}

$config = require($config_file);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $error = '';
    // Validate required fields
    foreach ($config['fields'] as $field) {
        if ($field['required'] && empty($_POST[$field['name']])) {
            $error .= "The field '{$field['label']}' is required. ";
        }
    }

    if ($error) {
        // Error in form submission
        $error = "Please fill in all required fields.";
    } else {
        // Collect form data
        $message = '';
        foreach ($config['fields'] as $field) {
            if ($field['type'] === 'file' && isset($_FILES[$field['name']])) {
                // Handle file attachments separately
                if ( $config['debug'] ) {
                   echo "_FILES <pre>" . print_r($_FILES, true) . "</pre>";
                }
                $attachments = $_FILES[$field['name']];
            } else {
                $message .= "{$field['label']}: " . htmlspecialchars($_POST[$field['name']]) . "\n";
            }
        }

        // Prepare to send email using PHPMailer
        $mail = new PHPMailer(true);

        try {
            // Server settings
            $mail->isSMTP();
            $mail->Host = $config['smtp_host'];
            $mail->SMTPAuth = true;
            $mail->Username = $config['smtp_username'];
            $mail->Password = $config['smtp_password'];
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            // if ignore_ssl is set to true, ignore SSL certificate verification
            if ($config['ignore_ssl']) {
                $mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );
            }
            $mail->Port = $config['smtp_port'];

            // Set the sender's email address from the form
            if (!empty($_POST['requestor_email']) && !empty($_POST['requestor_name'])) {
                $mail->setFrom($_POST['requestor_email'], $_POST['requestor_name']);
            } else {
                $mail->setFrom($config['from'], $config['from_name']);
            }

            // Add recipient
            $mail->addAddress($config['to']);
            if (!empty($config['bcc'])) {
                $bccs = explode(',', $config['bcc']);
                foreach ($bccs as $bcc) {
                    $mail->addBCC($bcc);
                }
            }

            // Add reply-to address
            if (!empty($_POST['requestor_email']) && !empty($_POST['requestor_name'])) {
                $mail->AddReplyTo($_POST['requestor_email'], $_POST['requestor_name']);
            }

            // Email subject and body
            $mail->Subject = htmlspecialchars($_POST['short_description']);
            $mail->Body = $message;

            // Attach files if any
            if (!empty($attachments)) {
                if ($config['debug']) {
                    echo "Attachments: <pre>" . print_r($attachments, true) . "</pre>";
                }
                if (is_array($attachments['name'])) {
                    for ($i = 0; $i < count($attachments['name']); $i++) {
                        if ($config['debug']) {
                            echo "Processing Attachment.<br />";
                            echo $attachments['name'][$i];
                        }
                        // Only send if the attachment uploaded OK
                        if ($attachments['error'][$i] !== UPLOAD_ERR_OK) {
                            continue;
                        }
                        $mail->addAttachment($attachments['tmp_name'][$i], $attachments['name'][$i]);
                    }
                }
            }

            // Send the email
            $mail->send();
            echo $config['success_message'];
        } catch (Exception $e) {
            echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php echo htmlspecialchars($config['page_title']); ?></title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f4f4f4;
                margin: 0;
                padding: 20px;
            }
            .form-container {
                max-width: 600px;
                margin: 0 auto;
                background-color: #fff;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            }
            h1 {
                font-size: 24px;
                margin-bottom: 20px;
                color: #333;
            }
            label {
                display: block;
                margin-bottom: 5px;
                font-weight: bold;
            }
            .required-field::after {
                content: " *";
                color: red;
            }
            input[type="text"], input[type="email"], select, textarea {
                width: 100%;
                padding: 10px;
                margin-bottom: 15px;
                border: 1px solid #ccc;
                border-radius: 4px;
                font-size: 16px;
                box-sizing: border-box;
            }
            textarea {
                resize: vertical;
                min-height: 100px;
            }
            input[type="file"] {
                margin-bottom: 15px;
            }
            input[type="submit"] {
                background-color: #28a745;
                color: white;
                padding: 10px 20px;
                border: none;
                border-radius: 4px;
                font-size: 16px;
                cursor: pointer;
            }
            input[type="submit"]:hover {
                background-color: #218838;
            }
            .form-group {
                margin-bottom: 20px;
            }
            .hidden-fields {
                display: none;
            }
            .message-error {
                color: white;
                background-color: red;
                text-align: center;
                width: 100%;
                padding: 4px;                    
            }
            .message-success {
                color: white;
                background-color: green;
                text-align: center;
                width: 100%;padding: 4px;
            }
        </style>
    </head>
    <body>
        <div class="form-container">
                <?php if (isset($result)): ?>
                <div class="<?= $result_error ? 'message-error' : 'message-success' ?>">
                <?= htmlspecialchars($result) ?>
                </div>
            <?php endif; ?>

                <?php if (isset($error) && !empty($error)): ?>
                <div class="message-error">
                <?= htmlspecialchars($error) ?>
                </div>
<?php endif; ?>

            <h1><?php echo htmlspecialchars($config['page_title']); ?></h1>
            <form action="" method="POST" enctype="multipart/form-data">
                <div class="hidden-fields">
                    <input type="hidden" name="to" value="<?php echo htmlspecialchars($config['to']); ?>">
                    <input type="hidden" name="bcc" value="<?php echo htmlspecialchars($config['bcc']); ?>">
                </div>

                        <?php foreach ($config['fields'] as $field): ?>
                    <div class="form-group">
                        <label for="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required-field' : '' ?>">
                        <?= htmlspecialchars($field['label']) ?>
                        </label>

                            <?php if ($field['type'] == 'select'): ?>
                            <select name="<?= htmlspecialchars($field['name']) ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>">
                                <?php foreach ($field['options'] as $value => $label): ?>
                                    <option value="<?= htmlspecialchars($value) ?>" <?= isset($_POST[$field['name']]) && $_POST[$field['name']] == $value ? 'selected' : '' ?>><?= htmlspecialchars($label) ?></option>
                                <?php endforeach; ?>
                            
                            </select>
                        <?php elseif ($field['type'] == 'textarea'): ?>
                            <textarea name="<?= htmlspecialchars($field['name']) ?>" id="<?= htmlspecialchars($field['name']) ?>" class="<?= $field['required'] ? 'required' : '' ?>"><?= isset($_POST[$field['name']]) ? htmlspecialchars($_POST[$field['name']]) : '' ?></textarea>
                        <?php else: ?>
                            <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']]) : (isset($field['value']) ? htmlspecialchars($field['value']) : '') ?>">
                    <?php endif; ?>
                    </div>
<?php endforeach; ?>

                <input type="submit" value="Submit">
            </form>
        </div>
    </body>
</html>